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
8,876 changes: 8,876 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

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(comment) {
return {
type: ADD_COMMENT,
payload: { comment }
}
}
19 changes: 14 additions & 5 deletions src/components/Article/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ 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 {
static propTypes = {
article: PropTypes.shape({
/* article: PropTypes.shape({
title: PropTypes.string.isRequired,
text: PropTypes.string,
comments: PropTypes.array
}).isRequired,
}).isRequired, */
id: PropTypes.string,
isOpen: PropTypes.bool,
toggleOpen: PropTypes.func
}
Expand All @@ -33,7 +35,7 @@ class Article extends PureComponent {
const body = isOpen && (
<div>
<section>{article.text}</section>
<CommentList comments = {article.comments} ref = {this.setCommentsRef} key = {this.state.count}/>
<CommentList comments = {article.comments} ref = {this.setCommentsRef} key = {this.state.count} articleId={article.id}/>
</div>
)
return (
Expand Down Expand Up @@ -84,6 +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)
9 changes: 5 additions & 4 deletions src/components/ArticleList.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ 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 = Object.values(articles).map((article) => <li key={article.id}>
<Article //article={article}
id={article.id}
isOpen={article.id === this.state.openItemId}
toggleOpen={this.toggleOpenItemMemoized(article.id)}
/>
</li>)
return (
Expand Down
16 changes: 13 additions & 3 deletions 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 All @@ -12,7 +14,7 @@ class CommentForm extends Component {

render() {
return (
<form onSubmit = {this.handleSubmit}>
<form onSubmit = {this.handleSubmit(this.props.articleId)}>
user: <input value = {this.state.user}
onChange = {this.handleChange('user')}
className = {this.getClassName('user')} />
Expand All @@ -24,12 +26,19 @@ class CommentForm extends Component {
)
}

handleSubmit = ev => {
handleSubmit = articleId => ev => {
const {addComment} = this.props;
ev.preventDefault()
this.setState({
user: '',
text: ''
})
addComment({
articleId: articleId,
id: '',
user: this.state.user,
text: this.state.text,
})
}

isValidForm = () => ['user', 'text'].every(this.isValidField)
Expand Down Expand Up @@ -58,4 +67,5 @@ const limits = {
}
}

export default CommentForm
//export default CommentForm
export default connect(null, {addComment})(CommentForm)
16 changes: 9 additions & 7 deletions src/components/CommentList.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
import React, {Component} from 'react'
import React, {Component, PureComponent} from 'react'
import PropTypes from 'prop-types'
import CommentForm from './CommentForm'
import Comment from './Comment'
import toggleOpen from '../decorators/toggleOpen'

class CommentList extends Component {
class CommentList extends PureComponent {
static propTypes = {
comments: PropTypes.array.isRequired,
//from toggleOpen decorator
isOpen: PropTypes.bool,
toggleOpen: PropTypes.func
toggleOpen: PropTypes.func,
articleId: PropTypes.string,
}

render() {
const {isOpen, toggleOpen} = this.props
console.log('---render commentsList')
const {isOpen, toggleOpen, articleId} = this.props
const text = isOpen ? 'hide comments' : 'show comments'
return (
<div>
<button onClick={toggleOpen}>{text}</button>
{this.getBody()}
{this.getBody(articleId)}
</div>
)
}

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

Expand All @@ -36,7 +38,7 @@ class CommentList extends Component {
return (
<div>
{body}
<CommentForm />
<CommentForm articleId={articleId}/>
</div>
)
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/Filters/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ 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 => ({
const options = Object.values(articles).map(article => ({
label: article.title,
value: article.id
}))
Expand Down
3 changes: 2 additions & 1 deletion src/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ 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'
11 changes: 11 additions & 0 deletions src/middlewares/commentId.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ADD_COMMENT } from '../constants'

export default store => next => action => {
switch (action.type) {
case ADD_COMMENT:
const id = Math.floor(Math.random() * 1000000) + '';
action.payload.comment.id = id;
}

next(action)
}
23 changes: 20 additions & 3 deletions src/reducer/articles.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
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
}), {})

function arrToObj(arr) {
return arr.reduce((acc, article) => ({
...acc,
[article.id]: article
}), {})
}

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

switch (type) {
case DELETE_ARTICLE:
return articlesState.filter(article => article.id !== payload.id)
return arrToObj(Object.values(articlesState).filter(article => article.id !== payload.id))

case ADD_COMMENT:
const {id, articleId} = payload.comment;
const newArticle = articlesState[articleId];
return {...articlesState, [articleId]: {...newArticle, comments: (newArticle.comments || []).concat(id)}}
}

return articlesState
Expand Down
14 changes: 11 additions & 3 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,18 @@ const commentsMap = defaultComments.reduce((acc, comment) => ({
}), {})

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

switch (type) {

case ADD_COMMENT:
const {id, articleId, user, text} = payload.comment;
let comments = {...state};
comments[id] = {
id: id,
user: user,
text: text,
}
return comments;
}

return state
Expand Down
7 changes: 6 additions & 1 deletion src/selectors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ export const createCommentSelector = () => createSelector(commentsSelector, idSe
return comments[id]
})

export const createArticleSelector = () => createSelector(articlesSelector, idSelector, (articles, id) => {
//console.log('---', 'searching for 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 => {
return Object.values(articles).filter(article => {
const published = Date.parse(article.date)
return (!selected.length || selected.includes(article.id)) &&
(!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,6 +1,7 @@
import {createStore, applyMiddleware, compose} from 'redux'
import rootReducer from '../reducer'
import logger from '../middlewares/logger'
import commentId from '../middlewares/commentId'

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

const enhancer = composeEnhancers(
applyMiddleware(logger)
applyMiddleware(logger, commentId)
)
const store = createStore(rootReducer, enhancer)

Expand Down