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.