aboutsummaryrefslogtreecommitdiff
path: root/packages/bank-ui/src/pages/PaymentOptions.tsx
blob: a31dd07140abec49cb00bed943a7ea93254013e8 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
 This file is part of GNU Taler
 (C) 2022-2024 Taler Systems S.A.

 GNU Taler is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 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 { AmountJson, TalerError } from "@gnu-taler/taler-util";
import { useTranslationContext } from "@gnu-taler/web-util/browser";
import { Fragment, VNode, h } from "preact";
import { useEffect } from "preact/hooks";
import { useWithdrawalDetails } from "../hooks/account.js";
import { useBankState } from "../hooks/bank-state.js";
import { useSessionState } from "../hooks/session.js";
import { RouteDefinition } from "@gnu-taler/web-util/browser";
import { PaytoWireTransferForm } from "./PaytoWireTransferForm.js";
import { WalletWithdrawForm } from "./WalletWithdrawForm.js";

function ShowOperationPendingTag({
  woid,
  onOperationAlreadyCompleted,
}: {
  woid: string;
  onOperationAlreadyCompleted?: () => void;
}): VNode {
  const { i18n } = useTranslationContext();
  const { state: credentials } = useSessionState();
  const result = useWithdrawalDetails(woid);
  const loading = !result;
  const error =
    !loading && (result instanceof TalerError || result.type === "fail");
  const pending =
    !loading &&
    !error &&
    result.body.status === "selected" &&
    // (result.body.status === "pending" || result.body.status === "selected") &&
    credentials.status === "loggedIn" &&
    credentials.username === result.body.username;

  if (error || !pending) {
    return <Fragment />;
  }

  return (
    <span class="flex items-center gap-x-1.5 w-fit rounded-md bg-yellow-100 px-2 py-1 text-xs font-medium text-yellow-700 whitespace-pre">
      <svg
        class="h-1.5 w-1.5 fill-yellow-500"
        viewBox="0 0 6 6"
        aria-hidden="true"
      >
        <circle cx="3" cy="3" r="3" />
      </svg>
      <i18n.Translate>Pending operation</i18n.Translate>
    </span>
  );
}

/**
 * Let the user choose a payment option,
 * then specify the details trigger the action.
 */
export function PaymentOptions({
  routeClose,
  routeCashout,
  routeChargeWallet,
  routeWireTransfer,
  tab,
  limit,
  balance,
  onOperationCreated,
  onClose,
  routeOperationDetails,
  onAuthorizationRequired,
}: {
  limit: AmountJson;
  balance: AmountJson;
  tab: "charge-wallet" | "wire-transfer" | undefined;
  onAuthorizationRequired: () => void;
  onOperationCreated: (wopid: string) => void;
  onClose: () => void;

  routeOperationDetails: RouteDefinition<{ wopid: string }>;
  routeClose: RouteDefinition;
  routeCashout: RouteDefinition;
  routeChargeWallet: RouteDefinition;
  routeWireTransfer: RouteDefinition<{
    account?: string;
    subject?: string;
    amount?: string;
  }>;
}): VNode {
  const { i18n } = useTranslationContext();
  const [bankState, updateBankState] = useBankState();

  return (
    <div class="mt-4">
      <fieldset>
        <legend class="px-4 text-base font-semibold leading-6 text-gray-900">
          <i18n.Translate>Send money</i18n.Translate>
        </legend>

        <div class="px-4 mt-4 grid grid-cols-1 gap-y-6 sm:grid-cols-2 sm:gap-x-4">
          {/* <!-- Active: "border-indigo-600 ring-2 ring-indigo-600", Not Active: "border-gray-300" --> */}
          <a name="charge wallet" href={routeChargeWallet.url({})}>
            <label
              class={
                "relative flex cursor-pointer rounded-lg border bg-white p-4 shadow-sm focus:outline-none" +
                (tab === "charge-wallet"
                  ? "border-indigo-600 ring-2 ring-indigo-600"
                  : "border-gray-300")
              }
            >
              <div class="flex flex-col">
                <span class="flex">
                  <div class="text-4xl mr-4 my-auto">&#x1F4B5;</div>
                  <span class="grow self-center text-lg text-gray-900 align-middle text-center">
                    <i18n.Translate>to a Taler wallet</i18n.Translate>
                  </span>
                  <svg
                    class="self-center flex-none h-5 w-5 text-indigo-600"
                    style={{
                      visibility:
                        tab === "charge-wallet" ? "visible" : "hidden",
                    }}
                    viewBox="0 0 20 20"
                    fill="currentColor"
                    aria-hidden="true"
                  >
                    <path
                      fill-rule="evenodd"
                      d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.857-9.809a.75.75 0 00-1.214-.882l-3.483 4.79-1.88-1.88a.75.75 0 10-1.06 1.061l2.5 2.5a.75.75 0 001.137-.089l4-5.5z"
                      clip-rule="evenodd"
                    />
                  </svg>
                </span>
                <div class="mt-1 flex items-center text-sm text-gray-500">
                  <i18n.Translate>
                    Withdraw digital money into your mobile wallet or browser
                    extension
                  </i18n.Translate>
                </div>
                {!!bankState.currentWithdrawalOperationId && (
                  <ShowOperationPendingTag
                    woid={bankState.currentWithdrawalOperationId}
                    onOperationAlreadyCompleted={() => {
                      updateBankState(
                        "currentWithdrawalOperationId",
                        undefined,
                      );
                    }}
                  />
                )}
              </div>
            </label>
          </a>

          <a name="wire transfer" href={routeWireTransfer.url({})}>
            <label
              class={
                "relative flex cursor-pointer rounded-lg border bg-white p-4 shadow-sm focus:outline-none" +
                (tab === "wire-transfer"
                  ? "border-indigo-600 ring-2 ring-indigo-600"
                  : "border-gray-300")
              }
            >
              <div class="flex flex-col">
                <span class="flex">
                  <div class="text-4xl mr-4 my-auto">&#x2194;</div>
                  <span class="grow self-center text-lg font-medium text-gray-900 align-middle text-center">
                    <i18n.Translate>to another bank account</i18n.Translate>
                  </span>
                  <svg
                    class="self-center flex-none h-5 w-5 text-indigo-600"
                    style={{
                      visibility:
                        tab === "wire-transfer" ? "visible" : "hidden",
                    }}
                    viewBox="0 0 20 20"
                    fill="currentColor"
                    aria-hidden="true"
                  >
                    <path
                      fill-rule="evenodd"
                      d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.857-9.809a.75.75 0 00-1.214-.882l-3.483 4.79-1.88-1.88a.75.75 0 10-1.06 1.061l2.5 2.5a.75.75 0 001.137-.089l4-5.5z"
                      clip-rule="evenodd"
                    />
                  </svg>
                </span>
                <div class="mt-1 flex items-center text-sm text-gray-500">
                  <i18n.Translate>
                    Make a wire transfer to an account with known bank account
                    number.
                  </i18n.Translate>
                </div>
              </div>
            </label>
          </a>
        </div>
        {tab === "charge-wallet" && (
          <WalletWithdrawForm
            routeOperationDetails={routeOperationDetails}
            focus
            limit={limit}
            balance={balance}
            onAuthorizationRequired={onAuthorizationRequired}
            onOperationCreated={onOperationCreated}
            onOperationAborted={onClose}
            routeCancel={routeClose}
          />
        )}
        {tab === "wire-transfer" && (
          <PaytoWireTransferForm
            focus
            routeHere={routeWireTransfer}
            limit={limit}
            balance={balance}
            onAuthorizationRequired={onAuthorizationRequired}
            onSuccess={onClose}
            routeCashout={routeCashout}
            routeCancel={routeClose}
          />
        )}
      </fieldset>
    </div>
  );
}