How to change all hidden values to false in each state object?

Viewed 69

I am a little bit stumped on completing this without my solution looking hacky. Any recommendations? Is this easier to do in the reducer or should I keep this strictly in the action?

Surely this is easier than what I am attempting.

const initialState = {
  unlockWebsite: {
    hidden: false,
    state: 'accepted',
    completedCount: 60,
    completedIds: ['priceYourHome', 'websiteInformation'],
    totalTasks: 6,
  },
  prepareHome: {
    hidden: true,
    state: 'default',
    completedCount: 0,
    completedIds: [],
    totalTasks: 6,
  },
  findingBuyers: {
    hidden: true,
    state: 'default',
    completedCount: 0,
    completedIds: [],
    totalTasks: 6,
  },
  negotiateOffers: {
    hidden: true,
    state: 'default',
    completedCount: 0,
    completedIds: [],
    totalTasks: 6,
  },
  closeHome: {
    hidden: true,
    state: 'default',
    completedCount: 0,
    completedIds: [],
    totalTasks: 6,
  },
};
1 Answers

Take an example where the initial state is

const initState = {"a":{no_change:"hi",change:"ha"},"b":{no_change:"hi",change:"ha"},"c":{no_change:"hi",change:"ha"}}

Say we want to update all of the change properties in all of a, b and c from "ha" to something else.

I can just create an action type in my reducer called CHANGE_TO_FALSE.

case 'CHANGE_TO_FALSE':
  const falsifier = Object.keys(state).map((key,index) =>({[key]:{...state[key],change:"hoo"}}))
  return Object.assign({}, ...falsifier )

Now, instead of "ha", the change property will hold "hoo".

State after change:

{"a":{"no_change":"hi","change":"hoo"},"b":{"no_change":"hi","change":"hoo"},"c":{"no_change":"hi","change":"hoo"}} 

I'm not sure there's a simpler way to achieve this. This looks clean to me.

Related