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

Ht4 Smotrova Lilit #42

Open
wants to merge 5 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
1 change: 1 addition & 0 deletions 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.1",
"react": "^16.3.2",
"react-addons-css-transition-group": "^15.6.2",
Expand Down
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 @@ -25,6 +26,13 @@ export function changeDateRange(dateRange) {
}
}

export function addComment({ commentText, author, articleId }) {
return {
type: ADD_COMMENT,
payload: { commentText, author, articleId }
}
}

export function changeSelection(selected) {
return {
type: CHANGE_SELECTION,
Expand Down
5 changes: 3 additions & 2 deletions src/components/article/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class Article extends PureComponent {
}).isRequired,
isOpen: PropTypes.bool,
toggleOpen: PropTypes.func.isRequired,
deleteArticle: PropTypes.func
deleteArticle: PropTypes.func,
addComment: PropTypes.func
}

state = {
Expand Down Expand Up @@ -59,7 +60,7 @@ class Article extends PureComponent {
return (
<section className="test__article_body">
{article.text}
<CommentList comments={article.comments} />
<CommentList comments={article.comments} articleId={article.id} />
</section>
)
}
Expand Down
63 changes: 63 additions & 0 deletions src/components/comment-form/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React, { Component } from 'react'
import { connect } from 'react-redux'
import PropTypes from 'prop-types'
import { addComment } from '../../ac'

class CommentForm extends Component {
static propTypes = {
articleId: PropTypes.string.isRequired,
addComment: PropTypes.func.isRequired
}

state = {
commentText: '',
author: ''
}

handleSubmit = (e) => {
e.preventDefault()
const { author, commentText } = this.state
const { addComment, articleId } = this.props

addComment({ author, commentText, articleId })
}

handleChangeAuthor = (e) => {
this.setState({ author: e.target.value })
}

handleChangeCommentText = (e) => {
this.setState({ commentText: e.target.value })
}

render() {
const { author, commentText } = this.state

return (
<form onSubmit={this.handleSubmit} style={{ padding: '25px' }}>
<label>
<div>Author:</div>
<input
type="text"
onChange={this.handleChangeAuthor}
value={author}
/>
</label>
<label>
<div>Comment:</div>
<textarea
onChange={this.handleChangeCommentText}
value={commentText}
/>
</label>
<div>
<input type="submit" value="Add comment" />
</div>
</form>
)
}
}

export default connect(null, {
addComment
})(CommentForm)
7 changes: 5 additions & 2 deletions src/components/comment-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import CSSTransition from 'react-addons-css-transition-group'
import Comment from '../comment'
import toggleOpen from '../../decorators/toggleOpen'
import './style.css'
import CommentForm from '../comment-form'

class CommentList extends Component {
static defaultProps = {
Expand All @@ -14,7 +15,8 @@ class CommentList extends Component {
comments: PropTypes.array.isRequired,
//from toggleOpen decorator
isOpen: PropTypes.bool,
toggleOpen: PropTypes.func
toggleOpen: PropTypes.func,
articleId: PropTypes.string.isRequired
}

render() {
Expand All @@ -37,7 +39,7 @@ class CommentList extends Component {
}

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

return (
Expand All @@ -47,6 +49,7 @@ class CommentList extends Component {
) : (
<h3 className="test__comment-list--empty">No comments yet</h3>
)}
<CommentForm articleId={articleId} />
</div>
)
}
Expand Down
7 changes: 5 additions & 2 deletions src/components/filters/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@ import PropTypes from 'prop-types'
import Select from 'react-select'
import { connect } from 'react-redux'
import { changeSelection } from '../../ac'
import map from 'lodash/map'

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) => ({
const { articles } = this.props

return map(articles, (article) => ({
label: article.title,
value: article.id
}))
Expand Down
2 changes: 2 additions & 0 deletions src/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ export const DELETE_ARTICLE = 'DELETE_ARTICLE'

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

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

export default (store) => (next) => (action) => {
if (action.type !== ADD_COMMENT) {
Copy link
Owner

Choose a reason for hiding this comment

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

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

return next(action)
}

return next({
...action,
payload: {
...action.payload,
id: new Date().getTime()
}
})
}
24 changes: 22 additions & 2 deletions src/reducer/articles.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
import { DELETE_ARTICLE } from '../constants'
import { normalizedArticles as defaultArticles } from '../fixtures'
import { ADD_COMMENT, DELETE_ARTICLE } from '../constants'
import { normalizedArticles } from '../fixtures'

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

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

switch (type) {
case ADD_COMMENT:
const article = articleState[payload.articleId]

return {
...articleState,
[article.id]: {
...article,
comments: [...article.comments, payload.id]
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

}
}

case DELETE_ARTICLE:
return articleState.filter((article) => article.id !== payload.id)

Expand Down
14 changes: 12 additions & 2 deletions src/reducer/comments.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {} from '../constants'
import { normalizedComments } from '../fixtures'
import { ADD_COMMENT } from '../constants'

const defaultComments = normalizedComments.reduce(
(acc, comment) => ({
Expand All @@ -10,9 +10,19 @@ const defaultComments = normalizedComments.reduce(
)

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

switch (type) {
case ADD_COMMENT:
return {
...commentsState,
[payload.id]: {
id: payload.id,
user: payload.author,
text: payload.commentText
}
}

default:
return commentsState
}
Expand Down
3 changes: 2 additions & 1 deletion src/selectors/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createSelector } from 'reselect'
import filter from 'lodash/filter'

const idSelector = (_, props) => props.id
const articlesSelector = (state) => state.articles
Expand All @@ -15,7 +16,7 @@ export const filtratedArticlesSelector = createSelector(
} = filters
console.log('---', 'calculating filters')

return articles.filter((article) => {
return filter(articles, (article) => {
const published = Date.parse(article.date)
return (
(!selected.length ||
Expand Down
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 reducer from '../reducer'
import logger from '../middlewares/logger'
import uniqueCommentId from '../middlewares/unique-comment-id'

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

const enhancer = composeEnhancers(applyMiddleware(logger))
const enhancer = composeEnhancers(applyMiddleware(logger, uniqueCommentId))

const store = createStore(reducer, enhancer)

Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4891,7 +4891,7 @@ lodash.uniq@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773"

"lodash@>=3.5 <5", lodash@^4.13.1, lodash@^4.14.0, lodash@^4.15.0, lodash@^4.17.2, lodash@^4.17.3, lodash@^4.17.4, lodash@^4.17.5, lodash@^4.2.0, lodash@^4.3.0:
"lodash@>=3.5 <5", lodash@^4.13.1, lodash@^4.14.0, lodash@^4.15.0, lodash@^4.17.10, lodash@^4.17.2, lodash@^4.17.3, lodash@^4.17.4, lodash@^4.17.5, lodash@^4.2.0, lodash@^4.3.0:
version "4.17.10"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.10.tgz#1b7793cf7259ea38fb3661d4d38b3260af8ae4e7"

Expand Down