From f24a29b18a0675e5079dd8def7a385ad5be2e5ba Mon Sep 17 00:00:00 2001 From: Sergey Breslavets Date: Sun, 4 Feb 2018 19:49:54 +0300 Subject: [PATCH 1/6] =?UTF-8?q?=D1=83=D1=80=D0=B0=20=D0=B2=D1=8B=D0=BD?= =?UTF-8?q?=D0=B5=D1=81=20=D1=81=D1=82=D0=B0=D1=82=D1=8C=D0=B8=20ID=20=20-?= =?UTF-8?q?>=20key?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/Article/index.js | 44 ++++++++++++++++++++++++-------- src/components/ArticleList.js | 21 ++++++++++----- src/components/Filters/Select.js | 4 +-- src/reducer/articles.js | 38 +++++++++++++++++++++++---- src/selectors/index.js | 15 ++++++++--- 5 files changed, 96 insertions(+), 26 deletions(-) diff --git a/src/components/Article/index.js b/src/components/Article/index.js index a3e834e..a83080d 100644 --- a/src/components/Article/index.js +++ b/src/components/Article/index.js @@ -7,16 +7,18 @@ import CommentList from '../CommentList' import {deleteArticle} from '../../AC' import './style.css' +import {createArticleSelector} from '../../selectors' + class Article extends PureComponent { - static propTypes = { - article: PropTypes.shape({ - title: PropTypes.string.isRequired, - text: PropTypes.string, - comments: PropTypes.array - }).isRequired, - isOpen: PropTypes.bool, - toggleOpen: PropTypes.func - } + // static propTypes = { + // article: PropTypes.shape({ + // title: PropTypes.string.isRequired, + // text: PropTypes.string, + // comments: PropTypes.array + // }).isRequired, + // isOpen: PropTypes.bool, + // toggleOpen: PropTypes.func + // } constructor(props) { super(props) @@ -84,6 +86,28 @@ class Article extends PureComponent { } } +const createMapStateToProps = (state, ownProps) =>{ + + const articleSelector = createArticleSelector() + + return ( state, ownProps ) => { + return { + article: articleSelector(state , ownProps) + } + } + + +} + +// const createMapStateToProps = () => { +// const commentSelector = createCommentSelector() +// return (state, ownProps) => { +// return { +// comment: commentSelector(state, ownProps) +// } +// } +// } -export default connect(null, { deleteArticle })(Article) \ No newline at end of file +// export default connect(createMapStateToProps)(Article) +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..a3cb8b1 100644 --- a/src/components/ArticleList.js +++ b/src/components/ArticleList.js @@ -7,14 +7,23 @@ import {filtratedArticlesSelector} from '../selectors' class ArticleList extends Accordion { render() { - console.log('---', 'rerendering article list') + + + + + console.log('---', 'rerendering article list' ) const {articles} = this.props + console.log(articles) if (!articles.length) return

No Articles

- const articleElements = articles.map((article) =>
  • -
  • + +
    +
  • ) return (
      @@ -29,9 +38,9 @@ ArticleList.defaultProps = { articles: [] } -ArticleList.propTypes = { - articles: PropTypes.array.isRequired -} +// ArticleList.propTypes = { +// articles: PropTypes.array.isRequired +// } export default connect(state => { console.log('---', 'connect updated') diff --git a/src/components/Filters/Select.js b/src/components/Filters/Select.js index 1a91857..0bf8716 100644 --- a/src/components/Filters/Select.js +++ b/src/components/Filters/Select.js @@ -8,14 +8,14 @@ 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 => ({ + const options = Object.values(articles).map(article => ({ label: article.title, value: article.id })) diff --git a/src/reducer/articles.js b/src/reducer/articles.js index 480a522..04f52a7 100644 --- a/src/reducer/articles.js +++ b/src/reducer/articles.js @@ -1,13 +1,41 @@ import { DELETE_ARTICLE } from '../constants' -import {normalizedArticles as defaultArticles} from '../fixtures' +import { normalizedArticles as defaultArticles } from '../fixtures' -export default (articlesState = defaultArticles, action) => { - const { type, payload } = action +const articleMap = defaultArticles.reduce((acc, article) => ({ + ...acc, + [article.id]: article +}), {}) + + +export default (articlesState = articleMap, action) => { + const { type, payload } = action + /// исправить потом switch (type) { case DELETE_ARTICLE: - return articlesState.filter(article => article.id !== payload.id) + return articlesState //.filter(article => article.id !== payload.id) } return articlesState -} \ No newline at end of file +} + + + + + +// import {normalizedComments as defaultComments} from '../fixtures' + +// const commentsMap = defaultComments.reduce((acc, comment) => ({ +// ...acc, +// [comment.id]: comment +// }), {}) + +// export default (state = commentsMap, action) => { +// const { type } = action + +// switch (type) { + +// } + +// return state +// } \ No newline at end of file diff --git a/src/selectors/index.js b/src/selectors/index.js index 4e3172b..78f2e50 100644 --- a/src/selectors/index.js +++ b/src/selectors/index.js @@ -1,4 +1,4 @@ -import {createSelector} from 'reselect' +import { createSelector } from 'reselect' export const articlesSelector = state => state.articles export const filtersSelector = state => state.filters @@ -12,11 +12,20 @@ export const createCommentSelector = () => createSelector(commentsSelector, idSe export const filtratedArticlesSelector = createSelector(articlesSelector, filtersSelector, (articles, filters) => { console.log('---', 'computing filters') - const {selected, dateRange: {from, to}} = filters + const { selected, dateRange: { from, to } } = filters - return articles.filter(article => { + return Object.values(articles).filter(article => { const published = Date.parse(article.date) return (!selected.length || selected.includes(article.id)) && (!from || !to || (published > from && published < to)) }) +}) + + +// export const articleSelector = state => state.article +export const idArtile = (_, props) => props.id + +export const createArticleSelector = () => createSelector(articlesSelector, idArtile, (articles, id) => { + console.log("search article", id) + return articles[id] }) \ No newline at end of file From 70e3a1ed64d45285804d027c582ee68a779d0a34 Mon Sep 17 00:00:00 2001 From: Sergey Breslavets Date: Sun, 4 Feb 2018 20:13:03 +0300 Subject: [PATCH 2/6] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D1=83=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/reducer/articles.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/reducer/articles.js b/src/reducer/articles.js index 04f52a7..94bc5f5 100644 --- a/src/reducer/articles.js +++ b/src/reducer/articles.js @@ -13,7 +13,17 @@ export default (articlesState = articleMap, action) => { /// исправить потом switch (type) { case DELETE_ARTICLE: - return articlesState //.filter(article => article.id !== payload.id) + + const articles = Object.keys(articlesState).reduce((obj, key) => { + if (key !== payload.id) return {...obj, [key]: articlesState[key] } + return obj + }, {}) + + return articles + + + + // return articlesState.filter(article => article.id !== payload.id) } return articlesState From b9b211988cca5743fe06b8a633d8600ed68c57cb Mon Sep 17 00:00:00 2001 From: Sergey Breslavets Date: Sun, 4 Feb 2018 20:39:56 +0300 Subject: [PATCH 3/6] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=20=D0=BC=D0=B8=D0=B4=D0=B5=D0=BB=D0=B2=D0=B0=D1=80=D1=83=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20=D0=B3=D0=B5=D0=BD=D0=B5=D1=80=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D0=B8=20id?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/constants/index.js | 6 +++++- src/middlewares/newIdComment.js | 23 +++++++++++++++++++++++ src/store/index.js | 12 ++++++------ 3 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 src/middlewares/newIdComment.js diff --git a/src/constants/index.js b/src/constants/index.js index a2f0a80..667c0bc 100644 --- a/src/constants/index.js +++ b/src/constants/index.js @@ -3,4 +3,8 @@ export const INCREMENT = 'INCREMENT' export const DELETE_ARTICLE = 'DELETE_ARTICLE' export const CHANGE_SELECTION = 'CHANGE_SELECTION' -export const CHANGE_DATE_RANGE = 'CHANGE_DATE_RANGE' \ No newline at end of file +export const CHANGE_DATE_RANGE = 'CHANGE_DATE_RANGE' + + + +export const ADD_NEW_COMMENT = 'ADD_NEW_COMMENT' \ No newline at end of file diff --git a/src/middlewares/newIdComment.js b/src/middlewares/newIdComment.js new file mode 100644 index 0000000..452a222 --- /dev/null +++ b/src/middlewares/newIdComment.js @@ -0,0 +1,23 @@ +import { ADD_NEW_COMMENT } from '../constants' + + +export default store => next => action => { + console.log('--- state before: ', store.getState()) + console.log('--- action: ', action) + + function randId() { + return (Math.random() * 1e32).toString(36); + } + + // console.log(randId()) // для теста + + + if (action.type === ADD_NEW_COMMENT) { + console.log("new id" + randId()) + } + + + + next(action) + console.log('--- state after: ', store.getState()) +} \ No newline at end of file diff --git a/src/store/index.js b/src/store/index.js index f83d367..a1813f2 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -1,16 +1,16 @@ -import {createStore, applyMiddleware, compose} from 'redux' +import { createStore, applyMiddleware, compose } from 'redux' import rootReducer from '../reducer' import logger from '../middlewares/logger' - +import newIdComment from '../middlewares/newIdComment' const composeEnhancers = typeof window === 'object' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ? - window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({ - // Specify extension’s options like name, actionsBlacklist, actionsCreators, serialize... - }) : compose + window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({ + // Specify extension’s options like name, actionsBlacklist, actionsCreators, serialize... + }) : compose const enhancer = composeEnhancers( - applyMiddleware(logger) + applyMiddleware(logger, newIdComment) ) const store = createStore(rootReducer, enhancer) From 6422a3bb1c6846c0f506d50f4efc7eaf6c44d0d7 Mon Sep 17 00:00:00 2001 From: Sergey Breslavets Date: Sun, 4 Feb 2018 21:36:41 +0300 Subject: [PATCH 4/6] =?UTF-8?q?=D1=83=D1=80=D0=B0=20=D0=B4=D0=BE=D0=B1?= =?UTF-8?q?=D0=B0=D0=B2=D0=B8=D0=BB=20=D0=B2=20=D1=81=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D0=B5=20=D0=BA=D0=BE=D0=BC=D0=B5=D0=BD=D1=82=20=D0=BD=D0=BE?= =?UTF-8?q?=D0=B2=D1=8B=D0=B9=20=D0=BA=D0=BE=D0=BC=D0=B5=D0=BD=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/AC/index.js | 11 ++++++++++- src/components/CommentForm/index.js | 15 ++++++++++++++- src/middlewares/newIdComment.js | 5 ++++- src/reducer/comments.js | 14 ++++++++++++-- 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/AC/index.js b/src/AC/index.js index dda2dd6..4227ab3 100644 --- a/src/AC/index.js +++ b/src/AC/index.js @@ -1,4 +1,4 @@ -import {INCREMENT, DELETE_ARTICLE, CHANGE_DATE_RANGE, CHANGE_SELECTION} from '../constants' +import { INCREMENT, DELETE_ARTICLE, CHANGE_DATE_RANGE, CHANGE_SELECTION, ADD_NEW_COMMENT } from '../constants' export function increment() { return { @@ -26,3 +26,12 @@ export function changeSelection(selected) { payload: { selected } } } + + +export function addNewComment(id, comment) { + return { + type: ADD_NEW_COMMENT, + payload: { id, comment } + } + +} \ No newline at end of file diff --git a/src/components/CommentForm/index.js b/src/components/CommentForm/index.js index 3685a10..b574564 100644 --- a/src/components/CommentForm/index.js +++ b/src/components/CommentForm/index.js @@ -1,6 +1,11 @@ import React, { Component } from 'react' import './style.css' +import {connect} from 'react-redux' +import {addNewComment} from '../../AC' + + + class CommentForm extends Component { static propTypes = { }; @@ -30,6 +35,13 @@ class CommentForm extends Component { user: '', text: '' }) + const {addNewComment} = this.props // подключим нашу функцию из пропса конеекта + + const id = "" // сделаем его пустым и подменим его в миделваре + + addNewComment(id, { user: this.state.user, text: this.state.text } ) + + } isValidForm = () => ['user', 'text'].every(this.isValidField) @@ -58,4 +70,5 @@ const limits = { } } -export default CommentForm \ No newline at end of file +// подключим к стору (редаксу) +export default connect(null, { addNewComment })(CommentForm) \ No newline at end of file diff --git a/src/middlewares/newIdComment.js b/src/middlewares/newIdComment.js index 452a222..17fcc77 100644 --- a/src/middlewares/newIdComment.js +++ b/src/middlewares/newIdComment.js @@ -13,9 +13,12 @@ export default store => next => action => { if (action.type === ADD_NEW_COMMENT) { + console.log("new id" + randId()) - } + action.payload.id = randId() + } + console.log('--- new pay ac', action) next(action) diff --git a/src/reducer/comments.js b/src/reducer/comments.js index 30bd10c..0d9fcdb 100644 --- a/src/reducer/comments.js +++ b/src/reducer/comments.js @@ -1,5 +1,5 @@ -import { } from '../constants' -import {normalizedComments as defaultComments} from '../fixtures' +import { ADD_NEW_COMMENT } from '../constants' +import { normalizedComments as defaultComments } from '../fixtures' const commentsMap = defaultComments.reduce((acc, comment) => ({ ...acc, @@ -10,7 +10,17 @@ export default (state = commentsMap, action) => { const { type } = action switch (type) { + case ADD_NEW_COMMENT: + { + console.log("==new data" + action.payload.id) + // return state + return { + ...state, + [action.payload.id]: { user: action.payload.comment.user, text: action.payload.comment.text } + } + + } } return state From 21cca2b32da1544833e08a57a29a8545fb506bae Mon Sep 17 00:00:00 2001 From: Sergey Breslavets Date: Sun, 4 Feb 2018 22:09:20 +0300 Subject: [PATCH 5/6] =?UTF-8?q?=D0=BD=D0=B5=20=D1=83=D1=81=D0=BF=D0=B5?= =?UTF-8?q?=D0=BB=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D1=82=D1=8C=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D1=83=20=D0=B4?= =?UTF-8?q?=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B2?= =?UTF-8?q?=20=D0=BB=D0=B8=D1=81=D1=82=20=D0=BA=D0=BE=D0=BC=D0=B5=D0=BD?= =?UTF-8?q?=D0=BD=D0=BE=D0=B2=20=20=D0=BD=D0=BE=20=D0=BF=D1=80=D0=B8=D0=BD?= =?UTF-8?q?=D1=86=D0=B5=D0=BF=D0=B8=20=D0=BF=D0=BE=D0=BD=D1=8F=D0=BB=20?= =?UTF-8?q?=D0=BA=D0=B0=D0=BA=20=20=D1=8D=D1=82=D0=BE=20=D1=81=D0=B4=D0=B5?= =?UTF-8?q?=D0=BB=D0=B0=D1=82=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/AC/index.js | 9 ++++++++- src/components/Article/index.js | 2 +- src/components/CommentForm/index.js | 9 +++++---- src/components/CommentList.js | 21 +++++++++++++-------- src/constants/index.js | 4 +++- src/reducer/articles.js | 8 +++++++- 6 files changed, 37 insertions(+), 16 deletions(-) diff --git a/src/AC/index.js b/src/AC/index.js index 4227ab3..cc26bd0 100644 --- a/src/AC/index.js +++ b/src/AC/index.js @@ -1,4 +1,4 @@ -import { INCREMENT, DELETE_ARTICLE, CHANGE_DATE_RANGE, CHANGE_SELECTION, ADD_NEW_COMMENT } from '../constants' +import { INCREMENT, DELETE_ARTICLE, CHANGE_DATE_RANGE, CHANGE_SELECTION, ADD_NEW_COMMENT, ADD_NEW_ID_COMMENT_TO_ARCTICLE } from '../constants' export function increment() { return { @@ -34,4 +34,11 @@ export function addNewComment(id, comment) { payload: { id, comment } } +} + +export function addIdNewCommentToArticle(idarcticle) { + return { + type: ADD_NEW_ID_COMMENT_TO_ARCTICLE, + payload: { idarcticle } + } } \ No newline at end of file diff --git a/src/components/Article/index.js b/src/components/Article/index.js index a83080d..463ae9e 100644 --- a/src/components/Article/index.js +++ b/src/components/Article/index.js @@ -35,7 +35,7 @@ class Article extends PureComponent { const body = isOpen && (
      {article.text}
      - +
      ) return ( diff --git a/src/components/CommentForm/index.js b/src/components/CommentForm/index.js index b574564..b765acc 100644 --- a/src/components/CommentForm/index.js +++ b/src/components/CommentForm/index.js @@ -2,7 +2,7 @@ import React, { Component } from 'react' import './style.css' import {connect} from 'react-redux' -import {addNewComment} from '../../AC' +import {addNewComment, addIdNewCommentToArticle} from '../../AC' @@ -35,12 +35,13 @@ class CommentForm extends Component { user: '', text: '' }) - const {addNewComment} = this.props // подключим нашу функцию из пропса конеекта + const {addNewComment, addIdNewCommentToArticle , idarticle } = this.props // подключим нашу функцию из пропса конеекта const id = "" // сделаем его пустым и подменим его в миделваре addNewComment(id, { user: this.state.user, text: this.state.text } ) - + // console.log("idarticle" + idarticle ) + addIdNewCommentToArticle( idarticle ) } @@ -71,4 +72,4 @@ const limits = { } // подключим к стору (редаксу) -export default connect(null, { addNewComment })(CommentForm) \ No newline at end of file +export default connect(null, { addNewComment , addIdNewCommentToArticle })(CommentForm) \ No newline at end of file diff --git a/src/components/CommentList.js b/src/components/CommentList.js index 8592d22..dad923f 100644 --- a/src/components/CommentList.js +++ b/src/components/CommentList.js @@ -13,30 +13,35 @@ class CommentList extends Component { } render() { - const {isOpen, toggleOpen} = this.props - const text = isOpen ? 'hide comments' : 'show comments' + const {isOpen, toggleOpen, idarticle } = this.props + const text = isOpen ? 'hide comments' : 'show comments' + return (
      - {this.getBody()} + {this.getBody(idarticle)}
      ) } - getBody() { + getBody(idarticle) { const {comments, isOpen} = this.props if (!isOpen) return null const body = comments.length ? (
        - {comments.map(id =>
      • )} + {comments.map(id =>
      • )}
      ) :

      No comments yet

      - return ( -
      + + + // console.log("idarticle------------" + idarticle) + return ( + +
      {body} - +
      ) } diff --git a/src/constants/index.js b/src/constants/index.js index 667c0bc..97770b2 100644 --- a/src/constants/index.js +++ b/src/constants/index.js @@ -7,4 +7,6 @@ export const CHANGE_DATE_RANGE = 'CHANGE_DATE_RANGE' -export const ADD_NEW_COMMENT = 'ADD_NEW_COMMENT' \ No newline at end of file +export const ADD_NEW_COMMENT = 'ADD_NEW_COMMENT' + +export const ADD_NEW_ID_COMMENT_TO_ARCTICLE = "ADD_NEW_ID_COMMENT_TO_ARCTICLE" // это врятли получится \ No newline at end of file diff --git a/src/reducer/articles.js b/src/reducer/articles.js index 94bc5f5..6921902 100644 --- a/src/reducer/articles.js +++ b/src/reducer/articles.js @@ -1,4 +1,4 @@ -import { DELETE_ARTICLE } from '../constants' +import { DELETE_ARTICLE, ADD_NEW_ID_COMMENT_TO_ARCTICLE } from '../constants' import { normalizedArticles as defaultArticles } from '../fixtures' @@ -20,9 +20,15 @@ export default (articlesState = articleMap, action) => { }, {}) return articles + case ADD_NEW_ID_COMMENT_TO_ARCTICLE: + console.log(" тут будет логика добавления ") + + return articlesState + + // return articlesState.filter(article => article.id !== payload.id) } From d10ad46945003470fec16c561a9adcdd19e737ca Mon Sep 17 00:00:00 2001 From: Sergey Breslavets Date: Sun, 4 Feb 2018 23:14:54 +0300 Subject: [PATCH 6/6] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=20=D0=B2=20=D1=81=D1=82=D0=B0=D1=82=D1=8C=D1=8E=20=D0=BD=D0=BE?= =?UTF-8?q?=20=D0=BD=D0=B5=D1=82=20=20=D0=B0=D0=B2=D1=82=D0=BE=D0=BC=D0=B0?= =?UTF-8?q?=D1=82=D0=B8=D1=87=D0=B5=D1=81=D0=BA=D0=BE=D0=B3=D0=BE=20=D0=BE?= =?UTF-8?q?=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B4?= =?UTF-8?q?=D0=BE=D0=BC=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/reducer/articles.js | 41 ++++++++++++++++++++++++++++++++++++++++- src/reducer/comments.js | 4 ++-- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/reducer/articles.js b/src/reducer/articles.js index 6921902..cf96da9 100644 --- a/src/reducer/articles.js +++ b/src/reducer/articles.js @@ -22,11 +22,50 @@ export default (articlesState = articleMap, action) => { return articles case ADD_NEW_ID_COMMENT_TO_ARCTICLE: console.log(" тут будет логика добавления ") + // console.log(" тут произошло обновление " + payload.idarcticle) + const articleid = payload.idarcticle; + // console.log(articlesState[id]) + // console.log(store) + // console.log(store.getState().comments) + // console.log( Object.key(store.getState().comments).reduce( ( obj ) ) ) + console.log(Object.values(store.getState().comments).length) + const l = Object.values(store.getState().comments).length; + + console.log(Object.values(store.getState().comments)[l - 1]) + + + const newIdComment = Object.values(store.getState().comments)[l - 1].id + console.log(newIdComment) + + + const art = Object.keys(articlesState).reduce((obj, key) => { + + + if (key == articleid) { + console.log(articlesState[key].comments) + articlesState[key].comments.push(newIdComment) + console.log(articlesState[key].comments) + return {...obj, [key]: articlesState[key] } + } + if (key !== articleid) return {...obj, [key]: articlesState[key] } + + }, {}) + + return art + + + + + + + // Object.values(store.getState().comments).forEach(element => { + // console.log(" zlo" + element) + // }); + - return articlesState // return articlesState.filter(article => article.id !== payload.id) diff --git a/src/reducer/comments.js b/src/reducer/comments.js index 0d9fcdb..dad2d8b 100644 --- a/src/reducer/comments.js +++ b/src/reducer/comments.js @@ -12,12 +12,12 @@ export default (state = commentsMap, action) => { switch (type) { case ADD_NEW_COMMENT: { - console.log("==new data" + action.payload.id) + // console.log("==new data" + action.payload.id) // return state return { ...state, - [action.payload.id]: { user: action.payload.comment.user, text: action.payload.comment.text } + [action.payload.id]: { id: action.payload.id, user: action.payload.comment.user, text: action.payload.comment.text } } }