import initialLayout from './initialLayout';
function addCard(state, action) {
const { card, column, tab, index, size } = action;
let cards = state[tab][size].columns[column].cards;
cards = [...cards.slice(0, index), card, ...cards.slice(index)];
const nextState = { ...state };
nextState[tab][size].columns[column].cards = cards;
return { ...state, ...nextState };
}
function removeCard(state, action) {
const { column, tab, index, size } = action;
let cards = state[tab][size].columns[column].cards;
cards = [...cards.slice(0, index), ...cards.slice(index + 1)];
const nextState = { ...state };
nextState[tab][size].columns[column].cards = cards;
return { ...state, ...nextState };
}
function moveCard(state, action) {
const { tab, size, from, to } = action;
const card = { ...state[tab][size].columns[from.column].cards[from.index] };
card.props.column = to.column;
const midState = removeCard(state, { card, column: from.column, index: from.index, size, tab });
const nextState = addCard(midState, { card, column: to.column, index: to.index, tab, size });
return { ...state, ...nextState };
}
function restoreDefault(state, action) {
return initialLayout;
}
export default function(state = initialLayout, action) {
switch (action.type) {
case 'MOVE_CARD':
return moveCard(state, action);
case 'REMOVE_CARD':
return removeCard(state, action);
case 'ADD_CARD':
return addCard(state, action);
case 'RESTORE_LAYOUT':
return restoreDefault(state, action);
default:
return state;
}
}
|