From b8abe251d466944b45fdc179fa15f25672bcf720 Mon Sep 17 00:00:00 2001 From: Jaden Date: Sun, 3 Sep 2023 11:00:07 -0700 Subject: [PATCH] auto-fill course for enrollment --- .../ClassSearchBar/EnrollmentSearchBar.jsx | 36 ++++++++++++------- .../ClassSearchBar/GradesSearchBar.jsx | 3 +- frontend/src/views/Enrollment/Enrollment.jsx | 2 -- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/frontend/src/components/ClassSearchBar/EnrollmentSearchBar.jsx b/frontend/src/components/ClassSearchBar/EnrollmentSearchBar.jsx index 65441119c..3faf13706 100644 --- a/frontend/src/components/ClassSearchBar/EnrollmentSearchBar.jsx +++ b/frontend/src/components/ClassSearchBar/EnrollmentSearchBar.jsx @@ -13,7 +13,8 @@ class EnrollmentSearchBar extends Component { this.state = { selectedClass: 0, selectPrimary: this.props.selectPrimary, - selectSecondary: this.props.selectSecondary + selectSecondary: this.props.selectSecondary, + selectedClassValue: undefined }; this.queryCache = {}; @@ -29,13 +30,6 @@ class EnrollmentSearchBar extends Component { this.reset = this.reset.bind(this); } - componentDidMount() { - let { fromCatalog } = this.props; - if (fromCatalog) { - this.handleClassSelect({ value: fromCatalog.id, addSelected: true }); - } - } - UNSAFE_componentWillReceiveProps(nextProps) { if (nextProps.selectPrimary !== this.state.selectPrimary) { this.setState({ @@ -49,6 +43,23 @@ class EnrollmentSearchBar extends Component { } } + componentDidUpdate(prevProps) { + const { selectedCourses, fetchEnrollSelected } = this.props; + + if ( + prevProps.selectedCourses != selectedCourses && + Array.isArray(selectedCourses) && + selectedCourses.length > 0 + ) { + const course = selectedCourses[selectedCourses.length - 1]; + const payload = { value: course.courseID, label: course.course, course }; + + fetchEnrollSelected(payload); + + this.setState({ selectedClassValue: payload, selectedClass: payload.value }); + } + } + handleClassSelect(updatedClass) { const { fetchEnrollSelected } = this.props; if (updatedClass === null) { @@ -210,7 +221,7 @@ class EnrollmentSearchBar extends Component { render() { const { classes, isFull, sections, isMobile } = this.props; - const { selectPrimary, selectSecondary, selectedClass } = this.state; + const { selectPrimary, selectSecondary, selectedClass, selectedClassValue } = this.state; let primaryOptions = this.buildPrimaryOptions(sections); let secondaryOptions = this.buildSecondaryOptions(sections, selectPrimary); let onePrimaryOption = primaryOptions && primaryOptions.length === 1 && selectPrimary; @@ -247,7 +258,7 @@ class EnrollmentSearchBar extends Component { courseSearch name="selectClass" placeholder="Choose a class..." - // value={selectedClass} + value={selectedClassValue} options={this.buildCoursesOptions(classes)} onChange={this.handleClassSelect} components={{ @@ -309,11 +320,12 @@ const mapDispatchToProps = (dispatch) => { }; const mapStateToProps = (state) => { - const { sections, selectPrimary, selectSecondary } = state.enrollment; + const { sections, selectPrimary, selectSecondary, selectedCourses } = state.enrollment; return { sections, selectPrimary, - selectSecondary + selectSecondary, + selectedCourses }; }; diff --git a/frontend/src/components/ClassSearchBar/GradesSearchBar.jsx b/frontend/src/components/ClassSearchBar/GradesSearchBar.jsx index 8eda9e847..ac5ece8d9 100644 --- a/frontend/src/components/ClassSearchBar/GradesSearchBar.jsx +++ b/frontend/src/components/ClassSearchBar/GradesSearchBar.jsx @@ -66,10 +66,9 @@ class GradesSearchBar extends Component { const course = selectedCourses[selectedCourses.length - 1]; const payload = { value: course.courseID, label: course.course, course }; - this.setState({ selectedClass: payload.value }); fetchGradeSelected(payload); - this.setState({ selectedClassValue: payload }) + this.setState({ selectedClassValue: payload, selectedClass: payload.value }); } } diff --git a/frontend/src/views/Enrollment/Enrollment.jsx b/frontend/src/views/Enrollment/Enrollment.jsx index ca4e5b146..5daa1167d 100644 --- a/frontend/src/views/Enrollment/Enrollment.jsx +++ b/frontend/src/views/Enrollment/Enrollment.jsx @@ -124,7 +124,6 @@ class Enrollment extends Component { render() { const { additionalInfo } = this.state; const { context, selectedCourses, isMobile } = this.props; - let { location } = this.props; let courses = context.courses; return ( @@ -133,7 +132,6 @@ class Enrollment extends Component {