41 lines
846 B
JavaScript
41 lines
846 B
JavaScript
|
import { SAVE_BOARD_SUCCESS, FETCH_BOARDS_SUCCESS } from "../actions/boards";
|
||
|
|
||
|
export const defaultState = {
|
||
|
byID: {},
|
||
|
};
|
||
|
|
||
|
export function boardsReducer(state = defaultState, action) {
|
||
|
switch(action.type) {
|
||
|
case SAVE_BOARD_SUCCESS:
|
||
|
return handleSaveBoardSuccess(state, action);
|
||
|
case FETCH_BOARDS_SUCCESS:
|
||
|
return handleFetchBoardsSuccess(state, action);
|
||
|
default:
|
||
|
return state;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function handleSaveBoardSuccess(state, action) {
|
||
|
return {
|
||
|
...state,
|
||
|
byID: {
|
||
|
...state.byID,
|
||
|
[action.board.id.toString()]: {
|
||
|
...action.board,
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
|
||
|
function handleFetchBoardsSuccess(state, action) {
|
||
|
const boardsByID = action.boards.reduce((byID, board) => {
|
||
|
byID[board.id] = board;
|
||
|
return byID;
|
||
|
}, {});
|
||
|
return {
|
||
|
...state,
|
||
|
byID: {
|
||
|
...boardsByID,
|
||
|
}
|
||
|
};
|
||
|
}
|