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

HT5 #35

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

HT5 #35

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
13 changes: 12 additions & 1 deletion src/AC/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {DELETE_ARTICLE, INCREMENT, CHANGE_DATE_RANGE, CHANGE_SELECTION} from '../constants'
import {DELETE_ARTICLE, INCREMENT, CHANGE_DATE_RANGE, CHANGE_SELECTION, ADD_COMMENT} from '../constants'

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

export function addComment(user, text, parentArticleId) {
return {
type: ADD_COMMENT,
payload: {
user,
text,
parentArticleId
}
}
}
2 changes: 1 addition & 1 deletion src/components/Article/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class Article extends PureComponent {
<section>
{article.text}
<button onClick = {() => this.setState({updateIndex: this.state.updateIndex + 1})}>update</button>
<CommentList comments = {article.comments} ref = {this.setCommentsRef} key = {this.state.updateIndex}/>
<CommentList comments = {article.comments} articleId = {article.id} ref = {this.setCommentsRef} key = {this.state.updateIndex}/>
</section>
)
}
Expand Down
11 changes: 6 additions & 5 deletions src/components/ArticleList.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {filtratedArticlesSelector} from '../selectors'
class ArticleList extends Component {
static propTypes = {
//from connect
articles: PropTypes.array.isRequired,
articles: PropTypes.object.isRequired,
//from accordion
openItemId: PropTypes.string,
toggleOpenItem: PropTypes.func.isRequired
Expand All @@ -17,11 +17,12 @@ class ArticleList extends Component {
render() {
console.log('---', 'update article list')
const { articles, openItemId, toggleOpenItem } = this.props
const articleElements = articles.map(article => <li key={article.id}>
const articleElements = Object.keys(articles).map(key =>
<li key={key}>
<Article
article = {article}
isOpen = {article.id === openItemId}
toggleOpen = {toggleOpenItem(article.id)}
article = {articles[key]}
isOpen = {key === openItemId}
toggleOpen = {toggleOpenItem(key)}
/>
</li>)

Expand Down
9 changes: 7 additions & 2 deletions src/components/CommentForm/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import './style.css'
import {connect} from 'react-redux'
import {addComment} from '../../AC'

class CommentForm extends Component {
static propTypes = {

articleId: PropTypes.string,
// from connect
addComment: PropTypes.func
};

state = {
Expand All @@ -28,6 +32,7 @@ class CommentForm extends Component {

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

export default CommentForm
export default connect(null, { addComment })(CommentForm)
9 changes: 5 additions & 4 deletions src/components/CommentList.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@ import Comment from './Comment'
import CommentForm from './CommentForm'
import toggleOpen from '../decorators/toggleOpen'

function CommentList({comments = [], isOpen, toggleOpen}) {
function CommentList({comments = [], isOpen, toggleOpen, articleId}) {
const text = isOpen ? 'hide comments' : 'show comments'
return (
<div>
<button onClick={toggleOpen}>{text}</button>
{getBody({comments, isOpen})}
{getBody({comments, isOpen, articleId})}
</div>
)
}

CommentList.propTypes = {
comments: PropTypes.array,
articleId: PropTypes.string,
//from toggleOpen decorator
isOpen: PropTypes.bool,
toggleOpen: PropTypes.func
}

function getBody({comments, isOpen}) {
function getBody({comments, isOpen, articleId}) {
if (!isOpen) return null
if (!comments.length) return (
<div>
Expand All @@ -35,7 +36,7 @@ function getBody({comments, isOpen}) {
<ul>
{comments.map(id => <li key={id}><Comment id = {id}/></li>)}
</ul>
<CommentForm/>
<CommentForm articleId = {articleId}/>
</div>
)
}
Expand Down
14 changes: 9 additions & 5 deletions src/components/Filters/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@ 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).forEach(key => {
options.push({
label: articles[key].title,
value: key
})
})

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

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

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

export default store => next => action => {
const { type, payload } = action
const newAction = Object.assign({}, action)

if (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.

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

newAction.payload.id = getId()
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(newAction)
}

function getId(min = 1000, max = 1000000) {
const id = '' + (Math.floor(Math.random() * (max - min)) + min)

if(isFree(id)){
return id
}
return getId()
}

function isFree(id) {
return true
}
18 changes: 15 additions & 3 deletions src/reducer/articles.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
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
return acc
}, {})

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

articleState = Object.assign({}, articleState)

switch (type) {
case DELETE_ARTICLE: return articleState.filter(article => article.id !== payload.id)
case DELETE_ARTICLE:
delete articleState[payload.id]
return articleState
case ADD_COMMENT:
articleState[payload.parentArticleId].comments.push(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.

вот здесь ты, на самом деле, мутируешь стейт. Все просто меняется по ссылке

return articleState
}

return articleState
Expand Down
6 changes: 5 additions & 1 deletion src/reducer/comments.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {normalizedComments as defaulComments} from '../fixtures'
import {} from '../constants'
import {ADD_COMMENT} from '../constants'

const commentsMap = defaulComments.reduce((acc, comment) => {
acc[comment.id] = comment
Expand All @@ -10,6 +10,10 @@ export default (commentsState = commentsMap, action) => {
const {type, payload} = action

switch (type) {
case ADD_COMMENT:
const newState = Object.assign({}, commentsState)
newState[payload.id] = payload
return newState
}

return commentsState
Expand Down
14 changes: 10 additions & 4 deletions src/selectors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ export const filtratedArticlesSelector = createSelector(articlesGetter, filtersG
const {selected, dateRange: {from, to}} = filters
console.log('---', 'recomputing filtration')

return articles.filter(article => {
const published = Date.parse(article.date)
return (!selected.length || selected.includes(article.id)) &&
(!from || !to || (published > from && published < to))
const filtered = {}

Object.keys(articles).forEach((key) => {
const published = Date.parse(articles[key].date)

if ((!selected.length || selected.includes(key)) &&
(!from || !to || (published > from && published < to))) {
filtered[key] = articles[key]
}
})
return filtered
})

export const commentSelectorFactory = () => createSelector(commentsGetter, idGetter, (comments, id) => {
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/idGenerator'

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

const store = createStore(reducer, {}, enhancer)

Expand Down