import { BUILD_KANBOARD_SUCCESS, MOVE_CARD } from "../actions/kanboards"; export const defaultState = { byID: {}, }; export function kanboardsReducer(state = defaultState, action) { switch(action.type) { case BUILD_KANBOARD_SUCCESS: return handleBuildKanboardSuccess(state, action); case MOVE_CARD: return handleMoveCard(state, action); default: return state; } } function handleBuildKanboardSuccess(state, action) { return { ...state, byID: { ...state.byID, [action.kanboard.id]: { ...action.kanboard, } } }; } function handleMoveCard(state, action) { const { boardID, fromLaneID, fromPosition, toLaneID, toPosition } = action; const kanboard = state.byID[boardID]; const lanes = [ ...kanboard.lanes ]; const fromLane = lanes[fromLaneID]; const toLane = lanes[toLaneID]; const card = fromLane.cards[fromPosition]; const fromCards = [ ...fromLane.cards ]; if (fromLaneID !== toLaneID) { fromCards.splice(fromPosition, 1); lanes[fromLaneID] = { ...fromLane, cards: fromCards, }; const toCards = [ ...toLane.cards ]; toCards.splice(toPosition, 0, card); lanes[toLaneID] = { ...toLane, cards: toCards, }; } else { fromCards.splice(fromPosition, 1); fromCards.splice(toPosition, 0, card); } return { ...state, byID: { ...state.byID, [boardID]: { ...state.byID[boardID], lanes, }, } }; }