2019-12-01 22:12:13 +01:00
|
|
|
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) {
|
2019-12-13 13:28:59 +01:00
|
|
|
const { board } = action;
|
2019-12-01 22:12:13 +01:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
byID: {
|
|
|
|
...state.byID,
|
2019-12-13 13:28:59 +01:00
|
|
|
[board.id]: {
|
|
|
|
...board,
|
2019-12-01 22:12:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleFetchBoardsSuccess(state, action) {
|
|
|
|
const boardsByID = action.boards.reduce((byID, board) => {
|
|
|
|
byID[board.id] = board;
|
|
|
|
return byID;
|
|
|
|
}, {});
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
byID: {
|
|
|
|
...boardsByID,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|