aboutsummaryrefslogtreecommitdiff
path: root/packages/merchant-backoffice-ui/src/hooks/transfer.ts
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2023-01-03 01:57:39 -0300
committerSebastian <sebasjm@gmail.com>2023-01-03 01:58:18 -0300
commita2668c22f0d18386fc988f27299172145d9fa15d (patch)
tree38f06046ce4d71ee3af64ede931754bfae6dc954 /packages/merchant-backoffice-ui/src/hooks/transfer.ts
parentd1aa79eae817b1cf4c23f800308ecad101692ac7 (diff)
downloadwallet-core-a2668c22f0d18386fc988f27299172145d9fa15d.tar.xz
refactor better QA
removed axios, use fetch removed jest, added mocha and chai moved the default request handler to runtime dependency (so it can be replaced for testing) refactored ALL the test to the standard web-utils all hooks now use ONE request handler moved the tests from test folder to src
Diffstat (limited to 'packages/merchant-backoffice-ui/src/hooks/transfer.ts')
-rw-r--r--packages/merchant-backoffice-ui/src/hooks/transfer.ts80
1 files changed, 17 insertions, 63 deletions
diff --git a/packages/merchant-backoffice-ui/src/hooks/transfer.ts b/packages/merchant-backoffice-ui/src/hooks/transfer.ts
index d1ac2c285..c827772e4 100644
--- a/packages/merchant-backoffice-ui/src/hooks/transfer.ts
+++ b/packages/merchant-backoffice-ui/src/hooks/transfer.ts
@@ -13,55 +13,21 @@
You should have received a copy of the GNU General Public License along with
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
+import { useEffect, useState } from "preact/hooks";
+import useSWR from "swr";
import { MerchantBackend } from "../declaration.js";
-import { useBackendContext } from "../context/backend.js";
+import { MAX_RESULT_SIZE, PAGE_SIZE } from "../utils/constants.js";
import {
- request,
- HttpResponse,
HttpError,
+ HttpResponse,
HttpResponseOk,
HttpResponsePaginated,
- useMatchMutate,
-} from "./backend.js";
-import useSWR from "swr";
-import { useInstanceContext } from "../context/instance.js";
-import { MAX_RESULT_SIZE, PAGE_SIZE } from "../utils/constants.js";
-import { useEffect, useState } from "preact/hooks";
-
-async function transferFetcher<T>(
- url: string,
- token: string,
- backend: string,
- payto_uri?: string,
- verified?: string,
- position?: string,
- delta?: number,
-): Promise<HttpResponseOk<T>> {
- const params: any = {};
- if (payto_uri !== undefined) params.payto_uri = payto_uri;
- if (verified !== undefined) params.verified = verified;
- if (delta !== undefined) {
- params.limit = delta;
- }
- if (position !== undefined) params.offset = position;
-
- return request<T>(`${backend}${url}`, { token, params });
-}
+} from "../utils/request.js";
+import { useBackendInstanceRequest, useMatchMutate } from "./backend.js";
export function useTransferAPI(): TransferAPI {
const mutateAll = useMatchMutate();
- const { url: baseUrl, token: adminToken } = useBackendContext();
- const { token: instanceToken, id, admin } = useInstanceContext();
-
- const { url, token } = !admin
- ? {
- url: baseUrl,
- token: adminToken,
- }
- : {
- url: `${baseUrl}/instances/${id}`,
- token: instanceToken,
- };
+ const { request } = useBackendInstanceRequest();
const informTransfer = async (
data: MerchantBackend.Transfers.TransferInformation,
@@ -70,10 +36,9 @@ export function useTransferAPI(): TransferAPI {
> => {
const res =
await request<MerchantBackend.Transfers.MerchantTrackTransferResponse>(
- `${url}/private/transfers`,
+ `/private/transfers`,
{
- method: "post",
- token,
+ method: "POST",
data,
},
);
@@ -103,12 +68,7 @@ export function useInstanceTransfers(
args?: InstanceTransferFilter,
updatePosition?: (id: string) => void,
): HttpResponsePaginated<MerchantBackend.Transfers.TransferList> {
- const { url: baseUrl, token: baseToken } = useBackendContext();
- const { token: instanceToken, id, admin } = useInstanceContext();
-
- const { url, token } = !admin
- ? { url: baseUrl, token: baseToken }
- : { url: `${baseUrl}/instances/${id}`, token: instanceToken };
+ const { transferFetcher } = useBackendInstanceRequest();
const [pageBefore, setPageBefore] = useState(1);
const [pageAfter, setPageAfter] = useState(1);
@@ -129,8 +89,6 @@ export function useInstanceTransfers(
} = useSWR<HttpResponseOk<MerchantBackend.Transfers.TransferList>, HttpError>(
[
`/private/transfers`,
- token,
- url,
args?.payto_uri,
args?.verified,
args?.position,
@@ -145,8 +103,6 @@ export function useInstanceTransfers(
} = useSWR<HttpResponseOk<MerchantBackend.Transfers.TransferList>, HttpError>(
[
`/private/transfers`,
- token,
- url,
args?.payto_uri,
args?.verified,
args?.position,
@@ -185,10 +141,9 @@ export function useInstanceTransfers(
if (afterData.data.transfers.length < MAX_RESULT_SIZE) {
setPageAfter(pageAfter + 1);
} else {
- const from = `${
- afterData.data.transfers[afterData.data.transfers.length - 1]
+ const from = `${afterData.data.transfers[afterData.data.transfers.length - 1]
.transfer_serial_id
- }`;
+ }`;
if (from && updatePosition) updatePosition(from);
}
},
@@ -197,10 +152,9 @@ export function useInstanceTransfers(
if (beforeData.data.transfers.length < MAX_RESULT_SIZE) {
setPageBefore(pageBefore + 1);
} else if (beforeData) {
- const from = `${
- beforeData.data.transfers[beforeData.data.transfers.length - 1]
+ const from = `${beforeData.data.transfers[beforeData.data.transfers.length - 1]
.transfer_serial_id
- }`;
+ }`;
if (from && updatePosition) updatePosition(from);
}
},
@@ -210,9 +164,9 @@ export function useInstanceTransfers(
!beforeData || !afterData
? []
: (beforeData || lastBefore).data.transfers
- .slice()
- .reverse()
- .concat((afterData || lastAfter).data.transfers);
+ .slice()
+ .reverse()
+ .concat((afterData || lastAfter).data.transfers);
if (loadingAfter || loadingBefore)
return { loading: true, data: { transfers } };
if (beforeData && afterData) {