–°hange the values of all nested objects Immutable.js

reducer looks like this

import {ADD_TASK, EDIT_TASK, CHECK_TASK, CHECK_ALL_TASK} from '../constants';
import {OrderedMap, Record} from 'immutable';

const TaskRecord = Record({
    id: null,
    text: null,
    isChecked: false
});

const ReducerState = Record({
    entities: new OrderedMap({})
});

const defaultState = new ReducerState();

export default (tasksState = defaultState, action) => {
    const {type, payload, randomId} = action;

    switch (type) {
        case ADD_TASK:
            return tasksState.setIn(['entities', randomId], new TaskRecord({...payload.task, id: randomId}));
        case CHECK_ALL_TASK:
            return tasksState.map(entities => {
                return entities.map((task) => {
                    task.set('isChecked', true);
                });
            });
    };

    return tasksState;
};

How change all isChecked in TaskRecord that in entities? I wrote CHECK_ALL_TASK, but it gives an error (tasksState undefined)

1 answer

  • answered 2018-01-12 06:52 Qwerty

    wrote this function, but can it be made as something simpler?

    function checkAllCheckboxes(state) {
        let newState = state;
        for (let id of state.entities) {
            newState = newState.setIn(['entities', id[0], 'isChecked'], true);
        }
        return newState;
    }