summaryrefslogtreecommitdiff
path: root/packages/aml-backoffice-ui/src/hooks/useCases.ts
blob: 1cd16a6f62bf08580f3f45baf59cf4afb0dcade9 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { useState } from "preact/hooks";

// FIX default import https://github.com/microsoft/TypeScript/issues/49189
import { AmountString, 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: "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;
}

const example1: TalerExchangeApi.AmlRecords = {
  records: [
    {
      current_state: 0,
      h_payto: "QWEQWEQWEQWEWQE",
      rowid: 1,
      threshold: "USD 100" as AmountString,
    },
    {
      current_state: 1,
      h_payto: "ASDASDASD",
      rowid: 1,
      threshold: "USD 100" as AmountString,
    },
    {
      current_state: 2,
      h_payto: "ZXCZXCZXCXZC",
      rowid: 1,
      threshold: "USD 1000" as AmountString,
    },
    {
      current_state: 0,
      h_payto: "QWEQWEQWEQWEWQE",
      rowid: 1,
      threshold: "USD 100" as AmountString,
    },
    {
      current_state: 1,
      h_payto: "ASDASDASD",
      rowid: 1,
      threshold: "USD 100" as AmountString,
    },
    {
      current_state: 2,
      h_payto: "ZXCZXCZXCXZC",
      rowid: 1,
      threshold: "USD 1000" as AmountString,
    },
  ].map((e, idx) => {
    e.rowid = idx;
    e.threshold = `${e.threshold}${idx}` as AmountString;
    return e;
  }),
};


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