Skip to content

Commit

Permalink
fix: increased timeout value for fetching course info + refactored fe…
Browse files Browse the repository at this point in the history
…tchTermData to fix issue with restoring previously saved data
  • Loading branch information
Shzmj committed Oct 15, 2023
1 parent 95d39bc commit e00c54f
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 73 deletions.
47 changes: 5 additions & 42 deletions client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,14 @@ const App: React.FC = () => {
setFirstDayOfTerm(firstDayOfTerm);
setTermsData(termsData);
const oldData = storage.get('timetables');

// avoid overwriting data from previous save
const newTimetableTerms = {
...oldData,
[termsData.prevTerm.term]: createDefaultTimetable(),
[termsData.newTerm.term]: createDefaultTimetable(),
...(oldData.hasOwnProperty(termsData.prevTerm.term) ? {} : { [termsData.prevTerm.term]: createDefaultTimetable() }),
...(oldData.hasOwnProperty(termsData.newTerm.term) ? {} : { [termsData.newTerm.term]: createDefaultTimetable() }),
}

setDisplayTimetables(newTimetableTerms)
storage.set('timetables', newTimetableTerms);
};
Expand Down Expand Up @@ -379,21 +382,6 @@ const App: React.FC = () => {

// The following three useUpdateEffects update local storage whenever a change is made to the timetable
useUpdateEffect(() => {
// console.log('updating selected courses')
// console.log(displayTimetables);
// const newDisplayTimetables = {
// ...displayTimetables,
// [term]: displayTimetables[term].map((timetable, index) => {
// return index === selectedTimetable ?
// { ...timetable, selectedCourses: selectedCourses }
// : timetable;
// })
// }
// console.log(newDisplayTimetables);
// storage.set('courseData', courseData);
// storage.set('timetables', newDisplayTimetables);
// setDisplayTimetables(newDisplayTimetables);
// console.log(storage.get('timetables')[term][selectedTimetable])
displayTimetables[term][selectedTimetable].selectedCourses = selectedCourses;
const newCourseData = courseData;
storage.set('courseData', newCourseData);
Expand All @@ -402,37 +390,12 @@ const App: React.FC = () => {
}, [selectedCourses]);

useUpdateEffect(() => {
// console.log('updating selected classes')
// console.log(displayTimetables);
// const newDisplayTimetables = {
// ...displayTimetables,
// [term]: displayTimetables[term].map((timetable, index) => {
// return index === selectedTimetable ?
// { ...timetable, selectedClasses: selectedClasses }
// : timetable;
// })
// }
// console.log(newDisplayTimetables);
// storage.set('timetables', newDisplayTimetables);
// setDisplayTimetables(newDisplayTimetables);
// console.log(storage.get('timetables')[term][selectedTimetable])
// console.log(displayTimetables[term][selectedTimetable])
displayTimetables[term][selectedTimetable].selectedClasses = selectedClasses;
storage.set('timetables', displayTimetables);
setDisplayTimetables(displayTimetables);
}, [selectedClasses]);

useUpdateEffect(() => {
// const newDisplayTimetables = {
// ...displayTimetables,
// [term]: displayTimetables[term].map((timetable, index) => {
// return index === selectedTimetable ?
// { ...timetable, createdEvents: createdEvents }
// : timetable;
// })
// }
// storage.set('timetables', newDisplayTimetables);
// setDisplayTimetables(newDisplayTimetables);
displayTimetables[term][selectedTimetable].createdEvents = createdEvents;
storage.set('timetables', displayTimetables);
setDisplayTimetables(displayTimetables);
Expand Down
2 changes: 1 addition & 1 deletion client/src/api/getCourseInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const getCourseInfo = async (
): Promise<CourseData> => {
const baseURL = `${API_URL.timetable}/terms/${year}-${term}`;
try {
const data = await timeoutPromise(1500, fetch(`${baseURL}/courses/${courseCode}/`));
const data = await timeoutPromise(2000, fetch(`${baseURL}/courses/${courseCode}/`));

// Remove any leftover courses from localStorage if they are not offered in the current term
// which is why a 400 error is returned
Expand Down
1 change: 0 additions & 1 deletion client/src/components/controls/History.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ const History: React.FC = () => {
if (displayTimetables[term].length > 1) {
disableStatus.all = false;
}
console.log(displayTimetables, term, selectedTimetable);

// Current timetable being non-empty is resetAll and resetOne-able
const currentTimetable = displayTimetables[term][selectedTimetable];
Expand Down
26 changes: 3 additions & 23 deletions client/src/components/navbar/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Description, Info, Security, Settings as SettingsIcon } from '@mui/icons-material';
import { AppBar, Toolbar, Typography, useMediaQuery, useTheme, FormControl, MenuItem, Select, InputLabel } from '@mui/material';
import { createDefaultTimetable } from '../../utils/timetableHelpers';
import { AppBar, Toolbar, Typography, useMediaQuery, useTheme, FormControl, MenuItem, Select } from '@mui/material';
import { styled } from '@mui/system';
import React, { useContext, useState } from 'react';

Expand Down Expand Up @@ -39,16 +38,6 @@ const NavbarTitle = styled(Typography)`
z-index: 1201;
`;

const Weak = styled('span')`
font-weight: 300;
opacity: 0.8;
margin-left: 15px;
font-size: 90%;
vertical-align: middle;
position: relative;
bottom: 1px;
z-index: 1201;
`;

const Navbar: React.FC = () => {
const [currLogo, setCurrLogo] = useState(notanglesLogo);
Expand All @@ -60,10 +49,8 @@ const Navbar: React.FC = () => {
year,
setTerm,
setYear,
selectedTimetable,
setSelectedTimetable,
displayTimetables,
setDisplayTimetables,
termsData
} = useContext(AppContext);

Expand All @@ -74,18 +61,15 @@ const Navbar: React.FC = () => {
const isMobile = useMediaQuery(theme.breakpoints.down('sm'));

const selectTerm = (e: any) => {
// // TODO: need to handle selecting that corresponding term n reloading term data/resetting timetable?
const defaultStartTimetable = 0;

let newTermName = e.target.value.split(', ')[0]
let termNum = 'T' + newTermName.split(' ')[1]
let newYear = e.target.value.split(', ')[1]
// // TODO:fix this so that we switch the termId for the current tab
// // TODO:implement error message to ask user if they want to reset the current timetable (if it has any data) b4 switching terms
// NEW IDEA: TODO: use arrow buttons to switch between terms - we shld keep independent timetables for each term and change/restore these when we switch between the terms

setTerm(termNum)
setYear(newYear)
setTermName(newTermName)
const defaultStartTimetable = 0;
setSelectedTimetable(defaultStartTimetable);
setSelectedClasses(displayTimetables[termNum][defaultStartTimetable].selectedClasses);
setCreatedEvents(displayTimetables[termNum][defaultStartTimetable].createdEvents);
Expand All @@ -105,14 +89,10 @@ const Navbar: React.FC = () => {
/>
<NavbarTitle variant="h6">
Notangles
{/* <Weak>{isMobile ? term : termName.concat(', ', year)}</Weak> */}
</NavbarTitle>
<FormControl>
<Select
labelId="demo-simple-select-label"
id="demo-simple-select"
value={isMobile ? term : termName.concat(', ', year)}
label="terms"
onChange={selectTerm}
>
{
Expand Down
9 changes: 3 additions & 6 deletions client/src/constants/timetable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const getAvailableTermDetails = async () => {
if (localStorage.getItem('termData')) {
termData = JSON.parse(localStorage.getItem('termData')!);
}

let year = termData.year || '0000';
let termNumber = Number(termData.termNumber) || 1;
let term = termData.termName || `T${termNumber}`;
Expand All @@ -31,17 +32,12 @@ export const getAvailableTermDetails = async () => {
const termDateFetch = await timeoutPromise(1000, fetch(`${API_URL.timetable}/startdate/notangles`));
const termDateRes = await termDateFetch.text();
const termIdFetch = await timeoutPromise(1000, fetch(`${API_URL.timetable}/availableterm`));
// console.log(termDateRes)

// testing how to get prev/current term data --> can potentially make use of freerooms api
// startdate/freerooms gets the start date of the current term
// /currentterm can be used to get the current terms id
const prevTermDate = await timeoutPromise(1000, fetch(`${API_URL.timetable}/startdate/freerooms`));
const prevTermRes = await prevTermDate.text();
const prevTermId = await timeoutPromise(1000, fetch(`${API_URL.timetable}/currentterm`));
const prevTermIdRes = await prevTermId.text();
// console.log(prevTermRes);
// console.log(prevTermIdRes);

let regexp = /(\d{2})\/(\d{2})\/(\d{4})/;

let matched = termDateRes.match(regexp);
Expand Down Expand Up @@ -97,6 +93,7 @@ export const getAvailableTermDetails = async () => {
const termsData = {
prevTerm: { year: prevYear, term: prevTerm, termName: prevTermName }, newTerm: { year: year, term: term, termName: termName }
}

// Store the term details in local storage.
localStorage.setItem(
'termData',
Expand Down

0 comments on commit e00c54f

Please sign in to comment.