-
Notifications
You must be signed in to change notification settings - Fork 20
Ht4 #46
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
base: master
Are you sure you want to change the base?
Ht4 #46
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| export const INCREMENT = 'INCREMENT' | ||
|
|
||
| export const DELETE_ARTICLE = 'DELETE_ARTICLE' | ||
| export const ADD_COMMENT = 'ADD_COMMENT' | ||
|
|
||
| export const CHANGE_SELECTION = 'CHANGE_SELECTION' | ||
| export const CHANGE_DATE_RANGE = 'CHANGE_DATE_RANGE' | ||
| export const CHANGE_DATE_RANGE = 'CHANGE_DATE_RANGE' |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| export default store => next => action => { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. через мидлвары будет проходить каждый экшин, но не каждому нужно генерить id |
||
| let uuid | ||
|
|
||
| function s4() { | ||
| return Math.floor((1 + Math.random()) * 0x10000) | ||
| .toString(16) | ||
| .substring(1); | ||
| } | ||
| uuid = s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4(); | ||
|
|
||
| action.payload.uuid = uuid; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лучше не мутировать payload, мало-ли что там станут передавать |
||
|
|
||
| next(action) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,26 @@ | ||
| 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 | ||
| }) | ||
| , {}) | ||
|
|
||
| export default (articlesState = defaultArticles, action) => { | ||
| const { type, payload } = action | ||
|
|
||
| console.log('ARTICLES', articlesState) | ||
| switch (type) { | ||
| case ADD_COMMENT: | ||
| let id = Object.keys(articlesState)[0] | ||
| articlesState[id].comments.push(action.payload.uuid) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Не мутируй стейт! |
||
| return articlesState | ||
|
|
||
| case DELETE_ARTICLE: | ||
| return articlesState.filter(article => article.id !== payload.id) | ||
|
|
||
| default: | ||
| return articlesState | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import {} from '../constants' | ||
| import {ADD_COMMENT} from '../constants' | ||
| import { normalizedComments } from '../fixtures' | ||
|
|
||
| const defaultComments = normalizedComments.reduce((acc, comment) => ({ | ||
|
|
@@ -11,8 +11,16 @@ export default (commentsState = defaultComments, action) => { | |
| const {type} = action | ||
|
|
||
| switch (type) { | ||
| case ADD_COMMENT: | ||
| let comment = {} | ||
| comment['id'] = action.payload.uuid | ||
| comment['text'] = action.payload.text | ||
| comment['user'] = 'USER1' | ||
| commentsState[action.payload.uuid] = comment | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. И здесь не мутируй |
||
| console.log('COMMENT STATE', commentsState) | ||
| return commentsState | ||
|
|
||
| default: | ||
| return commentsState | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import { CHANGE_SELECTED } from '../constants' | ||
|
|
||
| export default (selectState = null, action) => { | ||
| const { type, payload } = action | ||
|
|
||
| return type === CHANGE_SELECTED ? payload.selection : selectState | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,22 @@ | ||
| import { createSelector } from 'reselect' | ||
|
|
||
| const articleListSelector = state => state.articles | ||
| const articlesSelector = state => state.articles | ||
| const commentsSelector = state => state.comments | ||
| const filtersSelector = state => state.filters | ||
| const idSelector = (_, props) => props.id | ||
|
|
||
| export const filtratedArticles = createSelector(articleListSelector, filtersSelector, (articles, filters) => { | ||
| export const filtratedArticles = createSelector(articlesSelector, filtersSelector, (articles, filters) => { | ||
| const {selected, dateRange: {from, to}} = filters | ||
| console.log('---', 'recomputing filtration') | ||
|
|
||
| return articles.filter(article => { | ||
| return Object.keys(articles).filter(id => { | ||
| const article = articles[id] | ||
| const published = Date.parse(article.date) | ||
| return (!selected.length || selected.includes(article.id)) && | ||
| (!from || !to || (published > from && published < to)) | ||
| }) | ||
| }) | ||
|
|
||
| export const createCommentSelector = () => createSelector(commentsSelector, idSelector, (comments, id) => comments[id]) | ||
| export const createCommentSelector = () => createSelector(commentsSelector, idSelector, (comments, id) => comments[id]) | ||
|
|
||
| export const createArticleSelector = () => createSelector(articlesSelector, idSelector, (articles, id) => articles[id]) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,13 @@ | ||
| import { createStore, applyMiddleware } from 'redux' | ||
| import reducer from '../reducer' | ||
| import logger from '../middlewares/logger' | ||
| import uuid from '../middlewares/uuid' | ||
|
|
||
| const enhancer = applyMiddleware(logger) | ||
| const enhancer = applyMiddleware(logger, uuid) | ||
|
|
||
| const store = createStore(reducer, enhancer) | ||
|
|
||
| //dev only, no need in prod | ||
| window.store = store | ||
|
|
||
| export default store | ||
| export default store |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Лучше селектор, который достанет их в виде массива