summaryrefslogtreecommitdiff
path: root/packages/aml-backoffice-ui/src/hooks/useCases.ts
blob: 68deb7db965e63c14f57f21e10fa15fb2b7c4e9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { useState } from "preact/hooks";

// FIX default import https://github.com/microsoft/TypeScript/issues/49189
import { AmountString, HttpStatusCode, OfficerAccount, OperationFail, TalerExchangeApi, TalerExchangeResultByMethod, TalerHttpError } from "@gnu-taler/taler-util";
import _useSWR, { SWRHook } from "swr";
import { useExchangeApiContext } from "../context/config.js";
import { useOfficer } from "./useOfficer.js";
import { AmlExchangeBackend } from "../utils/types.js";
const useSWR = _useSWR as unknown as SWRHook;

const PAGE_SIZE = 10;
/**
 * FIXME: mutate result when balance change (transaction )
 * @param account
 * @param args
 * @returns
 */
export function useCases(state: AmlExchangeBackend.AmlState) {
  const officer = useOfficer();
  const session = officer.state === "ready" ? officer.account : undefined;
  const { api } = useExchangeApiContext();

  const [offset, setOffset] = useState<string>();

  async function fetcher([officer, state, offset]: [OfficerAccount, AmlExchangeBackend.AmlState, string | undefined]) {
    return await api.getDecisionsByState(officer, state, {
      order: "asc", offset, limit: PAGE_SIZE + 1
    })
  }

  const { data, error } = useSWR<TalerExchangeResultByMethod<"getDecisionsByState">, TalerHttpError>(
    !session ? undefined : [session, state, offset],
    fetcher,
  );

  // const [lastAfter, setLastAfter] = useState<
  //   HttpResponse<AmlExchangeBackend.AmlRecords, AmlExchangeBackend.AmlError>
  // >({ loading: true });

  // useEffect(() => {
  //   if (afterData) setLastAfter(afterData);
  // }, [afterData]);

  // if (afterError) {
  //   return afterError.cause;
  // }

  // if the query returns less that we ask, then we have reach the end or beginning
  const isLastPage =
    data && data.type === "ok" && data.body.records.length <= PAGE_SIZE;
  const isFirstPage = !offset;

  const pagination = {
    isLastPage,
    isFirstPage,
    loadMore: () => {
      if (isLastPage || data?.type !== "ok") return;
      const list = data.body.records
      setOffset(String(list[list.length - 1].rowid));
    },
    reset: () => {
      setOffset(undefined)
    },
  };

  // const public_accountslist = data?.type !== "ok" ? [] : data.body.public_accounts;
  if (!session) {
    return {
      data: {
        type: "fail",
        case: HttpStatusCode.Unauthorized,
        detail: {}
      } as OperationFail<never>
    }
  }

  if (data) {
    if (data.type === "fail") {
      return { data }
    }
    const records = isLastPage ? data.body.records : removeLastElement(data.body.records)
    return { data: { type: "ok" as const, body: { records } }, pagination }
  }
  if (error) {
    return error;
  }
  return undefined;
}

function removeLastElement<T>(list: Array<T>): Array<T> {
  if (list.length === 0) {
    return list;
  }
  return list.slice(0, -1)
}