-
Notifications
You must be signed in to change notification settings - Fork 319
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
} | ||
} | ||
} | ||
|
||
.dropdownItem { | ||
text-align: center; | ||
white-space: normal; | ||
|
||
br { | ||
display: none; | ||
} | ||
|
||
strong { | ||
font-size: 1em; | ||
} | ||
} | ||
Comment on lines
+23
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems to be unused? |
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; | ||
} | ||
} | ||
Comment on lines
+31
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to use a e.g. |
||
|
||
return false; | ||
} | ||
|
||
function getModuleId(module: Module, planToTakeModules: PlannerModuleInfo[]): string { | ||
// 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; | ||
} | ||
} | ||
Comment on lines
+42
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to use a e.g. |
||
return '0'; | ||
} | ||
|
||
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 }; | ||
} | ||
|
||
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; | ||
Comment on lines
+68
to
+69
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, { | ||
type: 'module', | ||
moduleCode: module.moduleCode, | ||
}); | ||
} | ||
} | ||
|
||
buttonLabel() { | ||
const hasModule = isModuleInPlanToTake(this.props.module, this.props.planToTakeModules); | ||
return hasModule ? ( | ||
<> | ||
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)} | ||
> | ||
{this.buttonLabel()} | ||
</button> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
const SaveModuleButtonConnected = connect( | ||
(state: StoreState) => ({ | ||
planToTakeModules: getPlanToTake(state), | ||
}), | ||
{ addModule: addPlannerModule, removeModule: removePlannerModule }, | ||
)(SaveModuleButtonComponent); | ||
|
||
export default SaveModuleButtonConnected; |
There was a problem hiding this comment.
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?