-
Notifications
You must be signed in to change notification settings - Fork 657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix State Mutation in Timeline Reducer and Refactor TimelineHandler for Immutable Week Title Updates #6144
Fix State Mutation in Timeline Reducer and Refactor TimelineHandler for Immutable Week Title Updates #6144
Conversation
@Abishekcs sure, seems reasonable. |
44ba6da
to
8e6a3a2
Compare
This seems fine to me, but since it's introducing a new tool, the PR should probably include some explanation of it in frontend.md. Also, as part of the PR description, please note how adding this package affect the bundle size. |
@@ -6866,7 +6867,7 @@ __metadata: | |||
languageName: node | |||
linkType: hard | |||
|
|||
"immer@npm:^10.0.3": | |||
"immer@npm:^10.0.3, immer@npm:^10.1.1": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since it looks like we're already using a different version of immer, is it possible to just use the newer version without the older one (possibly by upgrading whatever dependency was already requiring it)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ragesoss i think both version ranges (^10.0.3 and ^10.1.1) are being satisfied by a single installation of version 10.1.1. as shown in yarn .
Line 6871 in 8e6a3a2
version: 10.1.1 |
So there's only one version installed, and it satisfies both requirements.
And, while installing Redux Toolkit (RTK) in PR #5867 , it brought in immer as a dependency since RTK uses immer internally for its immutable state updates.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great. So it shouldn't affect the bundle size either.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great. So it shouldn't affect the bundle size either.
Yes.
Okay |
…T_TITLES' and 'UPDATE_TITLE' actions
…fore saving timeline after resetting week titles immutably
8e6a3a2
to
55c4061
Compare
@ragesoss i have updated the frontend.md. a) There, is one link i.e Thinking in React which leads to page not found. |
Thanks! This looks good to me; I'll merge once the tests pass. |
Note
Previously, I considered opening this as two separate PRs (#6138 and #6143). However, later i realized both require a common component change, specifically the TimelineHandler component.
For testing please set
enableMutationChecks
to true in create_store.jsWhat this PR does
getWeeksArray
selector.RESET_TITLES
&UPDATE_TITLE
Cause of Bug (For
getWeeksArray
selector)newWeek object
, which is a reference to theweeks
property of theRedux state (Timeline Slice).
WikiEduDashboard/app/assets/javascripts/selectors/index.js
Line 257 in 88a89ec
Fix (For
getWeeksArray
selector)spread operator
since we are modifying only the top level properties of the object.Cause of Bug (For
RESET_TITLES
&UPDATE_TITLE
)For this finding the cause of bug was easy as the error message exaclty indicated where mutation was as shown below:
For
RESET_TITLES
:For
UPDATE_TITLE
The spread operator doesn’t work as expected with deeply nested objects because it copies the values of the top-level properties of weeks to new addresses/references, but the inner objects retain the same addresses/references which are the one's updated by mutation.
Fix (For
RESET_TITLES
&UPDATE_TITLE
)Resolved by using
Immer Produce
.Why the changes in TimelineHandler component?
Previously, the
getWeeksArray
selector was mutating theweeks
property of thetimeline slice
directly. This meant that dispatchingSAVED_TIMELINE
from thesaveTimeline
function immediately after dispatchingRESET_TITLES
from the_resetTitles
function worked without issues because both were referencing the same mutated weeks array fromgetWeeksArray
selector.So, after updating immutable way immediately dispatching won't work as the existing weeks in the TimlineHandler from selector doesn't have the same reference.
Little bit detail about redux for future reference:
any code holding that reference sees the changes instantly.
making the changes not immediately available. Also, after resetting the timeline week titles, the saveTimeline
was called immediately, leading to an API call right away. During this time, Redux had not yet received the
new updates.
Screenshots (For
getWeeksArray
selector)Before:
Before.mp4
After:
After.mp4
Screenshots (For
RESET_TITLES
&UPDATE_TITLE
)Before:
Before.mp4
After:
After.mp4