Skip to content

Commit

Permalink
Fix adding of comments. Add lodash
Browse files Browse the repository at this point in the history
  • Loading branch information
irrbd committed Oct 28, 2018
1 parent 2333f55 commit b9da052
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 36 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"lodash": "^4.17.10",
"prop-types": "^15.6.2",
"react": "^16.5.2",
"react-addons-css-transition-group": "^15.6.2",
Expand All @@ -12,7 +13,8 @@
"react-scripts": "2.0.4",
"react-select": "^2.1.0",
"redux": "^4.0.1",
"reselect": "^4.0.0"
"reselect": "^4.0.0",
"uuid": "^3.3.2"
},
"scripts": {
"start": "react-scripts start",
Expand Down
8 changes: 4 additions & 4 deletions src/ac/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ export function increment() {
}
}

export function deleteArticle(id) {
export function deleteArticle(articleId) {
return {
type: DELETE_ARTICLE,
payload: { id }
payload: { articleId }
}
}

Expand All @@ -41,9 +41,9 @@ export function addCommentToList(payload) {
}
}

export function addCommentToArticle(id) {
export function addCommentToArticle(payload) {
return {
type: ADD_COMMENT_TO_ARTICLE,
payload: { id }
payload
}
}
11 changes: 1 addition & 10 deletions src/components/article/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import CSSTransition from 'react-addons-css-transition-group'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import CommentList from '../comment-list'
import { createArticleSelector } from '../../selectors'
import { deleteArticle } from '../../ac'
import './style.css'

Expand Down Expand Up @@ -70,15 +69,7 @@ class Article extends PureComponent {
}
}

const createMapStateToProps = () => {
const articleSelector = createArticleSelector()

return (state, ownProps) => ({
articles: articleSelector(state, ownProps)
})
}

export default connect(
createMapStateToProps,
null,
{ deleteArticle }
)(Article)
14 changes: 8 additions & 6 deletions src/components/comment-list/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import uuidv4 from 'uuid/v4'

import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
Expand All @@ -16,7 +18,6 @@ class CommentList extends Component {
}

state = {
id: 'ratatata',
comment: null,
user: null
}
Expand Down Expand Up @@ -47,11 +48,11 @@ class CommentList extends Component {
}

getBody() {
const { comments = [], isOpen } = this.props
const { id, text, user } = this.state
const { articleId, comments = [], isOpen } = this.props
const { text, user } = this.state
if (!isOpen) return null

// TODO Генерировать id
// TODO Вынести форму добавления комментария в отдельную компоненту
return (
<div className="test--comment-list__body">
{comments.length ? (
Expand All @@ -72,8 +73,9 @@ class CommentList extends Component {
/>
<button
onClick={() => {
this.props.addCommentToList({ id, text, user })
this.props.addCommentToArticle({ commentId: id })
const commentId = uuidv4()
this.props.addCommentToList({ commentId, text, user })
this.props.addCommentToArticle({ articleId, commentId })
}}
>
Добавить комментарий
Expand Down
12 changes: 7 additions & 5 deletions src/reducer/articles.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import omit from 'lodash/omit'

import { normalizedArticles } from '../fixtures'
import { ADD_COMMENT_TO_ARTICLE, DELETE_ARTICLE } from '../constants'

Expand All @@ -10,20 +12,20 @@ const defaultArticles = normalizedArticles.reduce(
)

export default (articlesState = defaultArticles, action) => {
const { type, payload: { id, commentId } = {} } = action
const { type, payload: { articleId, commentId } = {} } = action

switch (type) {
case ADD_COMMENT_TO_ARTICLE:
return {
...articlesState,
[id]: {
...articlesState.id,
comments: [...articlesState.comments, commentId]
[articleId]: {
...articlesState[articleId],
comments: [...articlesState[articleId].comments, commentId]
}
}

case DELETE_ARTICLE:
return articlesState.filter((article) => article.id !== id)
return omit(articlesState, [articleId])

default:
return articlesState
Expand Down
6 changes: 4 additions & 2 deletions src/reducer/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ const defaultComments = normalizedComments.reduce(
)

export default (state = defaultComments, action) => {
const { type, payload: { id, text, user } = {} } = action
const { type, payload: { commentId, text, user } = {} } = action

switch (type) {
case ADD_COMMENT_TO_LIST:
return { ...state, [id]: { text, user } }
console.log({ ...state, [commentId]: { text, user } }, 'comments')
return { ...state, [commentId]: { text, user } }

default:
return state
}
Expand Down
10 changes: 2 additions & 8 deletions src/selectors/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import { createSelector } from 'reselect'
import filter from 'lodash/filter'

export const selectionSelector = (state) => state.filters.selected
export const dateRangeSelector = (state) => state.filters.dateRange
export const articlesSelector = (state) => state.articles
export const commentsSelector = (state) => state.comments
export const idSelector = (_, props) => props.id

export const createArticleSelector = () =>
createSelector(articlesSelector, idSelector, (articles, id) => {
console.log(articles, 'articles')
console.log(id, 'id')
return articles[id]
})

export const filtratedArticlesSelector = createSelector(
selectionSelector,
dateRangeSelector,
Expand All @@ -21,7 +15,7 @@ export const filtratedArticlesSelector = createSelector(
console.log('---', 'article list selector')
const { from, to } = dateRange

return articles.filter((article) => {
return filter(articles, (article) => {
const published = Date.parse(article.date)
return (
(!selected.length ||
Expand Down

0 comments on commit b9da052

Please sign in to comment.