0

I have been trying to get this code to execute in order but for some reason the asssignments action.payload.subject.position.year = action.payload.year; action.payload.subject.position.quarter = action.payload.quarter; execute before even the console.log at the top of the function. This is messing things on alot of levels across my code. I have always understood JS as being interpreted and top to bottom but this is giving me alot of headaches.

case ACTIONS.ADD_SUBJECT_TO_QUARTER:
  {
    const yearProp    = `year${action.payload.year}`;
    const quarterProp = `quarter${action.payload.quarter}`;
    console.log(
      "ADD_SUBJECT_TO_QUARTER",
      curricullum[yearProp][quarterProp].subjects,
      action.payload.subject
    );
    if (!curricullum[yearProp][quarterProp].subjects.some(
        (subject: any) => subject.id === action.payload.subject.id
      )) {
      if (
        curricullum[yearProp][quarterProp].totalCredits
        + action.payload.subject.credit >  15
      ) {
        alert("You can't add more than 15 credits in a quarter");
        return curricullum;
      }

      action.payload.subject.position.year = action.payload.year;
      action.payload.subject.position.quarter = action.payload.quarter;

      console.log("position", action.payload.subject.position);

      return {
        ...curricullum,
        [yearProp]: {
          ...curricullum[yearProp],
          [quarterProp]: {
            ...curricullum[yearProp][quarterProp],
            subjects: [
              ...curricullum[yearProp][quarterProp].subjects,
              action.payload.subject,
            ],
            totalCredits: curricullum[yearProp][quarterProp].totalCredits +
              action.payload.subject.credit,
          },
        },
        subjectList: {
          subjects: curricullum.subjectList.subjects.filter(
            (subject: any) => subject.id !== action.payload.subject.id
          ),
        },
      };
    } else {
      return curricullum;
    }
  }

This is the colnsole.log that i get in the browser; the important part is that position year and quarter should be 0 at that point as the console.log happens before the assignment


{id: 1, language: 'English', credit: 15, courseName: 'OOP', version: '5', …}
courseName
: 
"OOP"
credit
: 
15
description
: 
"Object Oriented Programming"
id
: 
1
language
: 
"English"
learningGoalIds
: 
undefined
position
: 
{year: 1, quarter: 1}
version
: 
"5"
[[Prototype]]
: 
Object

Update: Okay figured out that using JSON.stringify would solve the console.log problem, but the main problem remains with this

case ACTIONS.ADD_SUBJECT_TO_SUBJECTLIST:
      console.log("ADD_SUBJECT_TO_SUBJECTLIST", action.payload.subject);

  const yearProp = `year${action.payload.subject.position.year}`;
  const quarterProp = `quarter${action.payload.subject.position.quarter}`;

  action.payload.subject.position.year = 0;
  action.payload.subject.position.quarter = 0;

  return {
    ...curricullum,
    subjectList: {
      subjects: [
        ...curricullum.subjectList.subjects,
        action.payload.subject,
      ],
    },
    [yearProp]: {
      ...curricullum[yearProp],
      [quarterProp]: {
        ...curricullum[yearProp][quarterProp],
        subjects: curricullum[yearProp][quarterProp].subjects.filter(
          (subject: any) => subject.id !== action.payload.subject.id
        ),
      },
    },
  };```

yearprop and quarterprop are updated after the assignments in the lines below them and that makes it impossible to access the values. Basically I need to make year and quarter equal to 0 while at the same time keeping a record of what they were befroe to use it in the return function.

VladSavu
  • 31
  • 4
  • Can you post your console output? – tromgy Jan 29 '23 at 17:31
  • It is running top to bottom, you're seeing updated values because your browser shows the values at the time of expanding the object when printing an object or array. Try logging `JSON.stringify(action.payload.subject)` – tkausl Jan 29 '23 at 18:16
  • @tkausl can you then explain a bit more the behaviour in the update? – VladSavu Jan 29 '23 at 18:25
  • Try to avoid reassigning in the arguments. JavaScript can be a bit [surprising](https://stackoverflow.com/a/3638034/17519505) when you do. Too many spreaders and implementing structural sharing on your own can be cumbersome; have a look at libraries like [immutability-helper](https://github.com/kolodny/immutability-helper) or [immer.js](https://immerjs.github.io/immer/) – olle Jan 29 '23 at 21:08

0 Answers0