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

Save selected courses/sections across browser sessions #62

Open
wants to merge 3 commits into
base: main
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
24 changes: 23 additions & 1 deletion app/src/components/ScheduleBuilder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ export default class ScheduleBuilder extends Component<Props, States> {
prevState.selections.filter(notEmpty),
)
) {
console.log(`Updating local storage data for "selections:${this.state.soc?.getSOCProgramString()},${this.state.soc?.getSOCTermString()}"`)
localStorage.setItem(`selections:${this.state.soc?.getSOCProgramString()},${this.state.soc?.getSOCTermString()}`, JSON.stringify(this.state.selections));
if (this.state.generator) {
// Make sure generator is not null
this.state.generator.loadSelections(
Expand Down Expand Up @@ -110,6 +112,21 @@ export default class ScheduleBuilder extends Component<Props, States> {
}),
);
this.reset(); // Make sure to only show info from the current SOC
await this.loadStoredSelections(termStr, programStr);
}

async loadStoredSelections(termStr: string, programStr: string) {
console.log(`Searching for local storage data for "selections:${programStr},${termStr}"`)
const data = localStorage.getItem(`selections:${programStr},${termStr}`);
if (data != null) {
console.log("Stored selection data found", JSON.parse(data));
let selectionArray = JSON.parse(data).map((selectionJson: Section[]): Selection => {
return Selection.parseJSON(selectionJson);;
});
this.setState({ selections: selectionArray });
} else {
console.log("Stored selection data not present.")
}
}

async handleDrop(ind: number, uid: string) {
Expand All @@ -130,11 +147,16 @@ export default class ScheduleBuilder extends Component<Props, States> {
(section) =>
!this.state.selections.some(
// TODO: extract to a Selections class
(sel) => sel.includes(section),
(sel) => {
return sel.map((sec) => sec.uid === section.uid).some((truthVal) => truthVal);
}
),
);
this.newSelection(ind, sectionsToAdd); // Add the section that have not been added
}
else {
alert("Error dragging course or section to selections from search results. Please try re-running your search.")
}
}
}

Expand Down
12 changes: 11 additions & 1 deletion app/src/scripts/scheduleGenerator.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import { Section, SOC_Generic } from "@scripts/soc";
import { Term } from "@constants/soc";

export class Selection extends Array<Section> {}




export class Selection extends Array<Section> {
static parseJSON(selectionJson: Section[]): Selection {
return selectionJson.map((sectionJson: Section) => {
return Section.parseJSON(sectionJson);
});
}
}

export class Schedule extends Array<Section> {
term: Term;
Expand Down
32 changes: 31 additions & 1 deletion app/src/scripts/soc/meet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,36 @@ export class MeetTime {
this.periodEnd >= other.periodBegin
);
}

static parseMeetings = (meetingsJson: Meetings): Meetings => {
let meetings = noMeetings();
meetingsJson.M.forEach((meetTimeJson: MeetTime) => meetings.M.push(MeetTime.parseMeetTime(meetTimeJson)));
meetingsJson.T.forEach((meetTimeJson: MeetTime) => meetings.T.push(MeetTime.parseMeetTime(meetTimeJson)));
meetingsJson.W.forEach((meetTimeJson: MeetTime) => meetings.W.push(MeetTime.parseMeetTime(meetTimeJson)));
meetingsJson.R.forEach((meetTimeJson: MeetTime) => meetings.R.push(MeetTime.parseMeetTime(meetTimeJson)));
meetingsJson.F.forEach((meetTimeJson: MeetTime) => meetings.F.push(MeetTime.parseMeetTime(meetTimeJson)));
meetingsJson.S.forEach((meetTimeJson: MeetTime) => meetings.S.push(MeetTime.parseMeetTime(meetTimeJson)));
return meetings;
}

static parseMeetTime = (meetTimeJson: MeetTime): MeetTime => {
let meetTime = new MeetTime(Term.Spring, this.emptyMeetTime(), false);
return Object.assign(meetTime, meetTimeJson);
}

static emptyMeetTime = () => {
return {
meetNo: 0,
meetDays: [],
meetTimeBegin: "",
meetTimeEnd: "",
meetPeriodBegin: "",
meetPeriodEnd: "",
meetBuilding: "",
meetBldgCode: "",
meetRoom: "",
}
}
}

export type Meetings = Record<API_Day, MeetTime[]>;
Expand All @@ -85,4 +115,4 @@ export function noMeetings(): Meetings {
[API_Day.Fri]: [],
[API_Day.Sat]: [],
};
}
}
53 changes: 52 additions & 1 deletion app/src/scripts/soc/section.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,55 @@ export class Section {
this.type == API_Section_Type.MostlyOnline
);
}
}

static parseJSON = (sectionJson: Section): Section => {
let section = new Section("0#0", Term.Fall, this.emptyApiSection(), "MAS3114");
section = Object.assign(section, sectionJson);
section.credits = new MinMax<number>(
sectionJson.credits.min,
sectionJson.credits.max,
);
section.meetings = MeetTime.parseMeetings(sectionJson.meetings);
return section;
}

static emptyApiSection = (): API_Section => {
return {
number: "",
classNumber: 0,
gradBasis: 0,
acadCareer: 0,
display: "",
credits: 0,
credits_min: 0,
credits_max: 0,
note: "",
dNote: "",
genEd: [],
quest: [],
sectWeb: API_Section_Type.PrimarilyClassroom,
rotateTitle: "",
deptCode: 0,
deptName: "",
openSeats: 0,
courseFee: 0,
lateFlag: "",
EEP: "",
LMS: "",
instructors: [],
meetTimes: [],
addEligible: "",
grWriting: "",
finalExam: "",
dropaddDeadline: "",
pastDeadline: false,
startDate: "",
endDate: "",
waitList: {
isEligible: "",
cap: 0,
total: 0,
}
}
}
}
10 changes: 10 additions & 0 deletions app/src/scripts/soc/soc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ export abstract class SOC_Generic {
throw new Error("SOC initializer not implemented.");
}

/* TERM AND PROGRAM */

getSOCTermString = (): string => {
return this.info.termStr;
}

getSOCProgramString = (): string => {
return getProgramString(this.info.program);
}

/* UID */

/**
Expand Down