-
Notifications
You must be signed in to change notification settings - Fork 14
Hw 5 #73
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?
Hw 5 #73
Changes from all commits
6da0140
a7e1a53
8b397f1
0c4d911
b9a9966
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,9 @@ | ||
| export const INCREMENT = 'INCREMENT' | ||
|
|
||
| export const OPEN_ARTICLE = 'OPEN_ARTICLE' | ||
| 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' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import {ADD_COMMENT} from '../constants' | ||
|
|
||
| export default store => next => action => { | ||
| function genId(len) { | ||
| function getRandomChar() { | ||
| let number = Math.round(Math.random() * 15); | ||
| return number < 10 ? String.fromCharCode(97 + number) : String.fromCharCode(39 + number) | ||
| } | ||
|
|
||
| let result = getRandomChar(); | ||
| for (let i = 0; i < len - 1; i++) { | ||
| result += getRandomChar(); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| switch (action.type) { | ||
| case ADD_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. через мидлвары будет проходить каждый экшин, они должны быть максимально общими, завязывать на конкретные экшины - не лучшее решение |
||
| action.payload.id = genId(10) | ||
|
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, мало-ли что там станут передавать |
||
| action.payload.articleId = store.getState().openArticleId | ||
| break; | ||
| } | ||
|
|
||
| next(action) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,30 @@ | ||
| import { DELETE_ARTICLE } from '../constants' | ||
| import { DELETE_ARTICLE, ADD_COMMENT, OPEN_ARTICLE } from '../constants' | ||
| import {normalizedArticles as defaultArticles} from '../fixtures' | ||
|
|
||
| export default (articlesState = defaultArticles, action) => { | ||
| const articlesMap = defaultArticles.reduce((acc, article) => { | ||
| article.comments = article.comments != null ? article.comments : [] | ||
| return { | ||
| ...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) | ||
| articlesState = {...articlesState} | ||
| delete articlesState[payload.id] | ||
| return articlesState | ||
| case ADD_COMMENT: | ||
| let article = {...articlesState[payload.articleId]} | ||
| article.comments = [...article.comments] | ||
|
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. comments может быть undefined |
||
| article.comments.push(payload.id) | ||
| return { | ||
| ...articlesState, | ||
| [payload.articleId]: article | ||
| } | ||
| } | ||
|
|
||
| return articlesState | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import {OPEN_ARTICLE} from '../constants' | ||
|
|
||
| export default (openArticleId = null, action) => { | ||
| return action.type === OPEN_ARTICLE ? action.payload.id : openArticleId | ||
| } |
| 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 idGenerator from '../middlewares/idGenerator' | ||
|
|
||
| const composeEnhancers = | ||
| typeof window === 'object' && | ||
|
|
@@ -10,7 +11,7 @@ const composeEnhancers = | |
| }) : compose | ||
|
|
||
| const enhancer = composeEnhancers( | ||
| applyMiddleware(logger) | ||
| applyMiddleware(logger, idGenerator) | ||
|
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. лучше логгер в конце ставить |
||
| ) | ||
| const store = createStore(rootReducer, enhancer) | ||
|
|
||
|
|
||
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.
Лучше селектор, который достанет из в виде массива