Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/AC/index.js
Original file line number Diff line number Diff line change
@@ -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_COMMENT} from '../constants'

export function increment() {
return {
Expand Down Expand Up @@ -26,3 +26,10 @@ export function changeSelection(selected) {
payload: { selected }
}
}

export function addComment(articleId, comment) {
return {
type: ADD_COMMENT,
payload: { articleId, comment }
}
}
15 changes: 12 additions & 3 deletions src/components/Article/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -29,11 +30,11 @@ class Article extends PureComponent {

render() {
console.log('---', 'rerendering')
const {article, isOpen, toggleOpen} = this.props
const {article, isOpen, toggleOpen, id} = this.props
const body = isOpen && (
<div>
<section>{article.text}</section>
<CommentList comments = {article.comments} ref = {this.setCommentsRef} key = {this.state.count}/>
<CommentList articleId = {id} comments = {article.comments} ref = {this.setCommentsRef} key = {this.state.count}/>
</div>
)
return (
Expand Down Expand Up @@ -85,5 +86,13 @@ class Article extends PureComponent {

}

const createMapStateToProps = () => {
const articleSelector = createArticleSelector()
return (state, ownProps) => {
return {
article: articleSelector(state, ownProps)
}
}
}

export default connect(null, { deleteArticle })(Article)
export default connect(createMapStateToProps, { deleteArticle })(Article)
8 changes: 4 additions & 4 deletions src/components/ArticleList.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ class ArticleList extends Accordion {
console.log('---', 'rerendering article list')
const {articles} = this.props
if (!articles.length) return <h3>No Articles</h3>
const articleElements = articles.map((article) => <li key={article.id}>
<Article article={article}
isOpen={article.id === this.state.openItemId}
toggleOpen={this.toggleOpenItemMemoized(article.id)}
const articleElements = articles.map(id => <li key={id}>
<Article id={id}
isOpen={id === this.state.openItemId}
toggleOpen={this.toggleOpenItemMemoized(id)}
/>
</li>)
return (
Expand Down
9 changes: 8 additions & 1 deletion src/components/CommentForm/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import React, { Component } from 'react'
import {connect} from 'react-redux'
import { addComment } from '../../AC'
import './style.css'

class CommentForm extends Component {
Expand Down Expand Up @@ -26,6 +28,11 @@ class CommentForm extends Component {

handleSubmit = ev => {
ev.preventDefault()
const {user, text} = this.state
this.props.addComment(this.props.articleId, {
user,
text
})
this.setState({
user: '',
text: ''
Expand Down Expand Up @@ -58,4 +65,4 @@ const limits = {
}
}

export default CommentForm
export default connect(null, { addComment })(CommentForm)
4 changes: 2 additions & 2 deletions src/components/CommentList.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class CommentList extends Component {
}

getBody() {
const {comments, isOpen} = this.props
const {comments, isOpen, articleId} = this.props
if (!isOpen) return null

const body = comments.length ? (
Expand All @@ -36,7 +36,7 @@ class CommentList extends Component {
return (
<div>
{body}
<CommentForm />
<CommentForm articleId={articleId} />
</div>
)
}
Expand Down
8 changes: 4 additions & 4 deletions src/components/Filters/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ 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
const options = Object.keys(articles).map(i => ({
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лучше селектор, который достанет из в виде массива

label: articles[i].title,
value: articles[i].id
}))

return <Select
Expand Down
4 changes: 3 additions & 1 deletion src/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ export const INCREMENT = 'INCREMENT'
export const DELETE_ARTICLE = 'DELETE_ARTICLE'

export const CHANGE_SELECTION = 'CHANGE_SELECTION'
export const CHANGE_DATE_RANGE = 'CHANGE_DATE_RANGE'
export const CHANGE_DATE_RANGE = 'CHANGE_DATE_RANGE'

export const ADD_COMMENT = 'ADD_COMMENT'
8 changes: 8 additions & 0 deletions src/middlewares/idGenerator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ADD_COMMENT } from "../constants";

export default store => next => action => {
const { payload } = action
next({...action, payload: {...payload, id: idGenerator()}})
}

const idGenerator = () => Math.random().toString(36).slice(2)
32 changes: 29 additions & 3 deletions src/reducer/articles.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,38 @@
import { DELETE_ARTICLE } from '../constants'
import { DELETE_ARTICLE, ADD_COMMENT } from '../constants'
import {normalizedArticles as defaultArticles} from '../fixtures'

export default (articlesState = defaultArticles, action) => {
const articlesMap = defaultArticles.reduce((acc, article) => ({
...acc,
[article.id]: article
}), {})

const deletePropFromObject = (object, property) => {
const obj = {...object}
delete obj[property]
return obj
}

const getArticlesWithNewComment = (articles, articleId, commentId) => {
const article = articles[articleId]
const {comments} = article
return {
...articles,
[articleId]: {
...article,
comments: [...comments, commentId]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comments может быть undefined

}
}
}

export default (articlesState = articlesMap, action) => {
const { type, payload } = action

switch (type) {
case DELETE_ARTICLE:
return articlesState.filter(article => article.id !== payload.id)
return deletePropFromObject(articlesState, payload.id)

case ADD_COMMENT:
return getArticlesWithNewComment(articlesState, payload.articleId, payload.id)
}

return articlesState
Expand Down
9 changes: 5 additions & 4 deletions src/reducer/comments.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { } from '../constants'
import { ADD_COMMENT } from '../constants'
import {normalizedComments as defaultComments} from '../fixtures'

const commentsMap = defaultComments.reduce((acc, comment) => ({
Expand All @@ -7,10 +7,11 @@ const commentsMap = defaultComments.reduce((acc, comment) => ({
}), {})

export default (state = commentsMap, action) => {
const { type } = action

switch (type) {
const { type, payload } = action

if (type === ADD_COMMENT) {
const {id, comment} = payload
return {...state, [id]: {...comment, id}}
}

return state
Expand Down
12 changes: 7 additions & 5 deletions src/selectors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ export const commentsSelector = state => state.comments
export const idSelector = (_, props) => props.id

export const createCommentSelector = () => createSelector(commentsSelector, idSelector, (comments, id) => {
console.log('---', 'searching for comment', id)
return comments[id]
})

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

export const filtratedArticlesSelector = createSelector(articlesSelector, filtersSelector, (articles, filters) => {
console.log('---', 'computing filters')
const {selected, dateRange: {from, to}} = filters

return articles.filter(article => {
const published = Date.parse(article.date)
return (!selected.length || selected.includes(article.id)) &&
return Object.keys(articles).filter(i => {
const published = Date.parse(articles[i].date)
return (!selected.length || selected.includes(articles[i].id)) &&
(!from || !to || (published > from && published < to))
})
})
3 changes: 2 additions & 1 deletion src/store/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {createStore, applyMiddleware, compose} from 'redux'
import rootReducer from '../reducer'
import logger from '../middlewares/logger'
import idGenerator from '../middlewares/idGenerator'

const composeEnhancers =
typeof window === 'object' &&
Expand All @@ -10,7 +11,7 @@ const composeEnhancers =
}) : compose

const enhancer = composeEnhancers(
applyMiddleware(logger)
applyMiddleware(logger, idGenerator)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лучше логгер в конце ставить

)
const store = createStore(rootReducer, enhancer)

Expand Down