diff --git a/app/assets/javascripts/components/wizard/form_panel.jsx b/app/assets/javascripts/components/wizard/form_panel.jsx index 142e2d92bd..51ab8884fc 100644 --- a/app/assets/javascripts/components/wizard/form_panel.jsx +++ b/app/assets/javascripts/components/wizard/form_panel.jsx @@ -1,144 +1,148 @@ -import React from 'react'; -import createReactClass from 'create-react-class'; +import React, { useRef, useCallback } from 'react'; import PropTypes from 'prop-types'; import Panel from './panel.jsx'; import DatePicker from '../common/date_picker.jsx'; import Calendar from '../common/calendar.jsx'; import CourseDateUtils from '../../utils/course_date_utils.js'; -const FormPanel = createReactClass({ - displayName: 'FormPanel', - - propTypes: { - course: PropTypes.object.isRequired, - shouldShowSteps: PropTypes.bool, - updateCourse: PropTypes.func.isRequired, - isValid: PropTypes.bool.isRequired - }, +// Utility function for safe property access and transformation +function __guard__(value, transform) { + return (typeof value !== 'undefined' && value !== null) ? transform(value) : undefined; +} - setAnyDatesSelected(bool) { - return this.setState({ anyDatesSelected: bool }); - }, +const FormPanel = ({ course, shouldShowSteps, updateCourse, isValid, persistCourse, noDates }) => { + const noDatesRef = useRef(null); - setBlackoutDatesSelected(bool) { - return this.setState({ blackoutDatesSelected: bool }); - }, - setNoBlackoutDatesChecked() { - const { checked } = this.noDates; - const toPass = this.props.course; - toPass.no_day_exceptions = checked; - return this.props.updateCourse(toPass); - }, + const setNoBlackoutDatesChecked = useCallback(() => { + const { checked } = noDatesRef.current; + const toPass = { ...course, no_day_exceptions: checked }; + updateCourse(toPass); + }, [course, updateCourse]); - updateCourseDates(valueKey, value) { - const updatedCourse = CourseDateUtils.updateCourseDates(this.props.course, valueKey, value); - return this.props.updateCourse(updatedCourse); - }, + const updateCourseDates = useCallback((valueKey, value) => { + const updatedCourse = CourseDateUtils.updateCourseDates(course, valueKey, value); + updateCourse(updatedCourse); + }, [course, updateCourse]); - saveCourse() { - if (this.props.isValid) { - this.props.persistCourse(this.props.course.slug); + const saveCourse = useCallback(() => { + if (isValid) { + persistCourse(course.slug); return true; } alert(I18n.t('error.form_errors')); return false; - }, - nextEnabled() { - if (__guard__(this.props.course.weekdays, x => x.indexOf(1)) >= 0 && (__guard__(this.props.course.day_exceptions, x1 => x1.length) > 0 || this.props.course.no_day_exceptions)) { - return true; - } - return false; - }, + }, [isValid, persistCourse, course.slug]); - render() { - const dateProps = CourseDateUtils.dateProps(this.props.course); + const nextEnabled = useCallback(() => { + return (__guard__(course.weekdays, x => x.indexOf(1)) >= 0) + && (__guard__(course.day_exceptions, x => x.length) > 0 || course.no_day_exceptions); + }, [course.weekdays, course.day_exceptions, course.no_day_exceptions]); - const step1 = this.props.shouldShowSteps - ?
Confirm the course’s start and end dates.
; + const dateProps = CourseDateUtils.dateProps(course); - const rawOptions = ( -{I18n.t('wizard.assignment_description')}
-{I18n.t('wizard.confirm_dates')}
; + + const rawOptions = ( +{I18n.t('wizard.assignment_description')}
+