From 6bb3c285adf9155e7be3c489ef7615ad55c97453 Mon Sep 17 00:00:00 2001
From: Izan Gil <66965250+SrIzan10@users.noreply.github.com>
Date: Sun, 10 Nov 2024 01:29:03 +0100
Subject: [PATCH] fix: token promise errors
---
app/(app)/(tabs)/settings.tsx | 38 ++++++++++++++++++++++++
app/(app)/drawer/_layout.tsx | 54 +++++++++++++++++------------------
app/_layout.tsx | 28 +++++++++---------
lib/clients/classroom.ts | 22 ++++++++++----
tsconfig.json | 3 +-
5 files changed, 98 insertions(+), 47 deletions(-)
diff --git a/app/(app)/(tabs)/settings.tsx b/app/(app)/(tabs)/settings.tsx
index 6be997e..83e3728 100644
--- a/app/(app)/(tabs)/settings.tsx
+++ b/app/(app)/(tabs)/settings.tsx
@@ -16,6 +16,7 @@ import { Colors, LoadingIndicator, ScreenInfo, styles } from '@/lib/ui'
import { reloadAppAsync } from 'expo'
import { useAuth } from '@/lib/providers/auth'
import { Image } from 'expo-image'
+import { queryClient } from '@/lib/clients/classroom'
const Settings = () => {
const colorScheme = useColorScheme()
@@ -274,6 +275,43 @@ const Settings = () => {
+ {__DEV__ && (
+
+ }
+ >
+ }
+ onPress={async () => {
+ await SecureStore.deleteItemAsync('settings')
+ setMessage({
+ visible: true,
+ content: 'Settings cleared',
+ })
+ await reloadAppAsync()
+ }}
+ />
+ }
+ onPress={async () => {
+ queryClient.clear()
+ setMessage({
+ visible: true,
+ content:
+ 'Cache cleared. Wait 1s for the persisters to actually clear',
+ })
+ }}
+ />
+
+
+ )}
+
setMessage({ ...message, visible: false })}
diff --git a/app/(app)/drawer/_layout.tsx b/app/(app)/drawer/_layout.tsx
index e0b8e30..c1d8e4c 100644
--- a/app/(app)/drawer/_layout.tsx
+++ b/app/(app)/drawer/_layout.tsx
@@ -8,35 +8,33 @@ const DrawerLayout = () => {
const theme = useTheme()
return (
-
- (
-
- )}
- screenOptions={{
- drawerStyle: {
- backgroundColor: theme.colors.background,
- paddingTop: 32,
- },
- header: (props) => (
-
- ),
- }}
- >
- (
+
-
-
+ )}
+ screenOptions={{
+ drawerStyle: {
+ backgroundColor: theme.colors.background,
+ paddingTop: 32,
+ },
+ header: (props) => (
+
+ ),
+ }}
+ >
+
+
)
}
diff --git a/app/_layout.tsx b/app/_layout.tsx
index 664de7a..8793b52 100644
--- a/app/_layout.tsx
+++ b/app/_layout.tsx
@@ -35,18 +35,20 @@ export default function Root() {
}, [])
return (
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
)
}
diff --git a/lib/clients/classroom.ts b/lib/clients/classroom.ts
index fe576a7..7b667ea 100644
--- a/lib/clients/classroom.ts
+++ b/lib/clients/classroom.ts
@@ -21,13 +21,25 @@ export const queryClient = new QueryClient({
// API Functions
const BASE_URL = 'https://classroom.googleapis.com'
+// Should fix annoying (in my experience the announcement hook) missing errors.
+let tokenPromise: Promise | null = null
+async function getAuthToken(): Promise {
+ if (!tokenPromise) {
+ tokenPromise = GoogleSignin.getTokens()
+ .then((tokens) => tokens.accessToken)
+ .finally(() => {
+ tokenPromise = null
+ })
+ }
+ return tokenPromise
+}
+
async function fetchApi(
endpoint: string,
insideKey?: string,
options?: RequestInit,
): Promise {
- const token = (await GoogleSignin.getTokens()).accessToken
- //console.log(token)
+ const token = await getAuthToken()
const response = await fetch(`${BASE_URL}${endpoint}`, {
...options,
headers: {
@@ -42,7 +54,7 @@ async function fetchApi(
}
const data = await response.json()
- return insideKey ? data[insideKey] : data
+ return insideKey ? (data[insideKey] ?? []) : data
}
// Query Keys
@@ -50,7 +62,7 @@ export const keys = {
courses: {
all: ['courses'],
one: (id: string) => ['courses', id],
- announcement: (courseId: string) => ['courses', courseId, 'announcements'],
+ announcements: (courseId: string) => ['courses', courseId, 'announcements'],
courseWork: (courseId: string) => ['courses', courseId, 'courseWork'],
courseWorkMaterials: (courseId: string) => [
'courses',
@@ -78,7 +90,7 @@ export function useCourse(id: string) {
export function useAnnouncements(courseId: string) {
return useQuery({
- queryKey: keys.courses.announcement(courseId),
+ queryKey: keys.courses.announcements(courseId),
queryFn: () =>
fetchApi(
`/v1/courses/${courseId}/announcements`,
diff --git a/tsconfig.json b/tsconfig.json
index e128207..00caff0 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -7,7 +7,8 @@
"./*"
]
},
- "jsxImportSource": "nativewind"
+ "jsxImportSource": "nativewind",
+ "module": "ESNext"
},
"include": [
"**/*.ts",