diff options
author | Sebastian <sebasjm@gmail.com> | 2024-03-11 14:56:25 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2024-03-11 14:57:48 -0300 |
commit | 37f46f4d6b821d163c3e4db5c374b1120212ac74 (patch) | |
tree | 641c5bccd6d1b77fa440e67b80543eec9378ef4a /packages/bank-ui/src/hooks | |
parent | 4cbe754aca72b6edee922e3a84f251030293f088 (diff) | |
download | wallet-core-37f46f4d6b821d163c3e4db5c374b1120212ac74.tar.xz |
obs and cancel request, plus lint
Diffstat (limited to 'packages/bank-ui/src/hooks')
-rw-r--r-- | packages/bank-ui/src/hooks/account.ts | 30 | ||||
-rw-r--r-- | packages/bank-ui/src/hooks/bank-state.ts | 9 | ||||
-rw-r--r-- | packages/bank-ui/src/hooks/form.ts | 125 | ||||
-rw-r--r-- | packages/bank-ui/src/hooks/preferences.ts | 3 | ||||
-rw-r--r-- | packages/bank-ui/src/hooks/regional.ts | 58 |
5 files changed, 131 insertions, 94 deletions
diff --git a/packages/bank-ui/src/hooks/account.ts b/packages/bank-ui/src/hooks/account.ts index aa0745253..5fe12573c 100644 --- a/packages/bank-ui/src/hooks/account.ts +++ b/packages/bank-ui/src/hooks/account.ts @@ -62,7 +62,11 @@ export function useAccountDetails(account: string) { } export function revalidateWithdrawalDetails() { - return mutate((key) => Array.isArray(key) && key[key.length - 1] === "getWithdrawalById", undefined, { revalidate: true }); + return mutate( + (key) => Array.isArray(key) && key[key.length - 1] === "getWithdrawalById", + undefined, + { revalidate: true }, + ); } export function useWithdrawalDetails(wid: string) { @@ -110,7 +114,9 @@ export function useWithdrawalDetails(wid: string) { export function revalidateTransactionDetails() { return mutate( - (key) => Array.isArray(key) && key[key.length - 1] === "getTransactionById", undefined, { revalidate: true } + (key) => Array.isArray(key) && key[key.length - 1] === "getTransactionById", + undefined, + { revalidate: true }, ); } export function useTransactionDetails(account: string, tid: number) { @@ -149,7 +155,9 @@ export function useTransactionDetails(account: string, tid: number) { export async function revalidatePublicAccounts() { return mutate( - (key) => Array.isArray(key) && key[key.length - 1] === "getPublicAccounts", undefined, { revalidate: true } + (key) => Array.isArray(key) && key[key.length - 1] === "getPublicAccounts", + undefined, + { revalidate: true }, ); } export function usePublicAccounts( @@ -193,9 +201,10 @@ export function usePublicAccounts( data && data.type === "ok" && data.body.public_accounts.length <= PAGE_SIZE; const isFirstPage = !offset; - const result = data && data.type == "ok" ? structuredClone(data.body.public_accounts) : [] - if (result.length == PAGE_SIZE+1) { - result.pop() + const result = + data && data.type == "ok" ? structuredClone(data.body.public_accounts) : []; + if (result.length == PAGE_SIZE + 1) { + result.pop(); } const pagination = { result, @@ -243,7 +252,7 @@ export function useTransactions(account: string, initial?: number) { return await api.getTransactions( { username, token }, { - limit: PAGE_SIZE +1 , + limit: PAGE_SIZE + 1, offset: txid ? String(txid) : undefined, order: "dec", }, @@ -267,9 +276,10 @@ export function useTransactions(account: string, initial?: number) { data && data.type === "ok" && data.body.transactions.length <= PAGE_SIZE; const isFirstPage = !offset; - const result = data && data.type == "ok" ? structuredClone(data.body.transactions) : [] - if (result.length == PAGE_SIZE+1) { - result.pop() + const result = + data && data.type == "ok" ? structuredClone(data.body.transactions) : []; + if (result.length == PAGE_SIZE + 1) { + result.pop(); } const pagination = { result, diff --git a/packages/bank-ui/src/hooks/bank-state.ts b/packages/bank-ui/src/hooks/bank-state.ts index 83bb009cf..1d8c4f9e6 100644 --- a/packages/bank-ui/src/hooks/bank-state.ts +++ b/packages/bank-ui/src/hooks/bank-state.ts @@ -118,7 +118,7 @@ const codecForChallengeConfirmWithdrawal = .property("request", codecForString()) .build("ConfirmWithdrawalChallenge"); -const codecForAppLocation = codecForString as () => Codec<AppLocation> +const codecForAppLocation = codecForString as () => Codec<AppLocation>; const codecForChallengeCashout = (): Codec<CashoutChallenge> => buildCodecForObject<CashoutChallenge>() @@ -141,8 +141,6 @@ const codecForChallenge = (): Codec<ChallengeInProgess> => .alternative("update-password", codecForChallengeUpdatePassword()) .build("ChallengeInProgess"); - - interface BankState { currentWithdrawalOperationId: string | undefined; currentChallenge: ChallengeInProgess | undefined; @@ -163,10 +161,10 @@ const BANK_STATE_KEY = buildStorageKey("bank-app-state", codecForBankState()); /** * Client state saved in local storage. - * + * * This information is saved in the client because * the backend server session API is not enough. - * + * * @returns tuple of [state, update(), reset()] */ export function useBankState(): [ @@ -185,4 +183,3 @@ export function useBankState(): [ } return [value, updateField, reset]; } - diff --git a/packages/bank-ui/src/hooks/form.ts b/packages/bank-ui/src/hooks/form.ts index 26354b108..afa4912eb 100644 --- a/packages/bank-ui/src/hooks/form.ts +++ b/packages/bank-ui/src/hooks/form.ts @@ -14,87 +14,102 @@ GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -import { AmountJson, TalerBankConversionApi, TranslatedString } from "@gnu-taler/taler-util"; +import { AmountJson, TranslatedString } from "@gnu-taler/taler-util"; import { useState } from "preact/hooks"; export type UIField = { value: string | undefined; onUpdate: (s: string) => void; error: TranslatedString | undefined; -} +}; type FormHandler<T> = { - [k in keyof T]?: - T[k] extends string ? UIField : - T[k] extends AmountJson ? UIField : - FormHandler<T[k]>; -} + [k in keyof T]?: T[k] extends string + ? UIField + : T[k] extends AmountJson + ? UIField + : FormHandler<T[k]>; +}; export type FormValues<T> = { - [k in keyof T]: - T[k] extends string ? (string | undefined) : - T[k] extends AmountJson ? (string | undefined) : - FormValues<T[k]>; -} + [k in keyof T]: T[k] extends string + ? string | undefined + : T[k] extends AmountJson + ? string | undefined + : FormValues<T[k]>; +}; export type RecursivePartial<T> = { - [k in keyof T]?: - T[k] extends string ? (string) : - T[k] extends AmountJson ? (AmountJson) : - RecursivePartial<T[k]>; -} + [k in keyof T]?: T[k] extends string + ? string + : T[k] extends AmountJson + ? AmountJson + : RecursivePartial<T[k]>; +}; export type FormErrors<T> = { - [k in keyof T]?: - T[k] extends string ? (TranslatedString) : - T[k] extends AmountJson ? (TranslatedString) : - FormErrors<T[k]>; -} - -export type FormStatus<T> = { - status: "ok", - result: T, - errors: undefined, -} | { - status: "fail", - result: RecursivePartial<T>, - errors: FormErrors<T>, -} - - -function constructFormHandler<T>(form: FormValues<T>, updateForm: (d: FormValues<T>) => void, errors: FormErrors<T> | undefined): FormHandler<T> { - const keys = (Object.keys(form) as Array<keyof T>) + [k in keyof T]?: T[k] extends string + ? TranslatedString + : T[k] extends AmountJson + ? TranslatedString + : FormErrors<T[k]>; +}; + +export type FormStatus<T> = + | { + status: "ok"; + result: T; + errors: undefined; + } + | { + status: "fail"; + result: RecursivePartial<T>; + errors: FormErrors<T>; + }; + +function constructFormHandler<T>( + form: FormValues<T>, + updateForm: (d: FormValues<T>) => void, + errors: FormErrors<T> | undefined, +): FormHandler<T> { + const keys = Object.keys(form) as Array<keyof T>; const handler = keys.reduce((prev, fieldName) => { - const currentValue: any = form[fieldName]; - const currentError: any = errors ? errors[fieldName] : undefined; - function updater(newValue: any) { - updateForm({ ...form, [fieldName]: newValue }) + const currentValue: unknown = form[fieldName]; + const currentError: unknown = errors ? errors[fieldName] : undefined; + function updater(newValue: unknown) { + updateForm({ ...form, [fieldName]: newValue }); } if (typeof currentValue === "object") { - const group = constructFormHandler(currentValue, updater, currentError) - // @ts-expect-error asdasd - prev[fieldName] = group + // @ts-expect-error FIXME better typing + const group = constructFormHandler(currentValue, updater, currentError); + // @ts-expect-error FIXME better typing + prev[fieldName] = group; return prev; } const field: UIField = { + // @ts-expect-error FIXME better typing error: currentError, + // @ts-expect-error FIXME better typing value: currentValue, - onUpdate: updater - } - // @ts-expect-error asdasd - prev[fieldName] = field - return prev - }, {} as FormHandler<T>) + onUpdate: updater, + }; + // @ts-expect-error FIXME better typing + prev[fieldName] = field; + return prev; + }, {} as FormHandler<T>); return handler; } -export function useFormState<T>(defaultValue: FormValues<T>, check: (f: FormValues<T>) => FormStatus<T>): [FormHandler<T>, FormStatus<T>] { - const [form, updateForm] = useState<FormValues<T>>(defaultValue) +export function useFormState<T>( + defaultValue: FormValues<T>, + check: (f: FormValues<T>) => FormStatus<T>, +): [FormHandler<T>, FormStatus<T>] { + const [form, updateForm] = useState<FormValues<T>>(defaultValue); - const status = check(form) - const handler = constructFormHandler(form, updateForm, status.errors) + const status = check(form); + const handler = constructFormHandler(form, updateForm, status.errors); - return [handler, status] -}
\ No newline at end of file + return [handler, status]; +} diff --git a/packages/bank-ui/src/hooks/preferences.ts b/packages/bank-ui/src/hooks/preferences.ts index 454dc8d80..bb3dcb153 100644 --- a/packages/bank-ui/src/hooks/preferences.ts +++ b/packages/bank-ui/src/hooks/preferences.ts @@ -61,7 +61,7 @@ const BANK_PREFERENCES_KEY = buildStorageKey( ); /** * User preferences. - * + * * @returns tuple of [state, update()] */ export function usePreferences(): [ @@ -109,4 +109,3 @@ export function getLabelForPreferences( return i18n.str`Show debug info`; } } - diff --git a/packages/bank-ui/src/hooks/regional.ts b/packages/bank-ui/src/hooks/regional.ts index bf948d293..51f3edad4 100644 --- a/packages/bank-ui/src/hooks/regional.ts +++ b/packages/bank-ui/src/hooks/regional.ts @@ -31,18 +31,20 @@ import { TalerHttpError, opFixedSuccess, } from "@gnu-taler/taler-util"; +import { useState } from "preact/hooks"; import _useSWR, { SWRHook, mutate } from "swr"; import { useBankCoreApiContext } from "../context/config.js"; -import { useState } from "preact/hooks"; // FIX default import https://github.com/microsoft/TypeScript/issues/49189 const useSWR = _useSWR as unknown as SWRHook; -export type TransferCalculation = { - debit: AmountJson; - credit: AmountJson; - beforeFee: AmountJson; -} | "amount-is-too-small"; +export type TransferCalculation = + | { + debit: AmountJson; + credit: AmountJson; + beforeFee: AmountJson; + } + | "amount-is-too-small"; type EstimatorFunction = ( amount: AmountJson, fee: AmountJson, @@ -95,7 +97,7 @@ export function useCashinEstimator(): ConversionEstimators { if (resp.type === "fail") { switch (resp.case) { case HttpStatusCode.Conflict: { - return "amount-is-too-small" + return "amount-is-too-small"; } // this below can't happen case HttpStatusCode.NotImplemented: //it should not be able to call this function @@ -120,7 +122,7 @@ export function useCashinEstimator(): ConversionEstimators { if (resp.type === "fail") { switch (resp.case) { case HttpStatusCode.Conflict: { - return "amount-is-too-small" + return "amount-is-too-small"; } // this below can't happen case HttpStatusCode.NotImplemented: //it should not be able to call this function @@ -142,7 +144,7 @@ export function useCashinEstimator(): ConversionEstimators { } export function useCashoutEstimator(): ConversionEstimators { - const { bank, conversion } = useBankCoreApiContext(); + const { conversion } = useBankCoreApiContext(); return { estimateByCredit: async (fiatAmount, fee) => { const resp = await conversion.getCashoutRate({ @@ -151,7 +153,7 @@ export function useCashoutEstimator(): ConversionEstimators { if (resp.type === "fail") { switch (resp.case) { case HttpStatusCode.Conflict: { - return "amount-is-too-small" + return "amount-is-too-small"; } // this below can't happen case HttpStatusCode.NotImplemented: //it should not be able to call this function @@ -176,7 +178,7 @@ export function useCashoutEstimator(): ConversionEstimators { if (resp.type === "fail") { switch (resp.case) { case HttpStatusCode.Conflict: { - return "amount-is-too-small" + return "amount-is-too-small"; } // this below can't happen case HttpStatusCode.NotImplemented: //it should not be able to call this function @@ -201,11 +203,15 @@ export function useCashoutEstimator(): ConversionEstimators { * @deprecated use useCashoutEstimator */ export function useEstimator(): ConversionEstimators { - return useCashoutEstimator() + return useCashoutEstimator(); } export async function revalidateBusinessAccounts() { - return mutate((key) => Array.isArray(key) && key[key.length - 1] === "getAccounts", undefined, { revalidate: true }); + return mutate( + (key) => Array.isArray(key) && key[key.length - 1] === "getAccounts", + undefined, + { revalidate: true }, + ); } export function useBusinessAccounts() { const { state: credentials } = useSessionState(); @@ -247,9 +253,10 @@ export function useBusinessAccounts() { data && data.type === "ok" && data.body.accounts.length <= PAGE_SIZE; const isFirstPage = !offset; - const result = data && data.type == "ok" ? structuredClone(data.body.accounts) : [] + const result = + data && data.type == "ok" ? structuredClone(data.body.accounts) : []; if (result.length == PAGE_SIZE + 1) { - result.pop() + result.pop(); } const pagination = { result, @@ -276,7 +283,9 @@ function notUndefined(c: CashoutWithId | undefined): c is CashoutWithId { export function revalidateOnePendingCashouts() { return mutate( (key) => - Array.isArray(key) && key[key.length - 1] === "useOnePendingCashouts", undefined, { revalidate: true } + Array.isArray(key) && key[key.length - 1] === "useOnePendingCashouts", + undefined, + { revalidate: true }, ); } export function useOnePendingCashouts(account: string) { @@ -290,7 +299,8 @@ export function useOnePendingCashouts(account: string) { if (list.type !== "ok") { return list; } - const pendingCashout = list.body.cashouts.length > 0 ? list.body.cashouts[0] : undefined; + const pendingCashout = + list.body.cashouts.length > 0 ? list.body.cashouts[0] : undefined; if (!pendingCashout) return opFixedSuccess(undefined); const cashoutInfo = await api.getCashoutById( { username, token }, @@ -334,7 +344,9 @@ export function useOnePendingCashouts(account: string) { } export function revalidateCashouts() { - return mutate((key) => Array.isArray(key) && key[key.length - 1] === "useCashouts"); + return mutate( + (key) => Array.isArray(key) && key[key.length - 1] === "useCashouts", + ); } export function useCashouts(account: string) { const { state: credentials } = useSessionState(); @@ -357,7 +369,7 @@ export function useCashouts(account: string) { }), ); const cashouts = all.filter(notUndefined); - return { type: "ok" as const, body: { cashouts }}; + return { type: "ok" as const, body: { cashouts } }; } const { data, error } = useSWR< | OperationOk<{ cashouts: CashoutWithId[] }> @@ -386,7 +398,9 @@ export function useCashouts(account: string) { export function revalidateCashoutDetails() { return mutate( - (key) => Array.isArray(key) && key[key.length - 1] === "getCashoutById", undefined, { revalidate: true } + (key) => Array.isArray(key) && key[key.length - 1] === "getCashoutById", + undefined, + { revalidate: true }, ); } export function useCashoutDetails(cashoutId: number | undefined) { @@ -435,7 +449,9 @@ export type LastMonitor = { }; export function revalidateLastMonitorInfo() { return mutate( - (key) => Array.isArray(key) && key[key.length - 1] === "useLastMonitorInfo", undefined, { revalidate: true } + (key) => Array.isArray(key) && key[key.length - 1] === "useLastMonitorInfo", + undefined, + { revalidate: true }, ); } export function useLastMonitorInfo( |