aboutsummaryrefslogtreecommitdiff
path: root/packages/aml-backoffice-ui/src/pages/NewFormEntry.tsx
blob: b291ffbee56350c6d0120312dc218beafc3a3dd8 (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
import { VNode, h } from "preact";
import { allForms } from "./AntiMoneyLaunderingForm.js";
import { Pages } from "../pages.js";
import { NiceForm } from "../NiceForm.js";
import { AbsoluteTime, Amounts, TalerExchangeApi, TalerProtocolTimestamp } from "@gnu-taler/taler-util";
import { AmlExchangeBackend } from "../types.js";
import { useOfficer } from "../hooks/useOfficer.js";
import { HandleAccountNotReady } from "./HandleAccountNotReady.js";
import { useExchangeApiContext } from "../context/config.js";

export function NewFormEntry({
  account,
  type,
}: {
  account?: string;
  type?: string;
}): VNode {
  const officer = useOfficer();

  if (!account) {
    return <div>no account</div>;
  }
  if (!type) {
    return <SelectForm account={account} />;
  }
  if (officer.state !== "ready") {
    return <HandleAccountNotReady officer={officer} />;
  }

  const selectedForm = Number.parseInt(type ?? "0", 10);
  if (Number.isNaN(selectedForm)) {
    return <div>WHAT! {type}</div>;
  }
  const showingFrom = allForms[selectedForm].impl;
  const formName = allForms[selectedForm].name
  const initial = {
    fullName: "loggedIn_user_fullname",
    when: AbsoluteTime.now(),
    state: AmlExchangeBackend.AmlState.pending,
    threshold: Amounts.parseOrThrow("KUDOS:1000"),
  };
  const { api } = useExchangeApiContext()

  return (
    <NiceForm
      initial={initial}
      form={showingFrom(initial)}
      onSubmit={(formValue) => {
        if (formValue.state === undefined || formValue.threshold === undefined) return;

        const justification = {
          index: selectedForm,
          name: formName,
          value: formValue
        }
        const decision: TalerExchangeApi.AmlDecision = {
          justification: JSON.stringify(justification),
          decision_time: TalerProtocolTimestamp.now(),
          h_payto: account,
          new_state: formValue.state,
          new_threshold: Amounts.stringify(formValue.threshold),
          officer_sig: "",
          kyc_requirements: undefined
        }
        // const signature = buildDecisionSignature(officer.account.signingKey, decision);
        // decision.officer_sig = signature
        api.addDecisionDetails(officer.account, decision);

        // alert(JSON.stringify(formValue));
      }}
    >
      <div class="mt-6 flex items-center justify-end gap-x-6">
        <a
          //   type="button"
          href={Pages.account.url({ account })}
          class="text-sm font-semibold leading-6 text-gray-900"
        >
          Cancel
        </a>
        <button
          type="submit"
          class="rounded-md bg-indigo-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
        >
          Confirm
        </button>
      </div>
    </NiceForm>
  );
}

function SelectForm({ account }: { account: string }) {
  return (
    <div>
      <pre>New form for account: {account}</pre>
      {allForms.map((form, idx) => {
        return (
          <a
            href={Pages.newFormEntry.url({ account, type: String(idx) })}
            class="m-4 block rounded-md w-fit border-0 p-3 py-2 text-center text-sm bg-indigo-700 text-white shadow-sm hover:bg-indigo-600"
          >
            {form.name}
          </a>
        );
      })}
    </div>
  );
}