gengitkan/client/src/store/reducers/boards.ts

42 lines
884 B
TypeScript
Raw Normal View History

2019-12-01 22:12:13 +01:00
import { SAVE_BOARD_SUCCESS, FETCH_BOARDS_SUCCESS } from "../actions/boards";
export const defaultState = {
byID: {},
};
2020-04-30 13:02:56 +02:00
export function boardsReducer(state = defaultState, action: any) {
2019-12-01 22:12:13 +01:00
switch(action.type) {
case SAVE_BOARD_SUCCESS:
return handleSaveBoardSuccess(state, action);
case FETCH_BOARDS_SUCCESS:
return handleFetchBoardsSuccess(state, action);
default:
return state;
}
}
2020-04-30 13:02:56 +02:00
function handleSaveBoardSuccess(state: any, action: any) {
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
}
}
};
}
2020-04-30 13:02:56 +02:00
function handleFetchBoardsSuccess(state: any, action: any) {
const boardsByID = action.boards.reduce((byID: any, board: any) => {
2019-12-01 22:12:13 +01:00
byID[board.id] = board;
return byID;
}, {});
return {
...state,
byID: {
...boardsByID,
}
};
}