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

Add new Button on Course Page to Add Course to Planner #3731

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
34 changes: 34 additions & 0 deletions website/src/views/components/module-info/SaveModuleButton.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
@import "~styles/utils/modules-entry";

.buttonGroup {
:global(.btn) {
line-height: 1.2;
}

strong {
font-size: 1.2em;
}

:global(.dropdown-toggle.dropdown-toggle-split) {
width: 2rem;
border-left: none;

@include media-breakpoint-down(sm) {
// Long selector for specificity
width: 3rem;
}
}
Comment on lines +12 to +20
Copy link
Member

Choose a reason for hiding this comment

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

Global seems like quite a large scope, do you know if this styling is actually applied on the button?

}

.dropdownItem {
text-align: center;
white-space: normal;

br {
display: none;
}

strong {
font-size: 1em;
}
}
Comment on lines +23 to +34
Copy link
Member

Choose a reason for hiding this comment

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

Seems to be unused?

129 changes: 129 additions & 0 deletions website/src/views/components/module-info/SaveModuleButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { PureComponent } from 'react';
import classnames from 'classnames';
import { connect } from 'react-redux';

import { State as StoreState } from 'types/state';

import { Module, Semester } from 'types/modules';
import { getFirstAvailableSemester } from 'utils/modules';

import { AddModuleData, PlannerModuleInfo } from 'types/planner';
import { getPlanToTake } from 'selectors/planner';
import { addPlannerModule, removePlannerModule } from 'actions/planner';

import styles from './SaveModuleButton.scss';

type Props = {
module: Module;
planToTakeModules: PlannerModuleInfo[];
className?: string;
block?: boolean;

addModule: (year: string, semester: Semester, module: AddModuleData) => void;
removeModule: (id: string) => void;
};

type State = {
loading: Semester | null;
};

function isModuleInPlanToTake(module: Module, planToTakeModules: PlannerModuleInfo[]): boolean {
for (let i = 0; i < planToTakeModules.length; i++) {
if (planToTakeModules[i].moduleCode === module.moduleCode) {
return true;

Check warning on line 33 in website/src/views/components/module-info/SaveModuleButton.tsx

View check run for this annotation

Codecov / codecov/patch

website/src/views/components/module-info/SaveModuleButton.tsx#L32-L33

Added lines #L32 - L33 were not covered by tests
}
}
Comment on lines +31 to +35
Copy link
Member

Choose a reason for hiding this comment

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

Is it possible to use a .some call here instead?

e.g. planToTakeModules.some(m => m.moduleCode === module.moduleCode)


return false;
}

function getModuleId(module: Module, planToTakeModules: PlannerModuleInfo[]): string {

Check warning on line 40 in website/src/views/components/module-info/SaveModuleButton.tsx

View check run for this annotation

Codecov / codecov/patch

website/src/views/components/module-info/SaveModuleButton.tsx#L40

Added line #L40 was not covered by tests
// If duplicate ids, gets the first id.
for (let i = 0; i < planToTakeModules.length; i++) {
if (planToTakeModules[i].moduleCode === module.moduleCode) {
return planToTakeModules[i].id;

Check warning on line 44 in website/src/views/components/module-info/SaveModuleButton.tsx

View check run for this annotation

Codecov / codecov/patch

website/src/views/components/module-info/SaveModuleButton.tsx#L42-L44

Added lines #L42 - L44 were not covered by tests
}
}
Comment on lines +42 to +46
Copy link
Member

Choose a reason for hiding this comment

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

Is it possible to use a .find call here instead?

e.g. planToTakeModules.find(m => m.moduleCode === module.moduleCode)?.id ?? '0'

return '0';

Check warning on line 47 in website/src/views/components/module-info/SaveModuleButton.tsx

View check run for this annotation

Codecov / codecov/patch

website/src/views/components/module-info/SaveModuleButton.tsx#L47

Added line #L47 was not covered by tests
}

export class SaveModuleButtonComponent extends PureComponent<Props, State> {
static getDerivedStateFromProps(nextProps: Props, prevState: State) {
const { planToTakeModules, module } = nextProps;
const { loading } = prevState;

if (loading != null && isModuleInPlanToTake(module, planToTakeModules)) {
return { loading: null };

Check warning on line 56 in website/src/views/components/module-info/SaveModuleButton.tsx

View check run for this annotation

Codecov / codecov/patch

website/src/views/components/module-info/SaveModuleButton.tsx#L56

Added line #L56 was not covered by tests
}

return null;
}

override state: State = {
loading: null,
};

onSelect(semester: Semester) {
const { module, planToTakeModules } = this.props;
const PLAN_TO_TAKE_YEAR = '3000';
const PLAN_TO_TAKE_SEMESTER = -2;

Check warning on line 69 in website/src/views/components/module-info/SaveModuleButton.tsx

View check run for this annotation

Codecov / codecov/patch

website/src/views/components/module-info/SaveModuleButton.tsx#L66-L69

Added lines #L66 - L69 were not covered by tests
Comment on lines +68 to +69
Copy link
Member

Choose a reason for hiding this comment

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

Is it possible for us to import these constants from `src/utils/planner.ts' instead?

Or better yet, create a new function that more accurately represents what this code is trying to do, i.e., add a module so far into the future that it will definitely be in the "plan to take" section (correct me if this not what the code is trying to do)?


if (isModuleInPlanToTake(module, planToTakeModules)) {
const id = getModuleId(module, planToTakeModules);
this.props.removeModule(id);
} else {
this.setState({ loading: semester });
this.props.addModule(PLAN_TO_TAKE_YEAR, PLAN_TO_TAKE_SEMESTER, {

Check warning on line 76 in website/src/views/components/module-info/SaveModuleButton.tsx

View check run for this annotation

Codecov / codecov/patch

website/src/views/components/module-info/SaveModuleButton.tsx#L71-L76

Added lines #L71 - L76 were not covered by tests
type: 'module',
moduleCode: module.moduleCode,
});
}
}

buttonLabel() {
const hasModule = isModuleInPlanToTake(this.props.module, this.props.planToTakeModules);
return hasModule ? (
<>

Check warning on line 86 in website/src/views/components/module-info/SaveModuleButton.tsx

View check run for this annotation

Codecov / codecov/patch

website/src/views/components/module-info/SaveModuleButton.tsx#L86

Added line #L86 was not covered by tests
Remove from <br />
<strong>Plan To Take</strong>
</>
) : (
<>
Add to <br />
<strong>Plan To Take</strong>
</>
);
}

override render() {
const { block, className, module } = this.props;
const defaultSemester = getFirstAvailableSemester(module.semesterData);

return (
<div
className={classnames('btn-group', styles.buttonGroup, className, {
'btn-block': block,
})}
>
<button
type="button"
className={classnames('btn btn-outline-primary', {
'btn-block': block,
})}
onClick={() => this.onSelect(defaultSemester)}

Check warning on line 113 in website/src/views/components/module-info/SaveModuleButton.tsx

View check run for this annotation

Codecov / codecov/patch

website/src/views/components/module-info/SaveModuleButton.tsx#L113

Added line #L113 was not covered by tests
>
{this.buttonLabel()}
</button>
</div>
);
}
}

const SaveModuleButtonConnected = connect(
(state: StoreState) => ({
planToTakeModules: getPlanToTake(state),
}),
{ addModule: addPlannerModule, removeModule: removePlannerModule },
)(SaveModuleButtonComponent);

export default SaveModuleButtonConnected;
7 changes: 7 additions & 0 deletions website/src/views/modules/ModulePageContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import ModuleExamClash from 'views/components/module-info/ModuleExamClash';
import ModuleWorkload from 'views/components/module-info/ModuleWorkload';
import ModuleExamInfo from 'views/components/module-info/ModuleExamInfo';
import AddModuleDropdown from 'views/components/module-info/AddModuleDropdown';
import SaveModuleButton from 'views/components/module-info/SaveModuleButton';
import Announcements from 'views/components/notfications/Announcements';
import Title from 'views/components/Title';

Expand Down Expand Up @@ -252,6 +253,12 @@ const ModulePageContent: React.FC<Props> = ({ module, archiveYear }) => {
</div>
)}

{!isArchive && offered && (
<div className={styles.addToTimetable}>
<SaveModuleButton module={module} className="btn-group-sm" block />
</div>
)}

<p>
<ReportError module={module} />
</p>
Expand Down