@@ -8,13 +8,27 @@ import ArticleList from './HomePage/ArticleList'
88import ArticleDetail from './HomePage/ArticleDetail'
99import { findWhere , findIndex , pick } from 'lodash'
1010import keygen from 'boost/keygen'
11- import { NEW } from './actions'
11+ import { NEW , refreshArticles } from './actions'
12+ import api from 'boost/api'
1213
13- class HomeContainer extends React . Component {
14+ class HomePage extends React . Component {
1415 componentDidMount ( ) {
1516 const { dispatch } = this . props
1617
1718 dispatch ( switchUser ( this . props . params . userId ) )
19+
20+ let currentUser = JSON . parse ( localStorage . getItem ( 'currentUser' ) )
21+ let users = [ currentUser ] . concat ( currentUser . Teams )
22+ users . forEach ( user => {
23+ api . fetchArticles ( user . id )
24+ . then ( res => {
25+ dispatch ( refreshArticles ( user . id , res . body ) )
26+ } )
27+ . catch ( err => {
28+ if ( err . status == null ) throw err
29+ console . error ( err )
30+ } )
31+ } )
1832 }
1933
2034 componentWillReceiveProps ( nextProps ) {
@@ -29,7 +43,7 @@ class HomeContainer extends React.Component {
2943 const { dispatch, status, users, activeUser, articles, activeArticle } = this . props
3044
3145 return (
32- < div className = 'HomeContainer ' >
46+ < div className = 'HomePage ' >
3347 < UserNavigator users = { users } />
3448 < ArticleNavigator dispatch = { dispatch } activeUser = { activeUser } status = { status } />
3549 < ArticleTopBar />
@@ -44,13 +58,17 @@ function remap (state) {
4458 let status = state . status
4559
4660 let currentUser = state . currentUser
61+ if ( currentUser == null ) return state
4762 let teams = Array . isArray ( currentUser . Teams ) ? currentUser . Teams : [ ]
4863
4964 let users = [ currentUser , ...teams ]
5065 let activeUser = findWhere ( users , { id : parseInt ( status . userId , 10 ) } )
5166 if ( activeUser == null ) activeUser = users [ 0 ]
67+
5268 let articles = state . articles [ 'team-' + activeUser . id ]
53- let activeArticle = findWhere ( articles , { id : status . articleId } )
69+ if ( articles == null ) articles = [ ]
70+
71+ let activeArticle = findWhere ( articles , { key : status . articleKey } )
5472 if ( activeArticle == null ) activeArticle = articles [ 0 ]
5573
5674 // remove Unsaved new article if user is not CREATE_MODE
@@ -68,7 +86,8 @@ function remap (state) {
6886 var newArticle = findWhere ( articles , { status : 'NEW' } )
6987 if ( newArticle == null ) {
7088 newArticle = {
71- id : keygen ( ) ,
89+ id : null ,
90+ key : keygen ( ) ,
7291 title : '' ,
7392 content : '' ,
7493 mode : 'markdown' ,
@@ -84,16 +103,18 @@ function remap (state) {
84103 status . mode = IDLE_MODE
85104 }
86105
87- return {
106+ let props = {
88107 users,
89108 activeUser,
90109 status,
91110 articles,
92111 activeArticle
93112 }
113+ console . log ( props )
114+ return props
94115}
95116
96- HomeContainer . propTypes = {
117+ HomePage . propTypes = {
97118 users : PropTypes . array ,
98119 activeUser : PropTypes . object ,
99120 params : PropTypes . shape ( {
@@ -108,4 +129,4 @@ HomeContainer.propTypes = {
108129 dispatch : PropTypes . func
109130}
110131
111- export default connect ( remap ) ( HomeContainer )
132+ export default connect ( remap ) ( HomePage )
0 commit comments