Skip to content

Commit

Permalink
Merge pull request #6120 from Abishekcs/fix/redux-state-mutation
Browse files Browse the repository at this point in the history
Fix: Prevent state mutation in course wikis update logic
  • Loading branch information
ragesoss authored Jan 15, 2025
2 parents 63fd7d7 + 5840b60 commit 12918cb
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions app/assets/javascripts/components/course_creator/course_form.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import React, { useEffect } from 'react';
import { useDispatch } from 'react-redux';
import { Link } from 'react-router-dom';
import TextAreaInput from '../common/text_area_input.jsx';
import CreatableInput from '../common/creatable_input.jsx';
Expand All @@ -12,6 +13,7 @@ import selectStyles from '../../styles/select';
import WikiSelect from '../common/wiki_select.jsx';
import AcademicSystem from '../common/academic_system.jsx';
import WIKI_OPTIONS from '../../utils/wiki_options';
import { updateCourse } from '@actions/course_actions.js';

const CourseForm = (props) => {
const handleWikiChange = (wiki) => {
Expand All @@ -31,6 +33,15 @@ const CourseForm = (props) => {
props.updateCourseProps({ wikis });
};

const dispatch = useDispatch();

useEffect(() => {
if (props.course.wikis && !props.course.wikis.length) {
dispatch(updateCourse({ wikis: [{ language: 'en', project: 'wikipedia' }] }));
}
}, []);


let term;
let courseSubject;
let expectedStudents;
Expand Down Expand Up @@ -133,17 +144,11 @@ const CourseForm = (props) => {
);
}



let privacyCheckbox;
let campaign;
let home_wiki;
let multi_wiki;

if (props.course.wikis && !props.course.wikis.length) {
props.course.wikis.push({ language: 'en', project: 'wikipedia' });
}

if (props.defaultCourse !== 'ClassroomProgramCourse') {
home_wiki = (
<div className="form-group home-wiki">
Expand Down Expand Up @@ -181,6 +186,7 @@ const CourseForm = (props) => {
/>
);
}

if (props.course.initial_campaign_title) {
campaign = (
<TextInput
Expand All @@ -189,7 +195,9 @@ const CourseForm = (props) => {
/>
);
}

let backCondition;

if (Features.wikiEd) {
backCondition = props.previousWikiEd;
} else {
Expand All @@ -198,15 +206,15 @@ const CourseForm = (props) => {

let backOrCancelButton;

// Displays "Back" button if the user has clonable courses or is on the P&E dashboard; otherwise shows "Cancel" link.
if (props.hasClonableCourses || props.defaultCourse !== 'ClassroomProgramCourse') {
backOrCancelButton = (
<button onClick={backCondition} className="button dark">{I18n.t('application.back')}</button>
);
} else {
backOrCancelButton = (
<Link className="button" to="/" id="course_cancel">{I18n.t('application.cancel')}</Link>
);
// Displays "Back" button if the user has clonable courses or is on the P&E dashboard; otherwise shows "Cancel" link.
if (props.hasClonableCourses || props.defaultCourse !== 'ClassroomProgramCourse') {
backOrCancelButton = (
<button onClick={backCondition} className="button dark">{I18n.t('application.back')}</button>
);
} else {
backOrCancelButton = (
<Link className="button" to="/" id="course_cancel">{I18n.t('application.cancel')}</Link>
);
}

return (
Expand Down

0 comments on commit 12918cb

Please sign in to comment.