aboutsummaryrefslogtreecommitdiff
path: root/packages/taler-wallet-webextension/src/wallet/ExchangeSetUrl.tsx
blob: e87a8894f6561c264bf4e1e20fac63b0ab87130e (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
import {
  canonicalizeBaseUrl,
  ExchangeListItem,
  i18n,
  TalerConfigResponse,
} from "@gnu-taler/taler-util";
import { Fragment, h } from "preact";
import { useEffect, useState } from "preact/hooks";
import { ErrorMessage } from "../components/ErrorMessage";
import {
  Button,
  ButtonPrimary,
  Input,
  WarningBox,
} from "../components/styled/index";

export interface Props {
  initialValue?: string;
  expectedCurrency?: string;
  knownExchanges: ExchangeListItem[];
  onCancel: () => void;
  onVerify: (s: string) => Promise<TalerConfigResponse | undefined>;
  onConfirm: (url: string) => Promise<string | undefined>;
  withError?: string;
}

export function ExchangeSetUrlPage({
  initialValue,
  knownExchanges,
  expectedCurrency,
  onCancel,
  onVerify,
  onConfirm,
  withError,
}: Props) {
  const [value, setValue] = useState<string>(initialValue || "");
  const [dirty, setDirty] = useState(false);
  const [result, setResult] = useState<TalerConfigResponse | undefined>(
    undefined,
  );
  const [error, setError] = useState<string | undefined>(withError);

  useEffect(() => {
    try {
      const url = canonicalizeBaseUrl(value);

      const found =
        knownExchanges.findIndex((e) => e.exchangeBaseUrl === url) !== -1;

      if (found) {
        setError("This exchange is already known");
        return;
      }
      onVerify(url)
        .then((r) => {
          setResult(r);
        })
        .catch(() => {
          setResult(undefined);
        });
      setDirty(true);
    } catch {
      setResult(undefined);
    }
  }, [value]);

  return (
    <Fragment>
      <section>
        {!expectedCurrency ? (
          <h1>Add new exchange</h1>
        ) : (
          <h2>Add exchange for {expectedCurrency}</h2>
        )}
        <ErrorMessage
          title={error && "Unable to add this exchange"}
          description={error}
        />
        <p>
          <Input invalid={dirty && !!error}>
            <label>URL</label>
            <input
              type="text"
              placeholder="https://"
              value={value}
              onInput={(e) => setValue(e.currentTarget.value)}
            />
          </Input>
          {result && (
            <Fragment>
              <Input>
                <label>Version</label>
                <input type="text" disabled value={result.version} />
              </Input>
              <Input>
                <label>Currency</label>
                <input type="text" disabled value={result.currency} />
              </Input>
            </Fragment>
          )}
        </p>
      </section>
      {result && expectedCurrency && expectedCurrency !== result.currency && (
        <WarningBox>
          This exchange doesn't match the expected currency{" "}
          <b>{expectedCurrency}</b>
        </WarningBox>
      )}
      <footer>
        <Button onClick={onCancel}>
          <i18n.Translate>Cancel</i18n.Translate>
        </Button>
        <ButtonPrimary
          disabled={
            !result ||
            !!error ||
            (expectedCurrency !== undefined &&
              expectedCurrency !== result.currency)
          }
          onClick={() => {
            const url = canonicalizeBaseUrl(value);
            return onConfirm(url).then((r) => (r ? setError(r) : undefined));
          }}
        >
          <i18n.Translate>Next</i18n.Translate>
        </ButtonPrimary>
      </footer>
    </Fragment>
  );
}