1
1
import React , { Component } from 'react' ;
2
2
import { connect } from 'react-redux' ;
3
3
import PropTypes from 'prop-types' ;
4
-
5
- import { accounts as accountsAction } from '../actions' ;
4
+ import { accounts as accountsAction ,
5
+ initBlockHeader ,
6
+ stopBlockHeader } from '../actions' ;
6
7
import Accounts from '../components/Accounts' ;
7
- import DataWrapper from "../components/DataWrapper" ;
8
8
import { getAccounts } from "../reducers/selectors" ;
9
9
import PageHead from "../components/PageHead" ;
10
10
11
+ const MAX_ACCOUNTS = 10 ;
12
+
11
13
class AccountsContainer extends Component {
14
+ constructor ( props ) {
15
+ super ( props ) ;
16
+
17
+ this . numAccountsToDisplay = this . props . numAccountsToDisplay || MAX_ACCOUNTS ;
18
+ this . state = { currentPage : 1 } ;
19
+ }
20
+
12
21
componentDidMount ( ) {
13
22
this . props . fetchAccounts ( ) ;
23
+ this . props . initBlockHeader ( ) ;
24
+ }
25
+
26
+ componentWillUnmount ( ) {
27
+ this . props . stopBlockHeader ( ) ;
28
+ }
29
+
30
+ get numberOfAccounts ( ) {
31
+ if ( this . _numberOfAccounts === undefined ) {
32
+ this . _numberOfAccounts = this . props . accounts . length ;
33
+ }
34
+ return this . _numberOfAccounts ;
35
+ }
36
+
37
+ get numberOfPages ( ) {
38
+ if ( this . _numberOfPages === undefined ) {
39
+ this . _numberOfPages = Math . ceil (
40
+ this . numberOfAccounts / this . numAccountsToDisplay
41
+ ) ;
42
+ }
43
+ return this . _numberOfPages ;
44
+ }
45
+
46
+ resetNums ( ) {
47
+ this . _numberOfAccounts = undefined ;
48
+ this . _numberOfPages = undefined ;
49
+ }
50
+
51
+ changePage ( newPage ) {
52
+ if ( newPage <= 0 ) {
53
+ newPage = 1 ;
54
+ } else if ( newPage > this . numberOfPages ) {
55
+ newPage = this . numberOfPages ;
56
+ }
57
+ this . setState ( { currentPage : newPage } ) ;
58
+ this . props . fetchAccounts ( ) ;
59
+ }
60
+
61
+ get currentAccounts ( ) {
62
+ return this . props . accounts
63
+ . filter ( ( account ) => {
64
+ const index = (
65
+ ( account . index + 1 ) -
66
+ ( this . numAccountsToDisplay * ( this . state . currentPage - 1 ) )
67
+ ) ;
68
+ return index <= this . numAccountsToDisplay && index > 0 ;
69
+ } ) ;
14
70
}
15
71
16
72
render ( ) {
73
+ this . resetNums ( ) ;
17
74
return (
18
75
< React . Fragment >
19
- < PageHead title = "Accounts" enabled = { this . props . overridePageHead } description = "Summary view of the accounts configured for Embark" />
20
- < DataWrapper shouldRender = { this . props . accounts . length > 0 } { ...this . props } render = { ( { accounts} ) => (
21
- < Accounts accounts = { accounts } />
22
- ) } />
76
+ < PageHead
77
+ title = "Accounts"
78
+ enabled = { this . props . overridePageHead }
79
+ description = "Summary view of the accounts configured for Embark" />
80
+ < Accounts accounts = { this . currentAccounts }
81
+ numberOfPages = { this . numberOfPages }
82
+ changePage = { ( newPage ) => this . changePage ( newPage ) }
83
+ currentPage = { this . state . currentPage } />
23
84
</ React . Fragment >
24
85
) ;
25
86
}
26
87
}
27
88
28
89
function mapStateToProps ( state ) {
29
- return { accounts : getAccounts ( state ) , error : state . errorMessage , loading : state . loading } ;
90
+ return {
91
+ accounts : getAccounts ( state ) ,
92
+ error : state . errorMessage ,
93
+ loading : state . loading
94
+ } ;
30
95
}
31
96
32
97
AccountsContainer . propTypes = {
33
98
accounts : PropTypes . arrayOf ( PropTypes . object ) ,
34
99
fetchAccounts : PropTypes . func ,
100
+ numAccountsToDisplay : PropTypes . number ,
101
+ initBlockHeader : PropTypes . func ,
102
+ stopBlockHeader : PropTypes . func ,
35
103
error : PropTypes . string ,
36
104
loading : PropTypes . bool ,
37
105
overridePageHead : PropTypes . bool
@@ -40,6 +108,8 @@ AccountsContainer.propTypes = {
40
108
export default connect (
41
109
mapStateToProps ,
42
110
{
43
- fetchAccounts : accountsAction . request
111
+ fetchAccounts : accountsAction . request ,
112
+ initBlockHeader,
113
+ stopBlockHeader
44
114
} ,
45
115
) ( AccountsContainer ) ;
0 commit comments