Skip to content

Commit

Permalink
6.3.2 리듀서에서 공통 기능 분리하기
Browse files Browse the repository at this point in the history
  • Loading branch information
b-chae committed Oct 22, 2021
1 parent ff6eca6 commit 030aa6b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 49 deletions.
29 changes: 29 additions & 0 deletions src/common/createItemsLogic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import createReducer from "./createReducer"

export default function createItemsLogic(name) {
const ADD = `${name}/ADD`
const REMOVE = `${name}/REMOVE`
const EDIT = `${name}/EDIT`

This comment has been minimized.

Copy link
@b-chae

b-chae Oct 22, 2021

Author Owner

name을 매개변수로 받아서 액션들을 만들어서 Return 해주는 함수


const add = item => ({type: ADD, item})
const remove = item => ({type: REMOVE, item})
const edit = item => ({type: EDIT, item})

const reducer = createReducer(
{ [name]: []},
{
[ADD]: (state, action) => state[name].push(action.item),
[REMOVE]: (state, action) => {
const index = state[name].findIndex(item => item.id === action.item.id)
state[name].splice(index, 1)
},
[EDIT]: (state, action) => {
const index = state[name].findIndex(item => item.id === action.item.id)
if (index >= 0) {
state[name][index] = action.item
}
}}
)

return {add, remove, edit, reducer}
}
13 changes: 13 additions & 0 deletions src/common/mergeReducers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default function mergeReducers(reducers) {

This comment has been minimized.

Copy link
@b-chae

b-chae Oct 22, 2021

Author Owner

combineReducers 함수를 이용하면 상태값의 깊이가 불필요하게 깊어진다.

const state = {
  timeline: {
    common:  {
      nextPage: 0,
    }, //
return function(state, action) {
if (!state) {
return reducers.reduce((acc, r) => ({...acc, ...r(state, action)}), {})
} else {
let nextState = state
for (const r of reducers) {
nextState = r(nextState, action)
}
return nextState
}
}
}
32 changes: 6 additions & 26 deletions src/friend/state.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,7 @@
import createReducer from "../common/createReducer"
import createItemsLogic from "../common/createItemsLogic"

const ADD = 'friend/ADD'
const REMOVE = 'friend/REMOVE'
const EDIT = 'friend/EDIT'

export const addFriend = friend => ({type: ADD, friend})
export const removeFriend = friend => ({type: REMOVE, friend})
export const editFriend = friend => ({type: EDIT, friend})

const INITIAL_STSTE = {friends: []}
export const friendReducer = createReducer(INITIAL_STSTE, {
[ADD]: (state, action) => state.friends.push(action.friend),
[REMOVE]: (state, action) => (
state.friends = state.friends.filter(
friend => friend.id !== action.friend.id
)
),
[EDIT]: (state, action) => {
const idx = state.friends.findIndex(
friend => friend.id === action.friend.id
)
if(idx >= 0){
state.friends[idx] = action.friend
}
},
})
const {add, remove, edit, reducer} = createItemsLogic('friends')
export const addFriend = add
export const removeFriend = remove
export const editFriend = edit
export const friendReducer = reducer
35 changes: 12 additions & 23 deletions src/timeline/state.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,19 @@
import createItemsLogic from "../common/createItemsLogic"
import createReducer from "../common/createReducer"
import mergeReducers from "../common/mergeReducers"

const ADD = 'timeline/ADD'
const REMOVE = 'timeline/REMOVE'
const EDIT = 'timeline/EDIT'
const INCREASE_NEXT_PAGE = 'timeline/INCREASE_NEXT_PAGE'

export const addTimeline = timeline => ({type: ADD, timeline})
export const removeTimeline = timeline => ({type: REMOVE, timeline})
export const editTimeline = timeline => ({type: EDIT, timeline})
const {add, remove, edit, reducer: timelinesReducer} = createItemsLogic('timelines')

export const addTimeline = add
export const removeTimeline = remove
export const editTimeline = edit
export const increaseNextPage = () => ({type: INCREASE_NEXT_PAGE})

const INITIAL_STSTE = {timelines: [], nextPage: 0}
export const timelineReducer = createReducer(INITIAL_STSTE, {
[ADD]: (state, action) => state.timelines.push(action.timeline),
[REMOVE]: (state, action) => (
state.timelines = state.timelines.filter(
timeline => timeline.id !== action.timeline.id
)
),
[EDIT]: (state, action) => {
const idx = state.timelines.findIndex(
timeline => timeline.id === action.timeline.id
)
if(idx >= 0){
state.timelines[idx] = action.timeline
}
},
const INITIAL_STSTE = {nextPage: 0}
const reducer = createReducer(INITIAL_STSTE, {
[INCREASE_NEXT_PAGE]: (state, ) => (state.nextPage += 1)
})
})
const reducers = [reducer, timelinesReducer]
export const timelineReducer = mergeReducers(reducers)

0 comments on commit 030aa6b

Please sign in to comment.