All files / reducers LayoutReducer.js

0% Statements 0/54
0% Branches 0/19
0% Functions 0/8
0% Lines 0/9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48                                                                                               
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;
  }
}