From e1ef14ab7d213876b271404306412a8777ecbb0e Mon Sep 17 00:00:00 2001 From: omiceron Date: Sun, 4 Feb 2018 22:26:13 +0500 Subject: [PATCH 1/2] HT5.1 - Normalized articles structure applied --- src/components/Article/index.js | 12 ++++++++++-- src/components/ArticleList.js | 8 ++++---- src/components/CommentList.js | 4 ++-- src/components/Filters/Select.js | 10 ++++++---- src/reducer/articles.js | 16 ++++++++++++---- src/selectors/index.js | 10 ++++++++-- 6 files changed, 42 insertions(+), 18 deletions(-) diff --git a/src/components/Article/index.js b/src/components/Article/index.js index a3e834e..12cfa69 100644 --- a/src/components/Article/index.js +++ b/src/components/Article/index.js @@ -5,6 +5,7 @@ import CSSTransition from 'react-addons-css-transition-group' import {connect} from 'react-redux' import CommentList from '../CommentList' import {deleteArticle} from '../../AC' +import {createArticleSelector} from '../../selectors' import './style.css' class Article extends PureComponent { @@ -85,5 +86,12 @@ class Article extends PureComponent { } - -export default connect(null, { deleteArticle })(Article) \ No newline at end of file +const createMapStateToProps = () => { + const articleSelector = createArticleSelector() + return (state, ownProps) => { + return { + article: articleSelector(state, ownProps) + } + } +} +export default connect(createMapStateToProps, { deleteArticle })(Article) \ No newline at end of file diff --git a/src/components/ArticleList.js b/src/components/ArticleList.js index bc1c261..f306ae6 100644 --- a/src/components/ArticleList.js +++ b/src/components/ArticleList.js @@ -10,10 +10,10 @@ class ArticleList extends Accordion { console.log('---', 'rerendering article list') const {articles} = this.props if (!articles.length) return

No Articles

- const articleElements = articles.map((article) =>
  • -
  • +
  • ) return ( diff --git a/src/components/CommentList.js b/src/components/CommentList.js index 8592d22..86c8704 100644 --- a/src/components/CommentList.js +++ b/src/components/CommentList.js @@ -6,7 +6,7 @@ import toggleOpen from '../decorators/toggleOpen' class CommentList extends Component { static propTypes = { - comments: PropTypes.array.isRequired, + comments: PropTypes.array,//.isRequired, //from toggleOpen decorator isOpen: PropTypes.bool, toggleOpen: PropTypes.func @@ -24,7 +24,7 @@ class CommentList extends Component { } getBody() { - const {comments, isOpen} = this.props + const {comments = [], isOpen} = this.props if (!isOpen) return null const body = comments.length ? ( diff --git a/src/components/Filters/Select.js b/src/components/Filters/Select.js index 1a91857..f49d684 100644 --- a/src/components/Filters/Select.js +++ b/src/components/Filters/Select.js @@ -8,16 +8,18 @@ import 'react-select/dist/react-select.css' class SelectFilter extends Component { static propTypes = { - articles: PropTypes.array.isRequired + articles: PropTypes.object.isRequired }; handleChange = selected => this.props.changeSelection(selected.map(option => option.value)) render() { const { articles, selected } = this.props - const options = articles.map(article => ({ - label: article.title, - value: article.id +// console.log(articles) + + const options = Object.values(articles).map(({title, id}) => ({ + label: title, + value: id })) return