Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* eslint-disable no-param-reassign */
- import {
- CREATE_FORM_SHAPE,
- TOGGLE_ADDONS,
- TOGGLE_PEOPLE_ADDONS,
- } from '../actions/actionTypes';
- type State = Array;
- const initialState = {
- product: [],
- party: {},
- };
- const createShape = (state, id, qty) => {
- const obj = {};
- for (let x = 0; x < qty; x++) {
- obj[x] = true;
- }
- console.log(state, 'shaper', state[id], obj);
- state[id] = obj;
- return state;
- };
- const updateParticipants = (state, id, i, qty) => {
- if (!state[id]) {
- state[id] = {};
- }
- state[id][i] = state[id][i] === false;
- const atLeastOne = Object.keys(state[id]).filter(x => state[id][x] === true).length;
- console.log(atLeastOne);
- state[id].qty = atLeastOne;
- return state;
- };
- export default function hotel(state: State = initialState, action: Object): State {
- switch (action.type) {
- case TOGGLE_ADDONS: return {
- ...state,
- product: state.product.some(x => x.id === action.id) ?
- state.product.filter(x => x.id !== action.id) :
- [...state.product, {
- id: action.id,
- qty: action.qty,
- }],
- };
- case TOGGLE_PEOPLE_ADDONS: return {
- ...state,
- party: updateParticipants(state.party, action.id, action.i, action.qty),
- };
- case CREATE_FORM_SHAPE: return {
- ...state,
- party: createShape(state.party, action.id, action.qty),
- };
- default: return state;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement