Skip to content

Commit

Permalink
refactoring and cleaning up code for termSelect
Browse files Browse the repository at this point in the history
  • Loading branch information
Shzmj committed Nov 13, 2023
1 parent 9daed8f commit 1986c21
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 79 deletions.
4 changes: 0 additions & 4 deletions client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,7 @@ const App: React.FC = () => {
const oldData = storage.get('timetables');

// avoid overwriting data from previous save
// TODO: need to check if this logic is correct - do we want to preserve more than just the prev timetable data
const newTimetableTerms = {
// ...oldData,
// ...(oldData.hasOwnProperty(termsData.prevTerm.term) ? {} : { [termsData.prevTerm.term]: createDefaultTimetable() }),
// ...(oldData.hasOwnProperty(termsData.newTerm.term) ? {} : { [termsData.newTerm.term]: createDefaultTimetable() }),
...({ [termsData.prevTerm.term]: oldData.hasOwnProperty(termsData.prevTerm.term) ? oldData[termsData.prevTerm.term] : createDefaultTimetable() }),
...({ [termsData.newTerm.term]: oldData.hasOwnProperty(termsData.newTerm.term) ? oldData[termsData.newTerm.term] : createDefaultTimetable() }),
}
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/controls/History.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const History: React.FC = () => {
return; // Prevents adding change induced by clicking redo/undo
}

if (selectedTimetable >= displayTimetables[term].length) {
if (selectedTimetable >= displayTimetables[term]?.length) {
return;
}

Expand Down
5 changes: 3 additions & 2 deletions client/src/components/navbar/TermSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ const TermSelect: React.FC = () => {
const isMobile = useMediaQuery(theme.breakpoints.down('sm'));

const termData = new Set([
termsData.prevTerm.termName.concat(', ', termsData.prevTerm.year),
termsData.newTerm.termName.concat(', ', termsData.newTerm.year),
`${termsData.newTerm.termName}, ${termsData.newTerm.year}`,
...(termsData.prevTerm.termName.length > 0 ? [`${termsData.prevTerm.termName}, ${termsData.prevTerm.year}`] : []),
]);


const selectTerm = (e: any) => {
const defaultStartTimetable = 0;

Expand Down
3 changes: 1 addition & 2 deletions client/src/components/timetableTabs/TimetableTabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ const TimetableTabs: React.FC = () => {
<Droppable droppableId="tabs" direction="horizontal">
{(props) => (
<StyledTabs ref={props.innerRef} {...props.droppableProps}>
{Object.keys(displayTimetables).length > 0 ? displayTimetables[term].map((timetable: TimetableData, index: number) => (
{Object.keys(displayTimetables).length > 0 ? displayTimetables[term]?.map((timetable: TimetableData, index: number) => (
<Draggable draggableId={index.toString()} index={index} key={index}>
{(props) => {
if (props.draggableProps.style?.transform) {
Expand Down Expand Up @@ -197,7 +197,6 @@ const TimetableTabs: React.FC = () => {
}}
</Draggable>
)) : null}

{props.placeholder}
</StyledTabs>
)}
Expand Down
120 changes: 50 additions & 70 deletions client/src/constants/timetable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ import timeoutPromise from '../utils/timeoutPromise';

const REGULAR_TERM_STR_LEN = 2;

const parseYear = (termDate: string) => {
let regexp = /(\d{2})\/(\d{2})\/(\d{4})/;

let matched = termDate.match(regexp);
let extractedYear = ''
if (matched != null) {
extractedYear = matched[3];
}
return extractedYear;
}

/**
* @returns The details of the latest term there is data for
*/
Expand All @@ -24,111 +35,80 @@ export const getAvailableTermDetails = async () => {

let year = termData.year || '0000';
let termNumber = Number(termData.termNumber) || 1;
let term = termData.termName || `T${termNumber}`;
let termName = `Term ${termNumber}`;
let firstDayOfTerm = termData.firstDayOfTerm || `0000-00-00`;

const parseTermData = (termId: string) => {
let termNum;
let term = termData.termName || `T${termNumber}`;
let termName = `Term ${termNumber}`;

if (termId.length === REGULAR_TERM_STR_LEN) {
// This is not a summer term.
termNum = parseInt(termId.substring(1));
term = `T${termNum}`;
termName = `Term ${termNum}`;
} else {
// This is a summer term.
termName = `Summer Term`;
term = termId;
termNum = 0;
}

return { term: term, termName: termName, termNum: termNum };
}

try {
// notangles api gets the latest term start date available from the scraper
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`));
const termIdRes = await termIdFetch.text();

// freerooms api gets the current term date and not the new term date
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();

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

let matched = termDateRes.match(regexp);
if (matched != null) {
year = matched[3];
let extractedCurrYear = parseYear(termDateRes);
if (extractedCurrYear.length > 0) {
year = extractedCurrYear;
}

let prevYear = ''
matched = prevTermRes.match(regexp);
if (matched != null) {
prevYear = matched[3]
}
let prevYear = parseYear(prevTermRes);

const termDateSplit = termDateRes.split('/');
firstDayOfTerm = termDateSplit.reverse().join('-');

const termIdRes = await termIdFetch.text();
if (termIdRes.length === REGULAR_TERM_STR_LEN) {
// This is not a summer term.
termNumber = parseInt(termIdRes.substring(1));
term = `T${termNumber}`;
termName = `Term ${termNumber}`;
} else {
// This is a summer term.
termName = `Summer Term`;
term = termIdRes;
termNumber = 0; // This is a summer term.
}

// // TODO: hardcoding to see behaviour of preserving prev timetable if the data exists
// termName = `Summer Term`;
// term = "Summer";
// termNumber = 0; // This is a summer term.

// TODO: hardcoding to see behaviour of preserving prev timetable for new year
// termNumber = 1;
// term = `T${termNumber}`;
// termName = `Term ${termNumber}`;
// year = "2024"

const prevDateSplit = prevTermRes.split('/');
let prevFirstDate = prevDateSplit.reverse().join('-');
let prevTermName = `Summer Term`;
let prevTermNum = 0;
let prevTerm = `T${prevTermNum}`;

if (prevTermIdRes.length === REGULAR_TERM_STR_LEN) {
// This is not a summer term.
prevTermNum = parseInt(prevTermIdRes.substring(1));
prevTerm = `T${prevTermNum}`;
prevTermName = `Term ${prevTermNum}`;

// TODO: hardcoded for now to be Term 2
prevTermNum = 2;
prevTerm = `T${prevTermNum}`;
prevTermName = `Term ${prevTermNum}`;

// TODO: testing restoring across years
// prevTermName = `Summer Term`;
// prevTerm = "Summer";
// prevTermNum = 0; // This is a summer term.

} else {
// This is a summer term.
prevTermName = `Summer Term`;
prevTerm = termIdRes;
prevTermNum = 0; // This is a summer term.
}
const newTerm = parseTermData(termIdRes);
const prevTerm = parseTermData(prevTermIdRes);

const termsData = {
prevTerm: { year: prevYear, term: prevTerm, termName: prevTermName }, newTerm: { year: year, term: term, termName: termName }
prevTerm: { year: prevYear, term: prevTerm.term, termName: prevTerm.termName },
newTerm: { year: year, term: newTerm.term, termName: newTerm.termName }
}

// Store the term details in local storage.
localStorage.setItem(
'termData',
JSON.stringify({
year: year,
term: term,
termNumber: termNumber,
termName: termName,
term: newTerm.term,
termNumber: newTerm.termNum,
termName: newTerm.termName,
firstDayOfTerm: firstDayOfTerm,
termsData: termsData
})
);

return {
term: term,
termName: termName,
termNumber: termNumber,
year: year,
term: newTerm.term,
termNumber: newTerm.termNum,
termName: newTerm.termName,
firstDayOfTerm: firstDayOfTerm,
termsData: termsData
};
Expand Down

0 comments on commit 1986c21

Please sign in to comment.