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

Autoscroll Course Planner to current year card #3640

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
20 changes: 19 additions & 1 deletion website/src/views/planner/PlannerYear.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PureComponent } from 'react';
import { PureComponent, Ref, createRef } from 'react';
import classnames from 'classnames';
import { flatMap, size, sortBy, toPairs, values } from 'lodash';

Expand All @@ -24,14 +24,31 @@

type State = {
readonly showSpecialSem: boolean;
readonly currentYearCardRef: Ref<HTMLDivElement>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ref should not be stored in state, see https://react.dev/reference/react/createRef

};

export default class PlannerYear extends PureComponent<Props, State> {
override state = {
// Always display Special Terms I and II if either one has modules
showSpecialSem: this.hasSpecialTermModules(),
currentYearCardRef: createRef<HTMLDivElement>(),
};

override componentDidMount() {
if (this.props.year !== config.academicYear) {
return;

Check warning on line 39 in website/src/views/planner/PlannerYear.tsx

View check run for this annotation

Codecov / codecov/patch

website/src/views/planner/PlannerYear.tsx#L37-L39

Added lines #L37 - L39 were not covered by tests
}
const currentYearCard = this.state.currentYearCardRef.current;
const parentContainer = currentYearCard?.parentElement;
if (!currentYearCard || !parentContainer) {
return;

Check warning on line 44 in website/src/views/planner/PlannerYear.tsx

View check run for this annotation

Codecov / codecov/patch

website/src/views/planner/PlannerYear.tsx#L41-L44

Added lines #L41 - L44 were not covered by tests
}
parentContainer.scrollTo({

Check warning on line 46 in website/src/views/planner/PlannerYear.tsx

View check run for this annotation

Codecov / codecov/patch

website/src/views/planner/PlannerYear.tsx#L46

Added line #L46 was not covered by tests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could consider using scrollIntoView instead, that would avoid having to do math to figure out exact offset

left: currentYearCard.offsetLeft - parentContainer.offsetLeft,
behavior: 'smooth',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For scrolling on component mount you probably don't want smooth, it's better for the component to appear already scrolled to the correct location.

});
}

hasSpecialTermModules() {
const { semesters } = this.props;
return size(semesters[3]) > 0 || size(semesters[4]) > 0;
Expand Down Expand Up @@ -72,6 +89,7 @@

return (
<section
ref={year === config.academicYear ? this.state.currentYearCardRef : undefined}

Check warning on line 92 in website/src/views/planner/PlannerYear.tsx

View check run for this annotation

Codecov / codecov/patch

website/src/views/planner/PlannerYear.tsx#L92

Added line #L92 was not covered by tests
key={year}
className={classnames(styles.year, {
[styles.currentYear]: year === config.academicYear,
Expand Down