aboutsummaryrefslogtreecommitdiff
path: root/packages/demobank-ui/src/hooks
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2023-02-17 16:23:37 -0300
committerSebastian <sebasjm@gmail.com>2023-02-17 16:23:49 -0300
commit9697e953f56dc37208c2852d686d1854256f71ef (patch)
treefbbe6e5934c1a8dd438da76d37b719372811b542 /packages/demobank-ui/src/hooks
parent8b83f729d7394837a3be231bbeeea44f6a01e9a1 (diff)
downloadwallet-core-9697e953f56dc37208c2852d686d1854256f71ef.tar.xz
cashout for business accounts
Diffstat (limited to 'packages/demobank-ui/src/hooks')
-rw-r--r--packages/demobank-ui/src/hooks/access.ts8
-rw-r--r--packages/demobank-ui/src/hooks/backend.ts23
-rw-r--r--packages/demobank-ui/src/hooks/circuit.ts136
3 files changed, 148 insertions, 19 deletions
diff --git a/packages/demobank-ui/src/hooks/access.ts b/packages/demobank-ui/src/hooks/access.ts
index 0379de27d..6046146ba 100644
--- a/packages/demobank-ui/src/hooks/access.ts
+++ b/packages/demobank-ui/src/hooks/access.ts
@@ -18,7 +18,7 @@ import {
HttpResponse,
HttpResponseOk,
HttpResponsePaginated,
- RequestError
+ RequestError,
} from "@gnu-taler/web-util/lib/index.browser";
import { useEffect, useState } from "preact/hooks";
import { useBackendContext } from "../context/backend.js";
@@ -26,12 +26,12 @@ import { MAX_RESULT_SIZE, PAGE_SIZE } from "../utils.js";
import {
useAuthenticatedBackend,
useMatchMutate,
- usePublicBackend
+ usePublicBackend,
} 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 useAccessAPI(): AccessAPI {
const mutateAll = useMatchMutate();
diff --git a/packages/demobank-ui/src/hooks/backend.ts b/packages/demobank-ui/src/hooks/backend.ts
index e87bdd5fe..e0649f5fe 100644
--- a/packages/demobank-ui/src/hooks/backend.ts
+++ b/packages/demobank-ui/src/hooks/backend.ts
@@ -118,6 +118,7 @@ interface useBackendType {
sandboxAccountsFetcher: <T>(
args: [string, number, number, string],
) => Promise<HttpResponseOk<T>>;
+ sandboxCashoutFetcher: <T>(endpoint: string[]) => Promise<HttpResponseOk<T>>;
}
export function usePublicBackend(): useBackendType {
const { state } = useBackendContext();
@@ -176,12 +177,21 @@ export function usePublicBackend(): useBackendType {
},
[baseUrl],
);
+ const sandboxCashoutFetcher = useCallback(
+ function fetcherImpl<T>([endpoint, account]: string[]): Promise<
+ HttpResponseOk<T>
+ > {
+ return requestHandler<T>(baseUrl, endpoint);
+ },
+ [baseUrl],
+ );
return {
request,
fetcher,
paginatedFetcher,
multiFetcher,
sandboxAccountsFetcher,
+ sandboxCashoutFetcher,
};
}
@@ -225,7 +235,6 @@ export function useAuthenticatedBackend(): useBackendType {
function multiFetcherImpl<T>([endpoints]: string[][]): Promise<
HttpResponseOk<T>[]
> {
- console.log("list size", endpoints.length, endpoints);
return Promise.all(
endpoints.map((endpoint) =>
requestHandler<T>(baseUrl, endpoint, { basicAuth: creds }),
@@ -249,12 +258,24 @@ export function useAuthenticatedBackend(): useBackendType {
[baseUrl],
);
+ const sandboxCashoutFetcher = useCallback(
+ function fetcherImpl<T>([endpoint, account]: string[]): Promise<
+ HttpResponseOk<T>
+ > {
+ return requestHandler<T>(baseUrl, endpoint, {
+ basicAuth: creds,
+ params: { account },
+ });
+ },
+ [baseUrl, creds],
+ );
return {
request,
fetcher,
paginatedFetcher,
multiFetcher,
sandboxAccountsFetcher,
+ sandboxCashoutFetcher,
};
}
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 };
+}