42 lines
884 B
TypeScript
42 lines
884 B
TypeScript
import { SAVE_BOARD_SUCCESS, FETCH_BOARDS_SUCCESS } from "../actions/boards";
|
|
|
|
export const defaultState = {
|
|
byID: {},
|
|
};
|
|
|
|
export function boardsReducer(state = defaultState, action: any) {
|
|
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: any, action: any) {
|
|
const { board } = action;
|
|
return {
|
|
...state,
|
|
byID: {
|
|
...state.byID,
|
|
[board.id]: {
|
|
...board,
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
function handleFetchBoardsSuccess(state: any, action: any) {
|
|
const boardsByID = action.boards.reduce((byID: any, board: any) => {
|
|
byID[board.id] = board;
|
|
return byID;
|
|
}, {});
|
|
return {
|
|
...state,
|
|
byID: {
|
|
...boardsByID,
|
|
}
|
|
};
|
|
} |