Skip to content

Commit

Permalink
auto-fill course for enrollment
Browse files Browse the repository at this point in the history
  • Loading branch information
Jdyn committed Sep 3, 2023
1 parent 0ddc935 commit 39c8bbd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
36 changes: 24 additions & 12 deletions frontend/src/components/ClassSearchBar/EnrollmentSearchBar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand All @@ -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({
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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={{
Expand Down Expand Up @@ -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
};
};

Expand Down
3 changes: 1 addition & 2 deletions frontend/src/components/ClassSearchBar/GradesSearchBar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
}

Expand Down
2 changes: 0 additions & 2 deletions frontend/src/views/Enrollment/Enrollment.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -133,7 +132,6 @@ class Enrollment extends Component {
<EnrollmentSearchBar
classes={courses}
addCourse={this.addCourse}
fromCatalog={location.state ? location.state.course : false}
isFull={selectedCourses.length === 4}
isMobile={isMobile}
/>
Expand Down

0 comments on commit 39c8bbd

Please sign in to comment.