-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
178 additions
and
23 deletions.
There are no files selected for viewing
1 change: 0 additions & 1 deletion
1
app/academia/components/subcomponents/Attendance/AttendanceContent.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 0 additions & 2 deletions
2
app/academia/components/subcomponents/Attendance/PredictionContent.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
"use client"; | ||
import { Cookie as cookies, getCookie } from "@/utils/Cookies"; | ||
import { type ReactNode, createContext, useContext, useState } from "react"; | ||
import useSWR from "swr"; | ||
import Storage from "@/utils/Storage"; | ||
import { AttendanceCourse } from "@/types/Attendance"; | ||
import { getUrl, revalUrl } from "@/utils/URL"; | ||
import { token } from "@/utils/Encrypt"; | ||
import { Mark } from "@/types/Marks"; | ||
import { Course } from "@/types/Course"; | ||
import { User } from "@/types/User"; | ||
import { Table } from "@/types/Timetable"; | ||
import { AllResponses } from "@/types/Response"; | ||
|
||
interface DataContextType { | ||
attendance: AttendanceCourse[] | null; | ||
marks: Mark[] | null; | ||
courses: Course[] | null; | ||
user: User | null; | ||
timetable: Table[] | null; | ||
error: Error | null; | ||
requestedAt: number | null; | ||
isLoading: boolean; | ||
mutate: () => Promise<void | AllResponses | null | undefined>; | ||
} | ||
|
||
const DataContext = createContext<DataContextType>({ | ||
attendance: null, | ||
marks: null, | ||
courses: null, | ||
user: null, | ||
timetable: null, | ||
error: null, | ||
requestedAt: null, | ||
isLoading: false, | ||
mutate: async () => {}, | ||
}); | ||
|
||
const fetcher = async (url: string) => { | ||
const cookie = cookies.get("key"); | ||
if (!cookie) return null; | ||
|
||
const cook = getCookie(cookie ?? "", "_iamadt_client_10002227248"); | ||
if ( | ||
!cook || | ||
cook === "" || | ||
cook === "undefined" || | ||
cookie.includes("undefined") | ||
) | ||
return null; | ||
else | ||
try { | ||
const response = await fetch(url, { | ||
method: "GET", | ||
headers: { | ||
Authorization: `Bearer ${token()}`, | ||
"X-CSRF-Token": cookie, | ||
"Set-Cookie": cookie, | ||
Cookie: cookie, | ||
Connection: "keep-alive", | ||
"Accept-Encoding": "gzip, deflate, br, zstd", | ||
"content-type": "application/json", | ||
"Cache-Control": "private, maxage=86400, stale-while-revalidate=7200", | ||
}, | ||
}); | ||
if (!response.ok) { | ||
const errorBody = await response.text(); | ||
throw new Error( | ||
`HTTP error! status: ${response.status}, body: ${errorBody}`, | ||
); | ||
} | ||
|
||
const data: AllResponses = await response.json(); | ||
if ( | ||
!data || | ||
!data.user || | ||
!data.attendance || | ||
!data.marks || | ||
!data.courses || | ||
!data.timetable | ||
) { | ||
throw new Error("Invalid response format"); | ||
} | ||
|
||
return data; | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
throw error; | ||
} | ||
throw new Error("An unexpected error occurred"); | ||
} | ||
}; | ||
|
||
export function useData() { | ||
return useContext(DataContext); | ||
} | ||
|
||
export function DataProvider({ children }: { children: ReactNode }) { | ||
const cookie = cookies.get("key"); | ||
|
||
const { | ||
data: data, | ||
error, | ||
isValidating, | ||
mutate, | ||
} = useSWR<AllResponses | null>(cookie ? `${revalUrl}/get` : null, fetcher, { | ||
revalidateOnFocus: false, | ||
revalidateOnReconnect: false, | ||
keepPreviousData: true, | ||
shouldRetryOnError: false, | ||
errorRetryCount: 0, | ||
revalidateIfStale: false, | ||
dedupingInterval: 1000 * 60 * 3, | ||
onErrorRetry: (error, key, config, revalidate, { retryCount }) => { | ||
return; | ||
}, | ||
onSuccess: (data) => { | ||
if (data) { | ||
Storage.set("attendance", data.attendance); | ||
Storage.set("marks", data.marks); | ||
Storage.set("courses", data.courses); | ||
Storage.set("timetable", data.timetable); | ||
} | ||
return data; | ||
}, | ||
}); | ||
|
||
return ( | ||
<DataContext.Provider | ||
value={{ | ||
attendance: data?.attendance || null, | ||
marks: data?.marks || null, | ||
courses: data?.courses || null, | ||
user: data?.user || null, | ||
timetable: data?.timetable || null, | ||
|
||
requestedAt: data?.requestedAt || 0, | ||
error: error || null, | ||
|
||
isLoading: isValidating, | ||
mutate, | ||
}} | ||
> | ||
{children} | ||
</DataContext.Provider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { AttendanceCourse } from "./Attendance"; | ||
import { Course } from "./Course"; | ||
import { Mark } from "./Marks"; | ||
import { Table } from "./Timetable"; | ||
import { User } from "./User"; | ||
|
||
export interface AllResponses { | ||
user: User; | ||
attendance: AttendanceCourse[]; | ||
marks: Mark[]; | ||
timetable: Table[]; | ||
courses: Course[]; | ||
requestedAt: number | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters