Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HT-4 Anton Sitnikov #42

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
10 changes: 9 additions & 1 deletion src/ac/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import {
INCREMENT,
DELETE_ARTICLE,
CHANGE_DATE_RANGE,
CHANGE_SELECTION
CHANGE_SELECTION,
ADD_COMMENT
} from '../constants'

export function increment() {
Expand All @@ -18,6 +19,13 @@ export function deleteArticle(id) {
}
}

export function addComment(comment) {
return {
type: ADD_COMMENT,
payload: comment
}
}

export function changeDateRange(dateRange) {
return {
type: CHANGE_DATE_RANGE,
Expand Down
9 changes: 5 additions & 4 deletions src/components/article-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ export class ArticleList extends Component {

get items() {
const { articles, openItemId, toggleOpenItem } = this.props
return articles.map((article) => (
<li key={article.id} className="test--article-list__item">
console.log(Object.keys(articles))
return articles.map((articleId) => (
<li key={articleId} className="test--article-list__item">
<Article
article={article}
isOpen={openItemId === article.id}
id={articleId}
isOpen={openItemId === articleId}
toggleOpen={toggleOpenItem}
/>
</li>
Expand Down
9 changes: 7 additions & 2 deletions src/components/article/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import './style.css'

class Article extends PureComponent {
static propTypes = {
id: PropTypes.string,
article: PropTypes.shape({
title: PropTypes.string.isRequired,
text: PropTypes.string
Expand Down Expand Up @@ -63,13 +64,17 @@ class Article extends PureComponent {
return (
<section className="test--article__body">
{article.text}
<CommentList comments={article.comments} />
<CommentList articleId={article.id} />
</section>
)
}
}

const createMapStateToProps = (state, ownProps) => {
return { article: state.articles[ownProps.id] }
}

export default connect(
null,
createMapStateToProps,
{ deleteArticle }
)(Article)
40 changes: 40 additions & 0 deletions src/components/comment-form.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React, { Component } from 'react'
import PropTypes from 'prop-types'

class CommentForm extends Component {
static propTypes = {
addComment: PropTypes.func
}

state = { text: '', user: 'Jane Dow' }

handleSubmit = (e) => {
e.preventDefault()
const { addComment } = this.props
addComment({ text: this.state.text, user: this.state.user })
}

handleTextChange = (e) => {
this.setState({ text: e.target.value })
}

handleUserChange = (e) => {
this.setState({ user: e.target.value })
}

render() {
return (
<form onSubmit={this.handleSubmit}>
<input
type="text"
value={this.state.user}
onChange={this.handleUserChange}
/>
<textarea value={this.state.text} onChange={this.handleTextChange} />
<input type="submit" value="Submit" />
</form>
)
}
}

export default CommentForm
15 changes: 14 additions & 1 deletion src/components/comment-list/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import CSSTransition from 'react-addons-css-transition-group'
import { connect } from 'react-redux'
import { addComment } from '../../ac'
import Comment from '../comment'
import CommentForm from '../comment-form'
import toggleOpen from '../../decorators/toggleOpen'
import './style.css'

Expand All @@ -18,6 +21,10 @@ class CommentList extends Component {
comments: []
}
*/
handleAddComment = (comment) => {
const { addComment, articleId } = this.props
addComment({ articleId, ...comment })
}

render() {
const { isOpen, toggleOpen } = this.props
Expand Down Expand Up @@ -49,6 +56,7 @@ class CommentList extends Component {
) : (
<h3 className="test--comment-list__empty">No comments yet</h3>
)}
<CommentForm addComment={this.handleAddComment} />
</div>
)
}
Expand All @@ -66,4 +74,9 @@ class CommentList extends Component {
}
}

export default toggleOpen(CommentList)
export default connect(
(state, ownProps) => ({
comments: state.articles[ownProps.articleId].comments
}),
{ addComment }
)(toggleOpen(CommentList))
2 changes: 1 addition & 1 deletion src/components/filters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Filters extends Component {
render() {
return (
<div>
<SelectFilter articles={this.props.articles} />
<SelectFilter />
<DateRange />
</div>
)
Expand Down
10 changes: 6 additions & 4 deletions src/components/filters/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ import { changeSelection } from '../../ac'

class SelectFilter extends Component {
static propTypes = {
articles: PropTypes.array.isRequired
articles: PropTypes.object.isRequired
}

handleChange = (selected) => {
this.props.changeSelection(selected)
}

get options() {
return this.props.articles.map((article) => ({
label: article.title,
value: article.id
const { articles } = this.props

return Object.keys(articles).map((articleId) => ({
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[articleId].title,
value: articleId
}))
}

Expand Down
2 changes: 2 additions & 0 deletions src/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ export const INCREMENT = 'INCREMENT'

export const DELETE_ARTICLE = 'DELETE_ARTICLE'

export const ADD_COMMENT = 'ADD_COMMENT'

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

export default (store) => (next) => (action) => {
if (action.type === ADD_COMMENT) action.payload.id = '' + randomizer(1, 10000)
Copy link
Owner

Choose a reason for hiding this comment

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

через мидлвары будет проходить каждый экшин, они должны быть максимально общими, завязывать на конкретные экшины - не лучшее решение. И лучше не мутировать payload, мало-ли что там станут передавать

console.log('---', 'addRandomId: ', action)
next(action)
}

function randomizer(min, max) {
let rand = min + Math.random() * (max + 1 - min)
return Math.floor(rand)
}
37 changes: 33 additions & 4 deletions src/reducer/articles.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,41 @@
import { normalizedArticles as defaultArticles } from '../fixtures'
import { DELETE_ARTICLE } from '../constants'
import { normalizedArticles } from '../fixtures'
import { DELETE_ARTICLE, ADD_COMMENT } from '../constants'

const defaultArticles = normalizedArticles.reduce(
(acc, article) => ({
...acc,
[article.id]: article
}),
{}
)

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

console.log('article', type)
switch (type) {
case ADD_COMMENT:
return Object.keys(articlesState).reduce((acc, articleId) => {
if (articleId === payload.articleId) {
const comments = articlesState[articleId].comments || []
acc[articleId] = {
...articlesState[articleId],
comments: [...comments, payload.id]
}
} else {
acc[articleId] = articlesState[articleId]
}

return acc
}, {})

case DELETE_ARTICLE:
return articlesState.filter((article) => article.id !== payload.id)
return Object.keys(articlesState).reduce((acc, articleId) => {
if (articleId !== payload.id) {
acc[articleId] = articlesState[articleId]
}

return acc
}, {})

default:
return articlesState
Expand Down
15 changes: 13 additions & 2 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 } from '../fixtures'

const defaultComments = normalizedComments.reduce(
Expand All @@ -11,8 +11,19 @@ const defaultComments = normalizedComments.reduce(

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

switch (type) {
case ADD_COMMENT:
console.log('addComment', action)
console.log('commentsList', state)
const { payload } = action
const newComment = {}
newComment[payload.id] = {
id: payload.id,
text: payload.text,
user: payload.user
}
return { ...state, ...newComment }

default:
return state
}
Expand Down
7 changes: 4 additions & 3 deletions src/selectors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const selectionSelector = (state) => state.filters.selected
export const dateRangeSelector = (state) => state.filters.dateRange
export const articleListSelector = (state) => state.articles
export const commentsSelector = (state) => state.comments
export const articlesSelector = (state) => state.articles
export const idSelector = (_, props) => props.id

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

return articles.filter((article) => {
const published = Date.parse(article.date)
return Object.keys(articles).filter((articleId) => {
const published = Date.parse(articles[articleId].date)
return (
(!selected.length ||
selected.find((selected) => selected.value === article.id)) &&
selected.find((selected) => selected.value === articleId)) &&
(!from || !to || (published > from && published < to))
)
})
Expand Down
3 changes: 2 additions & 1 deletion src/store/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { createStore, applyMiddleware } from 'redux'
import reducer from '../reducer'
import logger from '../middlewares/logger'
import generateId from '../middlewares/generateId'

const enhancer = applyMiddleware(logger)
const enhancer = applyMiddleware(logger, generateId)

const store = createStore(reducer, enhancer)

Expand Down
Loading