Skip to content

Commit 40c632a

Browse files
committed
feat: add subjects to drop down #37
1 parent 2688a9e commit 40c632a

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
lines changed

src/components/pastpaper/UploadModal.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import { getDownloadURL, ref, uploadBytes } from 'firebase/storage';
3737
import { v4 as uuidv4 } from 'uuid';
3838
import { doc, serverTimestamp, setDoc } from 'firebase/firestore';
3939
import { User } from 'firebase/auth';
40+
import useAllSubjects from '~/hooks/useAllSubjects';
4041

4142
const UploadModal = ({
4243
isOpen,
@@ -53,7 +54,7 @@ const UploadModal = ({
5354
});
5455

5556
const toast = useToast();
56-
const [subjects, setSubjects] = useState(['oop', 'dsa', 'foo']);
57+
const subjects = useAllSubjects();
5758

5859
const inputRef = useRef<HTMLInputElement>(null);
5960
const [selectedFile, setSelectedFile] = useState<{ img: null | File; err: undefined | string }>({

src/hooks/AllSubjectsProvider.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { createContext } from 'react';
2+
import { ReactNode } from 'react-markdown/lib/ast-to-react';
3+
4+
export const AllSubjectsContext = createContext<string[]>([]);
5+
6+
interface AllSubjectsProviderProps {
7+
children?: ReactNode;
8+
value: string[];
9+
}
10+
11+
const AllSubjectsProvider = ({ value, children }: AllSubjectsProviderProps) => {
12+
return <AllSubjectsContext.Provider value={value}>{children}</AllSubjectsContext.Provider>;
13+
};
14+
15+
export default AllSubjectsProvider;

src/hooks/useAllSubjects.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { AllSubjectsContext } from "./AllSubjectsProvider";
2+
import { useContext } from "react";
3+
4+
const useAllSubjects = ()=> {
5+
return useContext(AllSubjectsContext);
6+
};
7+
8+
export default useAllSubjects;

src/pages/pastpaper/index.tsx

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,48 @@
1+
import axios from 'axios';
12
import { GetServerSidePropsContext } from 'next';
23
import Head from 'next/head';
34
import MainAnimator from '~/components/design/MainAnimator';
45
import PastPapers from '~/components/pastpaper/pastpapers';
6+
import AllSubjectsProvider from '~/hooks/AllSubjectsProvider';
7+
import { decrypt } from '~/lib/cipher';
8+
import { APIS_ENDPOINTS } from '~/lib/constant';
9+
import { TimetableDataType, TimetableDocType } from '~/types/typedef';
510

611
export async function getServerSideProps(context: GetServerSidePropsContext) {
7-
// todo: Get all subjects here
12+
const { data } = await axios.get(APIS_ENDPOINTS.ALL_TIMETABLES);
13+
const timetables = decrypt(data) as Array<TimetableDocType>;
14+
const subjects = timetables
15+
.map(({ timetable }) =>
16+
Object.values(timetable)
17+
.map((oneDayTimetable: TimetableDataType[]) =>
18+
oneDayTimetable.map(({ subject }) => subject)
19+
)
20+
.flat(1)
21+
)
22+
.flat(1);
23+
const distSubjects = Array.from(new Set(subjects));
24+
825
return {
9-
props: {}
26+
props: {
27+
subjects: distSubjects
28+
}
1029
};
1130
}
1231

1332
interface GetStaticPropsReturnType {
14-
data: {};
33+
subjects: string[];
1534
}
1635

17-
export default function PastPaperPage({}: GetStaticPropsReturnType) {
36+
export default function PastPaperPage({ subjects }: GetStaticPropsReturnType) {
1837
return (
1938
<>
2039
<Head>
2140
<title>Past Papers</title>
2241
</Head>
2342
<MainAnimator>
24-
<PastPapers />
43+
<AllSubjectsProvider value={subjects}>
44+
<PastPapers />
45+
</AllSubjectsProvider>
2546
</MainAnimator>
2647
</>
2748
);

0 commit comments

Comments
 (0)