const Immutable = require('immutable'); import * as BoardActions from '../../actions/board/board.actions'; import Values from '../Values'; import { SETTINGS } from '../../App'; const initial = { level: -1, values: [], title: 'Setting up...' }; const count = SETTINGS.GRID_WIDTH * SETTINGS.GRID_HEIGHT; const reducer = (state = initial, action) => { if (action.type !== BoardActions.BOARD_ACTION) { return state; } switch (action.action) { case BoardActions.UPDATE: return Immutable.fromJS(state).setIn(['values', action.index, 'value'], action.value).toJS(); case BoardActions.SHOW: return Immutable.fromJS(state).setIn(['values', action.index, 'show'], true).toJS(); case BoardActions.HIDE: return Immutable.fromJS(state).setIn(['values', action.index, 'show'], false).toJS(); case BoardActions.NEXT_LEVEL: const lvl = state.level + 1; return Immutable.Map(state) .set('level', lvl) .set('title', Values.getDescription(lvl)) .set('values', Values.generate(count, lvl)) .toObject(); } return state; }; export default reducer;