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 #38

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Ht4 #38

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
6,931 changes: 3,468 additions & 3,463 deletions package-lock.json

Large diffs are not rendered by default.

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": {
"chance": "^1.0.16",
"prop-types": "^15.6.2",
"react": "^16.5.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,
CREATE_COMMENT_TO_ARTICLE
} from '../constants'

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

export function createCommentToArticle(articleId, user, text) {
return {
type: CREATE_COMMENT_TO_ARTICLE,
payload: { articleId, user, text }
}
}
4 changes: 2 additions & 2 deletions src/components/article-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { filtratedArticlesSelector } from '../selectors'

export class ArticleList extends Component {
static propTypes = {
articles: PropTypes.array.isRequired,
articles: PropTypes.object.isRequired,
fetchData: PropTypes.func,

//from accordion decorator
Expand All @@ -22,7 +22,7 @@ export class ArticleList extends Component {

get items() {
const { articles, openItemId, toggleOpenItem } = this.props
return articles.map((article) => (
return Object.values(articles).map((article) => (
Copy link
Owner

Choose a reason for hiding this comment

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

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

<li key={article.id} className="test--article-list__item">
<Article
article={article}
Expand Down
18 changes: 14 additions & 4 deletions src/components/article/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import CSSTransition from 'react-addons-css-transition-group'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import CommentList from '../comment-list'
import { deleteArticle } from '../../ac'
import { deleteArticle, createCommentToArticle } from '../../ac'
import './style.css'

class Article extends PureComponent {
static propTypes = {
article: PropTypes.shape({
id: PropTypes.string.isRequired,
title: PropTypes.string.isRequired,
text: PropTypes.string
text: PropTypes.string,
comments: PropTypes.array
}).isRequired,
isOpen: PropTypes.bool,
toggleOpen: PropTypes.func.isRequired
Expand Down Expand Up @@ -55,6 +57,11 @@ class Article extends PureComponent {

handleClick = () => this.props.toggleOpen(this.props.article.id)

handleCreateComment = (user, text) => {
const { createCommentToArticle, article } = this.props
createCommentToArticle(article.id, user, text)
}

get body() {
const { isOpen, article } = this.props
if (!isOpen) return null
Expand All @@ -63,13 +70,16 @@ class Article extends PureComponent {
return (
<section className="test--article__body">
{article.text}
<CommentList comments={article.comments} />
<CommentList
comments={article.comments}
onCreateComment={this.handleCreateComment}
/>
</section>
)
}
}

export default connect(
null,
{ deleteArticle }
{ deleteArticle, createCommentToArticle }
)(Article)
8 changes: 8 additions & 0 deletions src/components/comment-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import React, { Component } from 'react'
import PropTypes from 'prop-types'
import CSSTransition from 'react-addons-css-transition-group'
import Comment from '../comment'
import NewCommentForm from '../new-comment-form/index'
import toggleOpen from '../../decorators/toggleOpen'
import './style.css'

class CommentList extends Component {
static propTypes = {
comments: PropTypes.array,
onCreateComment: PropTypes.func,
//from toggleOpen decorator
isOpen: PropTypes.bool,
toggleOpen: PropTypes.func
Expand Down Expand Up @@ -49,6 +51,7 @@ class CommentList extends Component {
) : (
<h3 className="test--comment-list__empty">No comments yet</h3>
)}
<NewCommentForm onSendComment={this.handleSendComment} />
</div>
)
}
Expand All @@ -64,6 +67,11 @@ class CommentList extends Component {
</ul>
)
}

handleSendComment = (user, text) => {
const { onCreateComment } = this.props
onCreateComment && onCreateComment(user, text)
}
}

export default toggleOpen(CommentList)
5 changes: 3 additions & 2 deletions src/components/filters/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ 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) => ({
const { articles } = this.props
return Object.values(articles).map((article) => ({
label: article.title,
value: article.id
}))
Expand Down
56 changes: 56 additions & 0 deletions src/components/new-comment-form/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import './style.css'

class NewCommentForm extends PureComponent {
static propTypes = {
onSendComment: PropTypes.func
}

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

render() {
return (
<div className="new-comment-form__container">
<form onSubmit={this.handleSubmit}>
<div className="new-comment-form__user">
<label>Your name:</label>
<input
type="text"
value={this.state.user}
name="user"
onChange={this.handleChange}
/>
</div>
<div className="new-comment-form__text">
<textarea
value={this.state.text}
name="text"
onChange={this.handleChange}
/>
</div>
<div className="new-comment-form__button">
<button type="submit">Send</button>
</div>
</form>
</div>
)
}

handleChange = (ev) => {
this.setState({ [ev.target.name]: ev.target.value })
}

handleSubmit = (ev) => {
ev.preventDefault()

const { onSendComment } = this.props

onSendComment && onSendComment(this.state.user, this.state.text)
}
}

export default NewCommentForm
24 changes: 24 additions & 0 deletions src/components/new-comment-form/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
.new-comment-form__container {
border: 1px solid gray;
margin-top: 15px;
padding: 20px 15px;
width: 500px;
}

.new-comment-form__user {
margin-bottom: 10px;
}

.new-comment-form__user label {
margin-right: 10px;
}

.new-comment-form__text textarea {
width: 100%;
height: 50px;
}

.new-comment-form__button {
margin-top: 10px;
text-align: right;
}
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 CREATE_COMMENT_TO_ARTICLE = 'CREATE_COMMENT_TO_ARTICLE'
11 changes: 11 additions & 0 deletions src/middlewares/id-generator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Chance from 'chance'
import { CREATE_COMMENT_TO_ARTICLE } from '../constants'

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

Choose a reason for hiding this comment

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

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

const chance = new Chance()
action.payload.id = chance.guid()
Copy link
Owner

Choose a reason for hiding this comment

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

лучше не мутировать payload, мало-ли что там станут передавать

}

next(action)
}
38 changes: 35 additions & 3 deletions src/reducer/articles.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,44 @@
import { normalizedArticles as defaultArticles } from '../fixtures'
import { DELETE_ARTICLE } from '../constants'
import { normalizedArticles } from '../fixtures'
import { CREATE_COMMENT_TO_ARTICLE, DELETE_ARTICLE } from '../constants'

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

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

switch (type) {
case DELETE_ARTICLE:
return articlesState.filter((article) => article.id !== payload.id)
const newArticleState = {}

for (let id in articlesState) {
if (id === payload.id) {
continue
}

newArticleState[id] = articlesState[id]
}

return newArticleState

case CREATE_COMMENT_TO_ARTICLE:
const { id, articleId } = payload
const commentedArticle = articlesState[articleId]

return {
...articlesState,
[articleId]: {
...commentedArticle,
comments: commentedArticle.comments
? [...commentedArticle.comments, id]
: [id]
}
}

default:
return articlesState
Expand Down
11 changes: 10 additions & 1 deletion src/reducer/comments.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {} from '../constants'
import { normalizedComments } from '../fixtures'
import { CREATE_COMMENT_TO_ARTICLE } from '../constants'

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

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

switch (type) {
case CREATE_COMMENT_TO_ARTICLE:
const { id, user, text } = payload
return {
...state,
[id]: { id, user, text }
}
break

default:
return state
}
Expand Down
18 changes: 12 additions & 6 deletions src/selectors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@ export const filtratedArticlesSelector = createSelector(
console.log('---', 'article list selector')
const { from, to } = dateRange

return articles.filter((article) => {
const published = Date.parse(article.date)
return (
const outputArticles = {}

for (let id in articles) {
const published = Date.parse(articles[id].date)

if (
(!selected.length ||
selected.find((selected) => selected.value === article.id)) &&
selected.find((selected) => selected.value === id)) &&
(!from || !to || (published > from && published < to))
)
})
) {
outputArticles[id] = articles[id]
}
}
return outputArticles
}
)

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 idGenerator from '../middlewares/id-generator'

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

const store = createStore(reducer, enhancer)

Expand Down