diff --git a/website/src/utils/timeOptions.test.ts b/website/src/utils/timeOptions.test.ts new file mode 100644 index 0000000000..1100d3f420 --- /dev/null +++ b/website/src/utils/timeOptions.test.ts @@ -0,0 +1,53 @@ +import { filterTimeOptions, toTimeLabel } from './timeOptions'; + +describe('timeOptions', () => { + describe('toTimeLabel', () => { + test.each([ + ['0800', '08:00'], + ['0915', '09:15'], + ['1030', '10:30'], + ['1145', '11:45'], + ['0333', '03:33'], + ])('should label %s as %s', (time, expected) => { + expect(toTimeLabel(time)).toBe(expected); + }); + }); + + describe('filterTimeOptions', () => { + const timeOptions = [ + '0800', + '0830', + '0900', + '0930', + '1000', + '1030', + '1100', + '1130', + '1200', + '1230', + ]; + + describe('when filter is greater', () => { + it('should keep times greater than selected time', () => { + expect(filterTimeOptions(timeOptions, '1000', 'greater')).toEqual([ + '1030', + '1100', + '1130', + '1200', + '1230', + ]); + }); + }); + + describe('when filter is lesser', () => { + it('should keep times lesser than selected time', () => { + expect(filterTimeOptions(timeOptions, '1000', 'lesser')).toEqual([ + '0800', + '0830', + '0900', + '0930', + ]); + }); + }); + }); +}); diff --git a/website/src/utils/timeOptions.ts b/website/src/utils/timeOptions.ts new file mode 100644 index 0000000000..83521de617 --- /dev/null +++ b/website/src/utils/timeOptions.ts @@ -0,0 +1,107 @@ +export const TIME_OPTIONS = { + START_CLASS: [ + '0800', + '0830', + '0900', + '0930', + '1000', + '1030', + '1100', + '1130', + '1200', + '1230', + '1300', + '1330', + '1400', + '1430', + '1500', + '1530', + '1600', + '1630', + '1700', + '1730', + '1800', + '1830', + '1900', + '1930', + '2000', + '2030', + '2100', + '2130', + '2200', + ], + END_CLASS: [ + '0900', + '0930', + '1000', + '1030', + '1100', + '1130', + '1200', + '1230', + '1300', + '1330', + '1400', + '1430', + '1500', + '1530', + '1600', + '1630', + '1700', + '1730', + '1800', + '1830', + '1900', + '1930', + '2000', + '2030', + '2100', + '2130', + '2200', + '2230', + '2300', + ], + START_LUNCH: [ + '1000', + '1030', + '1100', + '1130', + '1200', + '1230', + '1300', + '1330', + '1400', + '1430', + '1500', + '1530', + '1600', + '1630', + ], + END_LUNCH: [ + '1100', + '1130', + '1200', + '1230', + '1300', + '1330', + '1400', + '1430', + '1500', + '1530', + '1600', + '1630', + '1700', + '1730', + ], +}; + +export const toTimeLabel = (time: string): string => `${time.slice(0, 2)}:${time.slice(2)}`; + +export const filterTimeOptions = ( + timeOptions: string[], + selectedTime: string, + filter: 'greater' | 'lesser', +) => + filter === 'greater' + ? timeOptions.filter((option) => option > selectedTime) + : timeOptions.filter((option) => option < selectedTime); diff --git a/website/src/views/optimiser/OptimiserForm.tsx b/website/src/views/optimiser/OptimiserForm.tsx index af1362fcd8..a9d44a46c2 100644 --- a/website/src/views/optimiser/OptimiserForm.tsx +++ b/website/src/views/optimiser/OptimiserForm.tsx @@ -1,10 +1,11 @@ -import React, { useCallback } from 'react'; +import React, { useCallback, useState } from 'react'; import classnames from 'classnames'; import { Info, X, AlertTriangle } from 'react-feather'; import Tooltip from 'views/components/Tooltip'; import { WorkingDays, Day } from 'types/modules'; import { LessonOption, FreeDayConflict } from './types'; import styles from './OptimiserForm.scss'; +import { TIME_OPTIONS, filterTimeOptions, toTimeLabel } from '../../utils/timeOptions'; interface OptimiserFormProps { lessonOptions: LessonOption[]; @@ -59,6 +60,11 @@ const OptimiserForm: React.FC = ({ [onToggleFreeDay], ); + const [classStartTimes, setClassStartTimes] = useState(TIME_OPTIONS.START_CLASS); + const [classEndTimes, setClassEndTimes] = useState(TIME_OPTIONS.END_CLASS); + const [lunchStartTimes, setLunchStartTimes] = useState(TIME_OPTIONS.START_LUNCH); + const [lunchEndTimes, setLunchEndTimes] = useState(TIME_OPTIONS.END_LUNCH); + return (
@@ -216,36 +222,18 @@ const OptimiserForm: React.FC = ({
@@ -260,36 +248,18 @@ const OptimiserForm: React.FC = ({ @@ -344,43 +314,35 @@ const OptimiserForm: React.FC = ({
to