From 269948cc20d08c7f02c083d2dce1450ac4a9236e Mon Sep 17 00:00:00 2001 From: Drew Volz Date: Sat, 11 Feb 2023 18:20:44 -0800 Subject: [PATCH 1/7] fix logout from throwing an error once we've cleared the credentials * prevent throwing an error from within loadCredentials * update QueryFnData and performLogin for falsey login credentials Co-authored-by: Hawken Rives --- source/lib/login.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/source/lib/login.ts b/source/lib/login.ts index 14bc9fd5a6..35e52d876a 100644 --- a/source/lib/login.ts +++ b/source/lib/login.ts @@ -33,11 +33,8 @@ type Credentials = { password: string } -async function loadCredentials(): Promise { +async function loadCredentials(): Promise { let credentials = await getInternetCredentials(SIS_LOGIN_KEY) - if (credentials === false) { - throw new NoCredentialsError() - } return credentials } @@ -62,10 +59,11 @@ export function resetCredentials(): Promise { export async function performLogin( credentials: Credentials | null = null, ): Promise { - const {username, password} = credentials ?? (await loadCredentials()) - if (!username || !password) { + const saved = credentials ?? (await loadCredentials()) + if (!saved) { throw new NoCredentialsError() } + const {username, password} = saved let formData = new FormData() formData.append('username', username) @@ -86,7 +84,7 @@ export async function performLogin( return {username, password} } -type QueryFnData = SharedWebCredentials +type QueryFnData = false | SharedWebCredentials type DefaultError = null | unknown type QueryT = ReturnType> From 6150115c1d834e4bca914900f711e61ae0506a7a Mon Sep 17 00:00:00 2001 From: Drew Volz Date: Sun, 12 Feb 2023 11:31:47 -0800 Subject: [PATCH 5/7] if no username is found opt to return null --- source/lib/login.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/lib/login.ts b/source/lib/login.ts index 78ad5c94fa..e0c4c15a4a 100644 --- a/source/lib/login.ts +++ b/source/lib/login.ts @@ -103,10 +103,10 @@ export function useCredentials( } export function useUsername(): QueryT< - false | Pick + null | Pick > { return useCredentials({ - select: (data) => (data ? {username: data.username} : false), + select: (data) => (data ? {username: data.username} : null), }) } From 5e380ec99ab75c15fdc9561e276061039531976b Mon Sep 17 00:00:00 2001 From: Drew Volz Date: Tue, 14 Feb 2023 21:24:57 -0800 Subject: [PATCH 6/7] simplify type signature for useUsername hook --- source/lib/login.ts | 6 ++---- .../views/settings/screens/overview/login-credentials.tsx | 4 ++-- source/views/sis/balances.tsx | 2 +- source/views/stoprint/print-release.tsx | 2 +- source/views/stoprint/query.ts | 8 ++++---- 5 files changed, 10 insertions(+), 12 deletions(-) diff --git a/source/lib/login.ts b/source/lib/login.ts index e0c4c15a4a..fffea21edf 100644 --- a/source/lib/login.ts +++ b/source/lib/login.ts @@ -102,11 +102,9 @@ export function useCredentials( }) } -export function useUsername(): QueryT< - null | Pick -> { +export function useUsername(): QueryT { return useCredentials({ - select: (data) => (data ? {username: data.username} : null), + select: (data) => data?.username, }) } diff --git a/source/views/settings/screens/overview/login-credentials.tsx b/source/views/settings/screens/overview/login-credentials.tsx index fd8b403220..c3ad6d0412 100644 --- a/source/views/settings/screens/overview/login-credentials.tsx +++ b/source/views/settings/screens/overview/login-credentials.tsx @@ -28,8 +28,8 @@ export const CredentialsLoginSection = (): JSX.Element => { return } - setUsername(data.username ?? '') - setPassword(data.password ?? '') + setUsername(data.username) + setPassword(data.password) }, }) diff --git a/source/views/sis/balances.tsx b/source/views/sis/balances.tsx index bfb4232dd2..99c38e61fa 100644 --- a/source/views/sis/balances.tsx +++ b/source/views/sis/balances.tsx @@ -20,7 +20,7 @@ const DISCLAIMER = 'This data may be outdated or otherwise inaccurate.' export const BalancesView = (): JSX.Element => { let navigation = useNavigation() let {data: usernameData} = useUsername() - let username = usernameData ? usernameData.username : '' + const username = usernameData ?? '' let { data = {} as BalancesShapeType, diff --git a/source/views/stoprint/print-release.tsx b/source/views/stoprint/print-release.tsx index 51c602972f..5962f0e42d 100644 --- a/source/views/stoprint/print-release.tsx +++ b/source/views/stoprint/print-release.tsx @@ -81,7 +81,7 @@ export const PrintJobReleaseView = (): JSX.Element => { let {job, printer} = route.params let {data: usernameData, isLoading: loadingUsername} = useUsername() - let username = usernameData ? usernameData.username : '' + const username = usernameData ?? '' let {data: heldJobs = []} = useHeldJobs(printer?.printerName) let jobId = job.id.toString() diff --git a/source/views/stoprint/query.ts b/source/views/stoprint/query.ts index 1d6afcf57f..1efd14b4c1 100644 --- a/source/views/stoprint/query.ts +++ b/source/views/stoprint/query.ts @@ -31,7 +31,7 @@ export const keys = { export function usePrintJobs(): UseQueryResult { let {data} = useUsername() - let username = data ? data.username : '' + const username = data ?? '' return useQuery({ queryKey: keys.jobs(username), @@ -42,7 +42,7 @@ export function usePrintJobs(): UseQueryResult { export function useAllPrinters(): UseQueryResult { let {data} = useUsername() - let username = data ? data.username : '' + const username = data ?? '' return useQuery({ queryKey: keys.printers(username), @@ -56,7 +56,7 @@ export function useRecentPrinters(): UseQueryResult< unknown > { let {data} = useUsername() - let username = data ? data.username : '' + const username = data ?? '' return useQuery({ queryKey: keys.printers(username), @@ -76,7 +76,7 @@ export function useHeldJobs( printerName: string | undefined, ): UseQueryResult { let {data} = useUsername() - let username = data ? data.username : '' + const username = data ?? '' let usablePrinterName = printerName || 'undefined' From 7f3d5b8e564984ecf6bbc30223cc3f1c31812431 Mon Sep 17 00:00:00 2001 From: Drew Volz Date: Tue, 14 Feb 2023 21:38:52 -0800 Subject: [PATCH 7/7] inline the default value and rename from usernameData to username Co-authored-by: Hawken Rives --- source/views/sis/balances.tsx | 3 +-- source/views/stoprint/print-release.tsx | 3 +-- source/views/stoprint/query.ts | 12 ++++-------- 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/source/views/sis/balances.tsx b/source/views/sis/balances.tsx index 99c38e61fa..931d56dc10 100644 --- a/source/views/sis/balances.tsx +++ b/source/views/sis/balances.tsx @@ -19,8 +19,7 @@ const DISCLAIMER = 'This data may be outdated or otherwise inaccurate.' export const BalancesView = (): JSX.Element => { let navigation = useNavigation() - let {data: usernameData} = useUsername() - const username = usernameData ?? '' + let {data: username = ''} = useUsername() let { data = {} as BalancesShapeType, diff --git a/source/views/stoprint/print-release.tsx b/source/views/stoprint/print-release.tsx index 5962f0e42d..31c1ccaea6 100644 --- a/source/views/stoprint/print-release.tsx +++ b/source/views/stoprint/print-release.tsx @@ -80,8 +80,7 @@ export const PrintJobReleaseView = (): JSX.Element => { let route = useRoute>() let {job, printer} = route.params - let {data: usernameData, isLoading: loadingUsername} = useUsername() - const username = usernameData ?? '' + let {data: username = '', isLoading: loadingUsername} = useUsername() let {data: heldJobs = []} = useHeldJobs(printer?.printerName) let jobId = job.id.toString() diff --git a/source/views/stoprint/query.ts b/source/views/stoprint/query.ts index 1efd14b4c1..418690e46b 100644 --- a/source/views/stoprint/query.ts +++ b/source/views/stoprint/query.ts @@ -30,8 +30,7 @@ export const keys = { } export function usePrintJobs(): UseQueryResult { - let {data} = useUsername() - const username = data ?? '' + let {data: username = ''} = useUsername() return useQuery({ queryKey: keys.jobs(username), @@ -41,8 +40,7 @@ export function usePrintJobs(): UseQueryResult { } export function useAllPrinters(): UseQueryResult { - let {data} = useUsername() - const username = data ?? '' + let {data: username = ''} = useUsername() return useQuery({ queryKey: keys.printers(username), @@ -55,8 +53,7 @@ export function useRecentPrinters(): UseQueryResult< RecentPopularPrintersResponse, unknown > { - let {data} = useUsername() - const username = data ?? '' + let {data: username = ''} = useUsername() return useQuery({ queryKey: keys.printers(username), @@ -75,8 +72,7 @@ export function useColorPrinters(): UseQueryResult { export function useHeldJobs( printerName: string | undefined, ): UseQueryResult { - let {data} = useUsername() - const username = data ?? '' + let {data: username = ''} = useUsername() let usablePrinterName = printerName || 'undefined'