-
Notifications
You must be signed in to change notification settings - Fork 20
ht4 #48
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 #48
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 |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export default store => next => action => { | ||
| if (action.type === 'ADD_COMMENT') action.payload.id = Math.random().toString(36).substring(7); | ||
| next(action); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,27 @@ | ||
| 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 | ||
| let articlesStateObj; | ||
|
|
||
| switch (type) { | ||
| case ADD_COMMENT: | ||
| articlesStateObj = articlesState | ||
| articlesStateObj[payload.articleId].comments.push(payload.id) | ||
|
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 articlesStateObj | ||
| case DELETE_ARTICLE: | ||
| return articlesState.filter(article => article.id !== payload.id) | ||
| articlesStateObj = articlesState | ||
| delete articlesStateObj[payload.id] | ||
|
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 articlesStateObj | ||
|
|
||
| default: | ||
| return articlesState | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import {} from '../constants' | ||
| import { normalizedComments } from '../fixtures' | ||
| import { ADD_COMMENT } from '../constants' | ||
|
|
||
| const defaultComments = normalizedComments.reduce((acc, comment) => ({ | ||
| ...acc, | ||
|
|
@@ -8,11 +9,21 @@ const defaultComments = normalizedComments.reduce((acc, comment) => ({ | |
| , {}) | ||
|
|
||
| export default (commentsState = defaultComments, action) => { | ||
| const {type} = action | ||
| const {type, payload} = action | ||
| let newCommentsState={}; | ||
|
|
||
| switch (type) { | ||
|
|
||
| case ADD_COMMENT: | ||
| debugger; | ||
| newCommentsState = commentsState | ||
| newCommentsState[payload.id] = { | ||
|
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: payload.id, | ||
| user: payload.user, | ||
| text: payload.comment | ||
| } | ||
| return newCommentsState | ||
| default: | ||
| return commentsState | ||
| } | ||
| } | ||
| } | ||
| 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 generateId from '../middlewares/generateId' | ||
|
|
||
| const enhancer = applyMiddleware(logger) | ||
| const enhancer = applyMiddleware(logger, generateId) | ||
|
|
||
| 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.
через мидлвары будет проходить каждый экшин, они должны быть максимально общими, завязывать на конкретные экшины - не лучшее решение