import React, { PropTypes} from 'react' import { connect } from 'react-redux' import { CREATE_MODE, IDLE_MODE, switchUser } from './actions' import UserNavigator from './HomePage/UserNavigator' import ArticleNavigator from './HomePage/ArticleNavigator' import ArticleTopBar from './HomePage/ArticleTopBar' import ArticleList from './HomePage/ArticleList' import ArticleDetail from './HomePage/ArticleDetail' import { findWhere, findIndex, pick } from 'lodash' import keygen from 'boost/keygen' import { NEW } from './actions' class HomeContainer extends React.Component { componentDidMount () { const { dispatch } = this.props dispatch(switchUser(this.props.params.userId)) } componentWillReceiveProps (nextProps) { const { dispatch, status } = this.props if (nextProps.params.userId !== status.userId) { dispatch(switchUser(nextProps.params.userId)) } } render () { const { dispatch, status, users, activeUser, articles, activeArticle } = this.props return (
) } } function remap (state) { let status = state.status let currentUser = state.currentUser let teams = Array.isArray(currentUser.Teams) ? currentUser.Teams : [] let users = [currentUser, ...teams] let activeUser = findWhere(users, {id: parseInt(status.userId, 10)}) if (activeUser == null) activeUser = users[0] let articles = state.articles['team-' + activeUser.id] let activeArticle = findWhere(articles, {id: status.articleId}) if (activeArticle == null) activeArticle = articles[0] // remove Unsaved new article if user is not CREATE_MODE if (status.mode !== CREATE_MODE) { let targetIndex = findIndex(articles, article => article.status === NEW) if (targetIndex >= 0) articles.splice(targetIndex, 1) } // switching CREATE_MODE // restrict // 1. team have one folder at least // or Change IDLE MODE if (status.mode === CREATE_MODE && activeUser.Folders.length > 0) { var newArticle = findWhere(articles, {status: 'NEW'}) if (newArticle == null) { newArticle = { id: keygen(), title: '', content: '', mode: 'markdown', Tags: [], User: pick(currentUser, ['email', 'name', 'profileName']), FolderId: activeUser.Folders[0].id, status: NEW } articles.unshift(newArticle) } activeArticle = newArticle } else if (status.mode === CREATE_MODE) { status.mode = IDLE_MODE } return { users, activeUser, status, articles, activeArticle } } HomeContainer.propTypes = { users: PropTypes.array, activeUser: PropTypes.object, params: PropTypes.shape({ userId: PropTypes.string }), status: PropTypes.shape({ userId: PropTypes.string, folderId: PropTypes.number }), articles: PropTypes.array, activeArticle: PropTypes.shape(), dispatch: PropTypes.func } export default connect(remap)(HomeContainer)