engineforce/ImmutableAssign

Error when using bracket notation to access property in getProp

husainshabbir opened this issue · 2 comments

I get an error if I use the bracket notation in the getProp function. The property names in my application are dynamic so I pass all the information used to construct them through the external context parameter. However, this results in a Cannot find quoted text for undefined error. The below code reproduces this problem:

var myState = {
  questions: {
    page_51: [
      { id: 100, text: 'Question One' },
      { id: 101, text: 'Question Two' }
    ],
    page_53: [
      { id: 500, text: 'Question Three' },
      { id: 502, text: 'Question Four' }
    ]
  }
};

const insertAction = {
  pageId: 53,
  position: 1,
  question: { id: 505, text: 'The new question' },
};

var newState = iassign(
    myState,
    (s, context) => s.questions[`page_${context.pageId}`],
    (questions) => {
      questions.splice(insertAction.position, 0, insertAction.question);
      return questions;
    },
    { pageId: insertAction.pageId }
);

console.log(newState);

Hi husainshabbir, unfortunately getProp() cannot handle complicated statement such as Template Strings. There is a easy workaround: use the Template Strings outside getProp():

var newState = iassign(
    myState,
    (s, context) => s.questions[context.pageId],
    (questions) => {
      questions.splice(insertAction.position, 0, insertAction.question);
      return questions;
    },
    { pageId: `page_${insertAction.pageId}` }
);

That worked. Thanks 😄