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

74 lines
1.6 KiB
TypeScript

import { BUILD_KANBOARD_SUCCESS, MOVE_CARD } from "../actions/kanboards";
import { CREATE_ISSUE_SUCCESS } from "../actions/issues";
export const defaultState = {
byID: {},
};
export function kanboardsReducer(state = defaultState, action: any) {
switch(action.type) {
case BUILD_KANBOARD_SUCCESS:
return handleBuildKanboardSuccess(state, action);
case MOVE_CARD:
return handleMoveCard(state, action);
default:
return state;
}
}
function handleBuildKanboardSuccess(state: any, action: any) {
return {
...state,
byID: {
...state.byID,
[action.kanboard.id]: {
...action.kanboard,
}
}
};
}
function handleMoveCard(state: any, action: any) {
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,
},
}
};
}