Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
2,817 additions
and
51 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// @flow | ||
import type { Venue, RawLesson, ModuleCode, DayText, LessonTime } from 'types/modules'; | ||
|
||
// Components within a venue availability class: | ||
export type VenueOccupiedState = 'vacant' | 'occupied'; | ||
export type Availability = { [LessonTime]: VenueOccupiedState } // E.g. { "1000": "vacant", "1030": "occupied", ... } | ||
|
||
// Raw lessons obtained from venue info API includes ModuleCode by default | ||
export type VenueLesson = RawLesson & { | ||
ModuleCode: ModuleCode, | ||
}; | ||
|
||
// A venue's availability info for one day | ||
// E.g. { "Day": "Monday", "Classes": [...], "Availability": {...} } | ||
export type DayAvailability = { | ||
Day: DayText, | ||
Classes: VenueLesson[], | ||
Availability: Availability, | ||
} | ||
|
||
// Describes venueInformation.json | ||
// E.g. { "LT16": [DayAvailability1, DayAvailability2, ...], "LT17": [...], ... } | ||
export type VenueInfo = { [Venue]: DayAvailability[] }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// @flow | ||
import { range, without, sample } from 'lodash'; | ||
|
||
import type { ColorIndex } from 'types/reducers'; | ||
import type { Lesson } from 'types/modules'; | ||
|
||
export const NUM_DIFFERENT_COLORS: number = 8; | ||
|
||
// Returns a new index that is not present in the current color index. | ||
// If there are more than NUM_DIFFERENT_COLORS modules already present, | ||
// will try to balance the color distribution if randomize === true. | ||
export function getNewColor(currentColorIndices: Array<ColorIndex>, randomize: boolean = true): ColorIndex { | ||
function generateInitialIndices(): Array<number> { | ||
return range(NUM_DIFFERENT_COLORS); | ||
} | ||
|
||
let availableColorIndices = generateInitialIndices(); | ||
currentColorIndices.forEach((index: ColorIndex) => { | ||
availableColorIndices = without(availableColorIndices, index); | ||
if (availableColorIndices.length === 0) { | ||
availableColorIndices = generateInitialIndices(); | ||
} | ||
}); | ||
|
||
if (randomize) { | ||
return sample(availableColorIndices); | ||
} | ||
return availableColorIndices[0]; | ||
} | ||
|
||
// Color lessons by a certain property of every lesson | ||
// e.g. clbk([...], 'LessonType') colors lessons by their type | ||
export function colorLessonsByKey(lessons: Lesson[], key: string) { | ||
const colorMap = new Map(); | ||
return lessons.map((lesson) => { | ||
let colorIndex = colorMap.get(lesson[key]); | ||
if (!colorMap.has(lesson[key])) { | ||
colorIndex = getNewColor(Array.from(colorMap.values()), false); | ||
colorMap.set(lesson[key], colorIndex); | ||
} | ||
|
||
return { ...lesson, colorIndex }; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// @flow | ||
import { range, without } from 'lodash'; | ||
|
||
import type { ColorIndex } from 'types/reducers'; | ||
import type { Lesson } from 'types/modules'; | ||
|
||
import { NUM_DIFFERENT_COLORS, getNewColor, colorLessonsByKey } from './colors'; | ||
|
||
describe('#getNewColor()', () => { | ||
test('it should get color without randomization', () => { | ||
// When there are no current colors | ||
expect(getNewColor([], false)).toBe(0); | ||
// When there are colors that have not been picked | ||
expect(getNewColor([0, 1], false)).toBe(2); | ||
// When all the colors have been picked once | ||
expect(getNewColor(range(NUM_DIFFERENT_COLORS), false)).toBe(0); | ||
// When all the colors have been picked once or more | ||
expect(getNewColor([...range(NUM_DIFFERENT_COLORS), 0, 1], false)).toBe(2); | ||
}); | ||
|
||
test('it should get random color', () => { | ||
// We're not actually testing randomness, only that the color indices returned are valid | ||
// Check that calling getNewColor with currentColors returns an int | ||
// in [0, NUM_DIFFERENT_COLORS] AND not in unexpectedColors | ||
function expectValidIndex(unexpectedColors: Array<ColorIndex>, currentColors: Array<ColorIndex>) { | ||
expect(without(range(NUM_DIFFERENT_COLORS), ...unexpectedColors)) | ||
.toContain(getNewColor(currentColors, true)); | ||
} | ||
|
||
range(100).forEach(() => { | ||
// When there are no current colors | ||
expectValidIndex([], []); | ||
// When there are colors that have not been picked | ||
expectValidIndex([5, 3], [5, 3]); | ||
// When all the colors have been picked once | ||
expectValidIndex([], range(NUM_DIFFERENT_COLORS)); | ||
// When all the colors have been picked once or more | ||
expectValidIndex([5, 3], [...range(NUM_DIFFERENT_COLORS), 5, 3]); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('#colorLessonsByKey()', () => { | ||
const bareLesson: Lesson = { | ||
ClassNo: '', | ||
DayText: '', | ||
EndTime: '', | ||
LessonType: '', | ||
StartTime: '', | ||
Venue: '', | ||
WeekText: '', | ||
ModuleCode: '', | ||
ModuleTitle: '', | ||
}; | ||
|
||
test('it should assign colors deterministically', () => { | ||
const lessons: Lesson[] = []; | ||
range(100).forEach((i) => { | ||
// Add 2 lessons for this ith venue | ||
const newLesson = { ...bareLesson, Venue: `LT${i}` }; | ||
lessons.push(newLesson); | ||
lessons.push(newLesson); | ||
|
||
const coloredLessons = colorLessonsByKey(lessons, 'Venue'); | ||
const coloredLesson = coloredLessons[coloredLessons.length - 1]; | ||
|
||
expect(coloredLesson).toMatchObject(newLesson); // Ensure that existing lesson info wasn't modified | ||
expect(coloredLesson).toHaveProperty('colorIndex', i % NUM_DIFFERENT_COLORS); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// @flow | ||
import React from 'react'; | ||
import { AlertTriangle } from 'views/components/icons'; | ||
import styles from './Warning.scss'; | ||
|
||
type Props = { | ||
message: string, | ||
} | ||
|
||
export default function Warning(props: Props) { | ||
return ( | ||
<div className="text-center mt-4"> | ||
<AlertTriangle className={styles.noModulesIcon} /> | ||
<h4>{props.message}</h4> | ||
</div> | ||
); | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// @flow | ||
import React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
import Warning from './Warning'; | ||
|
||
test('it displays warning message', () => { | ||
const wrapper = shallow(<Warning message="abcde/ghi123!@#$" />); | ||
expect(wrapper.find('[children="abcde/ghi123!@#$"]')).toHaveLength(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.