1
1
import React from 'react' ;
2
2
import { connect } from 'react-redux' ;
3
- import { ProfileType } from '../../types' ;
3
+ import { ProfileType , SubmitType } from '../../types' ;
4
4
import ReactRouterPropTypes from 'react-router-prop-types' ;
5
5
import { readEndpoint } from 'redux-json-api' ;
6
6
import PropTypes from 'prop-types' ;
7
7
import { bindActionCreators } from 'redux' ;
8
8
import E404 from '../404/' ;
9
9
import ProfileComponent from '../../components/profile' ;
10
+ import _get from 'lodash/get' ;
10
11
11
12
class Profile extends React . Component {
12
13
static propTypes = {
@@ -15,6 +16,7 @@ class Profile extends React.Component {
15
16
match : ReactRouterPropTypes . match ,
16
17
username : PropTypes . string ,
17
18
self : PropTypes . bool . isRequired ,
19
+ submits : PropTypes . arrayOf ( SubmitType ) ,
18
20
}
19
21
20
22
constructor ( props ) {
@@ -25,32 +27,38 @@ class Profile extends React.Component {
25
27
}
26
28
27
29
componentWillMount ( ) {
28
- this . props . readEndpoint ( `profile/${ this . props . username || '' } ` ) . then ( ( ) => {
30
+ this . props . readEndpoint ( `profile/${ this . props . username || '' } ` ) . then ( ( r ) => {
29
31
this . setState ( { loading : false } ) ;
32
+ const user = _get ( r , 'body.data[0]' , null ) ;
33
+ if ( user ) {
34
+ this . props . readEndpoint ( `submit/all/user/${ user . id } ` ) ;
35
+ }
30
36
} ) ;
31
37
}
32
38
33
39
render ( ) {
34
- const { profile, self } = this . props ;
40
+ const { profile, self, submits } = this . props ;
35
41
return (
36
42
< div >
37
- { ! this . state . loading && this . props . profile && < ProfileComponent profile = { profile } self = { self } /> }
43
+ { ! this . state . loading && this . props . profile && < ProfileComponent profile = { profile } self = { self } submits = { submits } /> }
38
44
{ ! this . state . loading && ! this . props . profile && < E404 /> }
39
45
</ div > ) ;
40
46
}
41
47
}
42
48
43
49
const mapStateToProps = ( state , ownProps ) => {
44
50
const { profile, api } = state ;
45
- const { users = { data : [ ] } } = api ;
51
+ const { users = { data : [ ] } , submit = { data : [ ] } } = api ;
46
52
47
53
const username = ownProps . match . params . username ? ownProps . match . params . username : profile . username ;
48
54
const user = users . data . find ( ( u ) => u . attributes . username === username ) ;
55
+ const submits = submit . data . filter ( s => s . attributes . _user === user . id ) ;
49
56
50
57
return {
51
58
profile : user ? user . attributes : undefined ,
52
59
username,
53
60
self : ! ownProps . match . params . username || ownProps . match . params . username === profile . username ,
61
+ submits,
54
62
} ;
55
63
} ;
56
64
0 commit comments