1
1
import React , { Component } from 'react' ;
2
+ import { connect } from 'react-redux' ;
3
+ import { compose } from 'recompose' ;
2
4
3
5
import withAuthorization from '../Session/withAuthorization' ;
4
6
import { db } from '../../firebase' ;
@@ -9,27 +11,23 @@ const fromObjectToList = (object) =>
9
11
: [ ] ;
10
12
11
13
class HomePage extends Component {
12
- constructor ( props ) {
13
- super ( props ) ;
14
-
15
- this . state = {
16
- users : [ ]
17
- } ;
18
- }
19
-
20
14
componentDidMount ( ) {
15
+ const { onSetUsers } = this . props ;
16
+
21
17
db . onceGetUsers ( ) . then ( snapshot =>
22
- this . setState ( ( ) => ( { users : fromObjectToList ( snapshot . val ( ) ) } ) )
18
+ onSetUsers ( fromObjectToList ( snapshot . val ( ) ) )
23
19
) ;
24
20
}
25
21
26
22
render ( ) {
23
+ const { users } = this . props ;
24
+
27
25
return (
28
26
< div >
29
27
< h1 > Home</ h1 >
30
28
< p > The Home Page is accessible by every signed in user.</ p >
31
29
32
- { ! ! this . state . users . length && < UserList users = { this . state . users } /> }
30
+ { ! ! users . length && < UserList users = { users } /> }
33
31
</ div >
34
32
) ;
35
33
}
@@ -43,4 +41,15 @@ const UserList = ({ users }) =>
43
41
) }
44
42
</ div >
45
43
46
- export default withAuthorization ( true ) ( HomePage ) ;
44
+ const mapStateToProps = ( state ) => ( {
45
+ users : state . userState . users ,
46
+ } ) ;
47
+
48
+ const mapDispatchToProps = ( dispatch ) => ( {
49
+ onSetUsers : ( users ) => dispatch ( { type : 'USERS_SET' , users } ) ,
50
+ } ) ;
51
+
52
+ export default compose (
53
+ withAuthorization ( true ) ,
54
+ connect ( mapStateToProps , mapDispatchToProps )
55
+ ) ( HomePage ) ;
0 commit comments