OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Is there a simpler way to update a nested Redux object?

  • Thread starter Thread starter robertwerner_sf
  • Start date Start date
R

robertwerner_sf

Guest
I'm dealing with a complex nested object in Redux with this general structure: Job > Tasks > Questions > Options. Here's some code I wrote a few years ago to update a question within this object:

Code:
case UPDATE_QUESTION:
  if (!action.payload?.taskId || action.payload?.taskId === state.job.id) {
    return {
      ...state,
      job: {
        ...state.job,
        renderedByQuestions: (state.job.renderedByQuestions ?? []).map((question) => {
          if (question.id === action.payload?.questionId) {
            return performContingentReduction(question);
          } else {
            return question;
          }
        }),
      },
      isDirty: true,
    };
  } else {
    return {
      ...state,
      job: {
        ...state.job,
        tasks: state.job.tasks.map((task) => {
          if (task.id === action.payload?.taskId) {
            if (action.payload?.isContingent) {
              return {
                ...task,
                renderedByQuestions: (task.renderedByQuestions ?? []).map((question) => {
                  if (question.id === action.payload?.questionId) {
                    return performContingentReduction(question);
                  } else {
                    return question;
                  }
                }),
              };
            } else {
              return {
                ...task,
                questions: task.questions.map((question) => {
                  if (action.payload?.isNested && question.children) {
                    return {
                      ...question,
                      children: question.children.map((childQuestion) => {
                        if (childQuestion.children) {
                          return {
                            ...childQuestion,
                            children: childQuestion.children.map((grandChildQuestion) => {
                              return grandChildQuestion.id === action.payload?.questionId
                                ? performReduction(grandChildQuestion)
                                : grandChildQuestion;
                            }),
                          };
                        } else {
                          return childQuestion.id === action.payload?.questionId
                            ? performReduction(childQuestion)
                            : childQuestion;
                        }
                      }),
                    };
                  } else if (question.id === action.payload?.questionId) {
                    return performReduction(question);
                  } else if (
                    question.conditionalQuestionParentId === action.payload?.questionId &&
                    question.type !== action.payload?.type
                  ) {
                    return {
                      ...question,
                      renderedByOptionIds: null,
                      renderedByMin: null,
                      renderedByMax: null,
                    };
                  } else {
                    return question;
                  }
                }),
              };
            }
          } else {
            return task;
          }
        }),
      },
      isDirty: true,
    };
  }

While this code works, it's rather complex and "ugly". Now that we're in 2024, I wonder if there any technologies that make it easier to update a given item in an array of nested objects?

Robert

<p>I'm dealing with a complex nested object in Redux with this general structure: <code>Job > Tasks > Questions > Options</code>. Here's some code I wrote a few years ago to update a question within this object:</p>
<pre><code>case UPDATE_QUESTION:
if (!action.payload?.taskId || action.payload?.taskId === state.job.id) {
return {
...state,
job: {
...state.job,
renderedByQuestions: (state.job.renderedByQuestions ?? []).map((question) => {
if (question.id === action.payload?.questionId) {
return performContingentReduction(question);
} else {
return question;
}
}),
},
isDirty: true,
};
} else {
return {
...state,
job: {
...state.job,
tasks: state.job.tasks.map((task) => {
if (task.id === action.payload?.taskId) {
if (action.payload?.isContingent) {
return {
...task,
renderedByQuestions: (task.renderedByQuestions ?? []).map((question) => {
if (question.id === action.payload?.questionId) {
return performContingentReduction(question);
} else {
return question;
}
}),
};
} else {
return {
...task,
questions: task.questions.map((question) => {
if (action.payload?.isNested && question.children) {
return {
...question,
children: question.children.map((childQuestion) => {
if (childQuestion.children) {
return {
...childQuestion,
children: childQuestion.children.map((grandChildQuestion) => {
return grandChildQuestion.id === action.payload?.questionId
? performReduction(grandChildQuestion)
: grandChildQuestion;
}),
};
} else {
return childQuestion.id === action.payload?.questionId
? performReduction(childQuestion)
: childQuestion;
}
}),
};
} else if (question.id === action.payload?.questionId) {
return performReduction(question);
} else if (
question.conditionalQuestionParentId === action.payload?.questionId &&
question.type !== action.payload?.type
) {
return {
...question,
renderedByOptionIds: null,
renderedByMin: null,
renderedByMax: null,
};
} else {
return question;
}
}),
};
}
} else {
return task;
}
}),
},
isDirty: true,
};
}
</code></pre>
<p>While this code works, it's rather complex and "ugly". Now that we're in 2024, I wonder if there any technologies that make it easier to update a given item in an array of nested objects?</p>
<p>Robert</p>
 
Top