Skip to content
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: Prevent state mutation in fetchRevisionsPromise function #6112

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix: Prevent state mutation in fetchRevisionsPromise function
- Avoid direct mutation of `course.revisions` by creating a new object with updated revisions.
- Ensures compliance with Redux's immutability rules to avoid errors during state updates.
Abishekcs committed Jan 13, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit a5fb923439ddfa733452dc06f7d8061baa822d18
14 changes: 10 additions & 4 deletions app/assets/javascripts/actions/revisions_actions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import {
RECEIVE_REVISIONS,
REVISIONS_LOADING,
@@ -35,12 +34,19 @@ const fetchAllArticles = async (course) => {

const fetchRevisionsPromise = async (course, users, last_date, dispatch) => {
const { revisions, last_date: new_last_date } = await fetchRevisionsFromUsers(course, users, 7, last_date);
course.revisions = sortRevisionsByDate(revisions);

// we don't await this. When the assessments/references get laoded, the action is dispatched
// Create a new course object with updated revisions
const updatedCourse = {
...course,
revisions: sortRevisionsByDate(revisions),
};

// we don't await this. When the assessments/references get loaded, the action is dispatched
fetchRevisionsAndReferences(revisions, dispatch);
return { course, last_date: new_last_date };

return { course: updatedCourse, last_date: new_last_date };
};

const fetchRevisionsCourseSpecificPromise = async (course, users, last_date, dispatch, articles) => {
const trackedArticles = new Set(
articles.filter(article => article.tracked).map(article => article.title)

Unchanged files with check annotations Beta

export const TitleOpener = ({ showArticle, showButtonClass, showButtonLabel, title }) => (
<div className={`tooltip-trigger ${showButtonClass || ''}`}>
<button style={{ textAlign: 'left' }} onClick={showArticle} aria-describedby="icon-article-viewer-desc">{title}</button>
<p id="icon-article-viewer-desc">Open Article Viewer</p>

Check warning on line 7 in app/assets/javascripts/components/common/ArticleViewer/components/TitleOpener.jsx

GitHub Actions / build

Use I18n over string literals for localization: <p id="icon-article-viewer-desc">Open Article Viewer</p>
<div className="tooltip tooltip-title dark large">
<p>{showButtonLabel()}</p>
</div>

Check warning on line 10 in app/assets/javascripts/components/common/AssignmentLinks/AllPeerReviewLinks.jsx

GitHub Actions / build

Use I18n over string literals for localization: <span key="title">Reviews: </span>
let requestedAccountsLink;
if (!Features.wikiEd) {
const massEnrollmentUrl = `/mass_enrollment/${course.slug}`;
massEnrollmentLink = <p><a href={massEnrollmentUrl}>Add multiple users at once.</a></p>;

Check warning on line 132 in app/assets/javascripts/components/common/enroll_button.jsx

GitHub Actions / build

Use I18n over string literals for localization: <a href={massEnrollmentUrl}>Add multiple users at once.</a>
}
if (!Features.wikiEd) {
const requestedAccountsUrl = `/requested_accounts/${course.slug}`;
let button;
if (isVisible) {
button = <button onClick={hide} className="button dark small">Okay</button>;

Check warning on line 41 in app/assets/javascripts/components/common/final_article_checklist.jsx

GitHub Actions / build

Use I18n over string literals for localization: <button onClick={hide} className="button dark small">Okay</button>
} else {
button = <a onClick={show} className="button dark small">Quality checklist</a>;

Check warning on line 43 in app/assets/javascripts/components/common/final_article_checklist.jsx

GitHub Actions / build

Use I18n over string literals for localization: <a onClick={show} className="button dark small">Quality checklist</a>
}
let modal;
} else {
modal = (
<div ref={ref} className="article-viewer my-assignment-checklist">
<h2>Final review checklist</h2>

Check warning on line 52 in app/assets/javascripts/components/common/final_article_checklist.jsx

GitHub Actions / build

Use I18n over string literals for localization: <h2>Final review checklist</h2>
<p>
As you polish up your completed article, review each of these items and fix any problems you find.
</p>
const wikipediaHelpUser = this.wikipediaHelpUser();
wikipediaHelpButton = (
<span className="contact-wikipedia-help" key={`${wikipediaHelpUser.username}-wikipedia-help`}>
<a href="#" className="wikipedia-help-link button dark small stacked" onClick={e => this.updateTargetUser(wikipediaHelpUser, e)}>question about editing Wikipedia</a>

Check warning on line 114 in app/assets/javascripts/components/common/get_help_button.jsx

GitHub Actions / build

Use I18n over string literals for localization: <a href="#" className="wikipedia-help-link button dark small stacked" onClick={e => this.updateTargetUser(wikipediaHelpUser, e)}>question about editing Wikipedia</a>
<br />
</span>
);
const programHelpUser = this.programHelpUser();
programHelpButton = (
<span className="contact-program-help" key={`${programHelpUser.username}-program-help`}>
<a href="#" className="program-help-link button dark stacked small" onClick={e => this.updateTargetUser(programHelpUser, e)}>question about Wiki Ed or your assignment</a>

Check warning on line 124 in app/assets/javascripts/components/common/get_help_button.jsx

GitHub Actions / build

Use I18n over string literals for localization: <a href="#" className="program-help-link button dark stacked small" onClick={e => this.updateTargetUser(programHelpUser, e)}>question about Wiki Ed or your assignment</a>
<br />
</span>
);
const dashboardHelpUser = this.dashboardHelpUser();
dashboardHelpButton = (
<span className="contact-dashboard-help" key={`${dashboardHelpUser.username}-dashboard-help`}>
<a href="#" className="dashboard-help-link button dark stacked small" onClick={e => this.updateTargetUser(dashboardHelpUser, e)}>question about the dashboard</a>

Check warning on line 134 in app/assets/javascripts/components/common/get_help_button.jsx

GitHub Actions / build

Use I18n over string literals for localization: <a href="#" className="dashboard-help-link button dark stacked small" onClick={e => this.updateTargetUser(dashboardHelpUser, e)}>question about the dashboard</a>
<br />
</span>
);
let button;
if (isVisible) {
button = <button onClick={hide} className="button dark small">Okay</button>;

Check warning on line 20 in app/assets/javascripts/components/common/mainspace_checklist.jsx

GitHub Actions / build

Use I18n over string literals for localization: <button onClick={hide} className="button dark small">Okay</button>
} else {
button = <a onClick={show} className="button dark small">Quality checklist</a>;
}