Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* eslint-disable no-param-reassign */
  2. import {
  3.   CREATE_FORM_SHAPE,
  4.   TOGGLE_ADDONS,
  5.   TOGGLE_PEOPLE_ADDONS,
  6. } from '../actions/actionTypes';
  7.  
  8. type State = Array;
  9.  
  10. const initialState = {
  11.   product: [],
  12.   party: {},
  13. };
  14.  
  15.  
  16. const createShape = (state, id, qty) => {
  17.   const obj = {};
  18.   for (let x = 0; x < qty; x++) {
  19.     obj[x] = true;
  20.   }
  21.  
  22.   console.log(state, 'shaper', state[id], obj);
  23.   state[id] = obj;
  24.   return state;
  25. };
  26.  
  27. const updateParticipants = (state, id, i, qty) => {
  28.   if (!state[id]) {
  29.     state[id] = {};
  30.   }
  31.   state[id][i] = state[id][i] === false;
  32.   const atLeastOne = Object.keys(state[id]).filter(x => state[id][x] === true).length;
  33.   console.log(atLeastOne);
  34.   state[id].qty = atLeastOne;
  35.   return state;
  36. };
  37.  
  38.  
  39. export default function hotel(state: State = initialState, action: Object): State {
  40.   switch (action.type) {
  41.     case TOGGLE_ADDONS: return {
  42.       ...state,
  43.       product: state.product.some(x => x.id === action.id) ?
  44.         state.product.filter(x => x.id !== action.id) :
  45.         [...state.product, {
  46.           id: action.id,
  47.           qty: action.qty,
  48.         }],
  49.     };
  50.     case TOGGLE_PEOPLE_ADDONS: return {
  51.       ...state,
  52.       party: updateParticipants(state.party, action.id, action.i, action.qty),
  53.     };
  54.     case CREATE_FORM_SHAPE: return {
  55.       ...state,
  56.       party: createShape(state.party, action.id, action.qty),
  57.     };
  58.     default: return state;
  59.   }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement