diff options
author | Sebastian <sebasjm@gmail.com> | 2023-02-17 16:23:37 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2023-02-17 16:23:49 -0300 |
commit | 9697e953f56dc37208c2852d686d1854256f71ef (patch) | |
tree | fbbe6e5934c1a8dd438da76d37b719372811b542 /packages/demobank-ui/src/hooks/circuit.ts | |
parent | 8b83f729d7394837a3be231bbeeea44f6a01e9a1 (diff) | |
download | wallet-core-9697e953f56dc37208c2852d686d1854256f71ef.tar.xz |
cashout for business accounts
Diffstat (limited to 'packages/demobank-ui/src/hooks/circuit.ts')
-rw-r--r-- | packages/demobank-ui/src/hooks/circuit.ts | 136 |
1 files changed, 122 insertions, 14 deletions
diff --git a/packages/demobank-ui/src/hooks/circuit.ts b/packages/demobank-ui/src/hooks/circuit.ts index 21e5ce852..c7170309b 100644 --- a/packages/demobank-ui/src/hooks/circuit.ts +++ b/packages/demobank-ui/src/hooks/circuit.ts @@ -27,8 +27,8 @@ import { MAX_RESULT_SIZE, PAGE_SIZE } from "../utils.js"; import { useAuthenticatedBackend, useMatchMutate } from "./backend.js"; // FIX default import https://github.com/microsoft/TypeScript/issues/49189 -import _useSWR, { SWRHook } from 'swr'; -const useSWR = _useSWR as unknown as SWRHook +import _useSWR, { SWRHook } from "swr"; +const useSWR = _useSWR as unknown as SWRHook; export function useAdminAccountAPI(): AdminAccountAPI { const { request } = useAuthenticatedBackend(); @@ -118,7 +118,54 @@ export function useCircuitAccountAPI(): CircuitAccountAPI { return res; }; - return { updateAccount, changePassword }; + const createCashout = async ( + data: SandboxBackend.Circuit.CashoutRequest, + ): Promise<HttpResponseOk<SandboxBackend.Circuit.CashoutPending>> => { + const res = await request<SandboxBackend.Circuit.CashoutPending>( + `circuit-api/cashouts`, + { + method: "POST", + data, + contentType: "json", + }, + ); + return res; + }; + + const confirmCashout = async ( + cashoutId: string, + data: SandboxBackend.Circuit.CashoutConfirm, + ): Promise<HttpResponseOk<void>> => { + const res = await request<void>( + `circuit-api/cashouts/${cashoutId}/confirm`, + { + method: "POST", + data, + contentType: "json", + }, + ); + await mutateAll(/.*circuit-api\/cashout.*/); + return res; + }; + + const abortCashout = async ( + cashoutId: string, + ): Promise<HttpResponseOk<void>> => { + const res = await request<void>(`circuit-api/cashouts/${cashoutId}/abort`, { + method: "POST", + contentType: "json", + }); + await mutateAll(/.*circuit-api\/cashout.*/); + return res; + }; + + return { + updateAccount, + changePassword, + createCashout, + confirmCashout, + abortCashout, + }; } export interface AdminAccountAPI { @@ -144,11 +191,14 @@ export interface CircuitAccountAPI { changePassword: ( data: SandboxBackend.Circuit.AccountPasswordChange, ) => Promise<HttpResponseOk<void>>; -} - -export interface InstanceTemplateFilter { - //FIXME: add filter to the template list - position?: string; + createCashout: ( + data: SandboxBackend.Circuit.CashoutRequest, + ) => Promise<HttpResponseOk<SandboxBackend.Circuit.CashoutPending>>; + confirmCashout: ( + id: string, + data: SandboxBackend.Circuit.CashoutConfirm, + ) => Promise<HttpResponseOk<void>>; + abortCashout: (id: string) => Promise<HttpResponseOk<void>>; } async function getBusinessStatus( @@ -217,6 +267,35 @@ export function useBusinessAccountDetails( return { loading: true }; } +export function useRatiosAndFeeConfig(): HttpResponse< + SandboxBackend.Circuit.Config, + SandboxBackend.SandboxError +> { + const { fetcher } = useAuthenticatedBackend(); + + const { data, error } = useSWR< + HttpResponseOk<SandboxBackend.Circuit.Config>, + RequestError<SandboxBackend.SandboxError> + >([`circuit-api/config`], fetcher, { + refreshInterval: 0, + refreshWhenHidden: false, + revalidateOnFocus: false, + revalidateOnReconnect: false, + refreshWhenOffline: false, + errorRetryCount: 0, + errorRetryInterval: 1, + shouldRetryOnError: false, + keepPreviousData: true, + }); + + if (data) { + data.data.currency = "FIAT"; + } + if (data) return data; + if (error) return error.info; + return { loading: true }; +} + interface PaginationFilter { account?: string; page?: number; @@ -299,17 +378,18 @@ export function useBusinessAccounts( return { loading: true }; } -export function useCashouts(): HttpResponse< - (SandboxBackend.Circuit.CashoutStatusResponse & WithId)[], +export function useCashouts( + account: string, +): HttpResponse< + SandboxBackend.Circuit.CashoutStatusResponseWithId[], SandboxBackend.SandboxError > { - const { fetcher, multiFetcher } = useAuthenticatedBackend(); - + const { sandboxCashoutFetcher, multiFetcher } = useAuthenticatedBackend(); const { data: list, error: listError } = useSWR< HttpResponseOk<SandboxBackend.Circuit.Cashouts>, RequestError<SandboxBackend.SandboxError> - >([`circuit-api/cashouts`], fetcher, { + >([`circuit-api/cashouts`, account], sandboxCashoutFetcher, { refreshInterval: 0, refreshWhenHidden: false, revalidateOnFocus: false, @@ -317,7 +397,7 @@ export function useCashouts(): HttpResponse< refreshWhenOffline: false, }); - const paths = (list?.data.cashouts || []).map( + const paths = ((list?.data && list?.data.cashouts) || []).map( (cashoutId) => `circuit-api/cashouts/${cashoutId}`, ); const { data: cashouts, error: productError } = useSWR< @@ -346,3 +426,31 @@ export function useCashouts(): HttpResponse< } return { loading: true }; } + +export function useCashoutDetails( + id: string, +): HttpResponse< + SandboxBackend.Circuit.CashoutStatusResponse, + SandboxBackend.SandboxError +> { + const { fetcher } = useAuthenticatedBackend(); + + const { data, error } = useSWR< + HttpResponseOk<SandboxBackend.Circuit.CashoutStatusResponse>, + RequestError<SandboxBackend.SandboxError> + >([`circuit-api/cashouts/${id}`], fetcher, { + refreshInterval: 0, + refreshWhenHidden: false, + revalidateOnFocus: false, + revalidateOnReconnect: false, + refreshWhenOffline: false, + errorRetryCount: 0, + errorRetryInterval: 1, + shouldRetryOnError: false, + keepPreviousData: true, + }); + + if (data) return data; + if (error) return error.info; + return { loading: true }; +} |