Skip to content

Commit 9c6ddae

Browse files
committed
feat: timetable history
1 parent 4f51d63 commit 9c6ddae

File tree

3 files changed

+78
-8
lines changed

3 files changed

+78
-8
lines changed

src/components/TimetableSelection.tsx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,23 @@ import Fuse from 'fuse.js';
5555
import ViewLinkTxt from './design/ViewLinkTxt';
5656
import { ROUTING } from '~/lib/constant';
5757
import { hashStr } from '~/lib/cipher';
58+
import useTimetableHistory from '~/hooks/useTimetableHistory';
5859

5960
function Selection({ metaData }: { metaData: any }): JSX.Element {
6061
const [userInput, setUserInput] = useState<TimetableInput>({
6162
fall: null,
6263
semester: null,
6364
section: null
6465
});
66+
6567
const [currTabIdx, setCurrTabIdx] = useState<number>(0);
6668
const [history, setHistory] = useState<Array<ITimetableHistory>>([]);
69+
const [isUnder400] = useMediaQuery('(max-width: 400px)');
70+
const [localHistory, setLocalHistory] = useTimetableHistory();
6771

6872
const user = useContext(UserCredentialsContext);
6973
const router = useRouter();
7074

71-
const [isUnder400] = useMediaQuery('(max-width: 400px)');
72-
7375
useEffect(() => {
7476
if (!user?.user) return;
7577
const fetchTimetableHistory = async () => {
@@ -205,12 +207,14 @@ function Selection({ metaData }: { metaData: any }): JSX.Element {
205207
createdAt: serverTimestamp()
206208
});
207209
}
210+
211+
setLocalHistory({
212+
payload: userInput,
213+
created_at: new Date().toDateString(),
214+
hash: hashStr(`${fall} ${semester} ${section}`.replaceAll('/', ''))
215+
});
216+
208217
const hash = hashStr(`${fall} ${semester} ${section}`.replaceAll('/', ''));
209-
console.log(
210-
`${fall} ${semester} ${section}`.replaceAll('/', ''),
211-
' ~ ',
212-
hash
213-
);
214218
router.push(`/timetable/${hash}`);
215219
}}
216220
/>

src/hooks/useTimetableHistory.tsx

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { useContext, useState, useEffect } from 'react';
2+
import { TimetableInput } from '~/types/typedef';
3+
import { UserCredentialsContext } from './UserCredentialsContext';
4+
import { timetableHistoryCol } from '~/lib/firebase';
5+
import { doc, getCountFromServer, query, serverTimestamp, setDoc, where } from 'firebase/firestore';
6+
7+
const STORAGE_KEY = 'TIME_TABLE_HISTORY';
8+
9+
interface UseTimetableHistory {
10+
payload: TimetableInput;
11+
created_at: string;
12+
hash: string;
13+
}
14+
15+
const useTimetableHistory = (): [
16+
UseTimetableHistory[],
17+
(timetable: UseTimetableHistory) => void
18+
] => {
19+
const user = useContext(UserCredentialsContext);
20+
const [timetableHistory, setTimetableHistory] = useState<UseTimetableHistory[]>([]);
21+
22+
useEffect(() => {
23+
const storedTimetableHistory = localStorage.getItem(STORAGE_KEY);
24+
if (storedTimetableHistory) setTimetableHistory(JSON.parse(storedTimetableHistory));
25+
}, []);
26+
27+
useEffect(() => {
28+
if (user == null || user.user == null) return;
29+
30+
const timetableHistoryQuery = query(
31+
timetableHistoryCol,
32+
where('email', '==', user.user?.email)
33+
);
34+
35+
const email = user.user.email as string;
36+
37+
getCountFromServer(timetableHistoryQuery).then((snapShot) => {
38+
const count = snapShot.data().count;
39+
if (count !== 0) return;
40+
41+
timetableHistory.forEach((history) => {
42+
setDoc(doc(timetableHistoryCol), {
43+
payload: history.payload,
44+
email: email,
45+
createdAt: serverTimestamp()
46+
});
47+
});
48+
});
49+
50+
return () => {};
51+
}, [user, timetableHistory]);
52+
53+
const addTimetableHistory = (timetable: UseTimetableHistory) => {
54+
setTimetableHistory((prev) => {
55+
const isAlreadyInHistory = prev.some((history) => history.hash === timetable.hash);
56+
if (isAlreadyInHistory) return prev;
57+
const newTimetableHistory = [timetable, ...prev];
58+
if (newTimetableHistory.length > 50) newTimetableHistory.pop();
59+
localStorage.setItem(STORAGE_KEY, JSON.stringify(newTimetableHistory));
60+
return newTimetableHistory;
61+
});
62+
};
63+
64+
return [timetableHistory, addTimetableHistory];
65+
};
66+
67+
export default useTimetableHistory;

src/pages/_document.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { FavIcons } from '~/components/seo/Seo';
44

55
import { themeConfig } from '~/styles/Style';
66

7-
// re-deploying.
87
export default function Document() {
98
return (
109
<Html lang="en">

0 commit comments

Comments
 (0)