diff --git a/src/AC/index.js b/src/AC/index.js index dda2dd6..6d323a2 100644 --- a/src/AC/index.js +++ b/src/AC/index.js @@ -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 { @@ -26,3 +26,10 @@ export function changeSelection(selected) { payload: { selected } } } + +export function addComment(data) { + return { + type: ADD_COMMENT, + payload: {data} + } +} diff --git a/src/components/article-list/index.js b/src/components/article-list/index.js index f2cebc8..8895d72 100644 --- a/src/components/article-list/index.js +++ b/src/components/article-list/index.js @@ -21,7 +21,7 @@ export class ArticleList extends Component { render() { const { articles, openItemId, toggleItem } = this.props console.log('---', 'rendering ArticlList') - const articleElements = articles.map(article => + const articleElements = Object.values(articles).map(article =>
  • + + + +
    + + +
    + + +
    + +
    + + ) + } + + handleInput = ev => { + this.setState({ + authorname: ev.target.value + }) + } + + handleTextChange = ev => { + this.setState({ + commenttext: ev.target.value + }) + } + + handleAddcommentClick = () => { + const {addComment, rel, toggleOpen} = this.props + if (this.state.authorname.length && this.state.commenttext.length ) { + toggleOpen() + addComment({articleid: rel, user: this.state.authorname, text: this.state.commenttext}) + this.setState({authorname: '', commenttext:''}) + } + } +} + +export default connect(null, { addComment })(toggleOpen(CommentForm)) \ No newline at end of file diff --git a/src/components/filters/select.js b/src/components/filters/select.js index 1a91857..a9252b6 100644 --- a/src/components/filters/select.js +++ b/src/components/filters/select.js @@ -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 })) diff --git a/src/constants/index.js b/src/constants/index.js index a2f0a80..dfd51bf 100644 --- a/src/constants/index.js +++ b/src/constants/index.js @@ -3,4 +3,6 @@ export const INCREMENT = 'INCREMENT' export const DELETE_ARTICLE = 'DELETE_ARTICLE' export const CHANGE_SELECTION = 'CHANGE_SELECTION' -export const CHANGE_DATE_RANGE = 'CHANGE_DATE_RANGE' \ No newline at end of file +export const CHANGE_DATE_RANGE = 'CHANGE_DATE_RANGE' + +export const ADD_COMMENT = 'ADD_COMMENT' \ No newline at end of file diff --git a/src/middlewares/randomid.js b/src/middlewares/randomid.js new file mode 100644 index 0000000..0fb0911 --- /dev/null +++ b/src/middlewares/randomid.js @@ -0,0 +1,22 @@ +import { ADD_COMMENT } from '../constants' +/** + * Shuffles array in place. ES6 version + * @param {Array} a items An array containing the items. + */ +function shuffle(a) { + for (let i = a.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [a[i], a[j]] = [a[j], a[i]]; + } + return a; +} + +export default store => next => action => { + if (action.type === ADD_COMMENT) { + const randomId = shuffle((new Date()).toLocaleString('en-US').split('').filter((symbol)=> { + return symbol !== ' ' + })).join('') + action.payload.data['id'] = randomId + } + next(action) +} \ No newline at end of file diff --git a/src/reducer/articles.js b/src/reducer/articles.js index 1c2e629..f02661c 100644 --- a/src/reducer/articles.js +++ b/src/reducer/articles.js @@ -1,12 +1,25 @@ -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((wii, article)=>({ + ...wii, + [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) + let articles = Object.assign({}, articlesState); + delete articles[payload.id] + return articles + + case ADD_COMMENT: + let comments = articlesState[payload.data.articleid].comments + typeof comments !== 'undefined' ? comments.push(payload.data.id) : articlesState[payload.data.articleid].comments = new Array(payload.data.id) + //break default: return articlesState diff --git a/src/reducer/comments.js b/src/reducer/comments.js index 0bd398a..0cf409a 100644 --- a/src/reducer/comments.js +++ b/src/reducer/comments.js @@ -1,4 +1,4 @@ -import {} from '../constants' +import { ADD_COMMENT } from '../constants' import { normalizedComments } from '../fixtures' const defaultComments = normalizedComments.reduce((acc, comment) => ({ @@ -8,10 +8,13 @@ const defaultComments = normalizedComments.reduce((acc, comment) => ({ , {}) export default (commentsState = defaultComments, action) => { - const {type} = action + const {type, payload} = action switch (type) { - + case ADD_COMMENT: + const {id, user, text} = payload.data + commentsState[id] = {id: id, user: user, text: text} + //break default: return commentsState } diff --git a/src/selectors/index.js b/src/selectors/index.js index d503ae7..1b19d61 100644 --- a/src/selectors/index.js +++ b/src/selectors/index.js @@ -8,8 +8,8 @@ const idSelector = (_, props) => props.id export const filtratedArticles = createSelector(articleListSelector, filtersSelector, (articles, filters) => { const {selected, dateRange: {from, to}} = filters console.log('---', 'recomputing filtration') - - 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)) diff --git a/src/store/index.js b/src/store/index.js index e3ddcc8..400bc89 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -1,10 +1,13 @@ import { createStore, applyMiddleware } from 'redux' import reducer from '../reducer' import logger from '../middlewares/logger' +import randomstringid from '../middlewares/randomid' const enhancer = applyMiddleware(logger) -const store = createStore(reducer, enhancer) +const saidtodo = applyMiddleware(randomstringid) + +const store = createStore(reducer, enhancer, saidtodo) //dev only, no need in prod window.store = store