summaryrefslogtreecommitdiff
path: root/packages/demobank-ui/src/pages/admin/Home.tsx
blob: 5033b7fdc846bd5b79c646133076c50cecf3e98a (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
import { notifyInfo, useTranslationContext } from "@gnu-taler/web-util/browser";
import { Fragment, VNode, h } from "preact";
import { useState } from "preact/hooks";
import { Cashouts } from "../../components/Cashouts/index.js";
import { ShowCashoutDetails } from "../business/Home.js";
import { handleNotOkResult } from "../HomePage.js";
import { ShowAccountDetails } from "../ShowAccountDetails.js";
import { UpdateAccountPassword } from "../UpdateAccountPassword.js";
import { AdminAccount } from "./Account.js";
import { AccountList } from "./AccountList.js";
import { CreateNewAccount } from "./CreateNewAccount.js";
import { RemoveAccount } from "./RemoveAccount.js";

/**
 * Query account information and show QR code if there is pending withdrawal
 */
interface Props {
  onRegister: () => void;
}
export type AccountAction = "show-details" |
  "show-cashout" |
  "update-password" |
  "remove-account" |
  "show-cashouts-details";

export function AdminHome({ onRegister }: Props): VNode {
  const [action, setAction] = useState<{
    type: AccountAction,
    account: string
  } | undefined>()

  const [createAccount, setCreateAccount] = useState(false);

  const { i18n } = useTranslationContext();

  if (action) {
    switch (action.type) {
      case "show-cashouts-details": return <ShowCashoutDetails
        id={action.account}
        onLoadNotOk={handleNotOkResult(i18n, onRegister)}
        onCancel={() => {
          setAction(undefined);
        }}
      />
      case "show-cashout": return (
        <div>
          <div>
            <h1 class="nav welcome-text">
              <i18n.Translate>Cashout for account {action.account}</i18n.Translate>
            </h1>
          </div>
          <Cashouts
            account={action.account}
            onSelected={(id) => {
              setAction({
                type: "show-cashouts-details",
                account: action.account
              });
            }}
          />
          <p>
            <input
              class="pure-button"
              type="submit"
              value={i18n.str`Close`}
              onClick={async (e) => {
                e.preventDefault();
                setAction(undefined);
              }}
            />
          </p>
        </div>
      )
      case "update-password": return <UpdateAccountPassword
        account={action.account}
        onLoadNotOk={handleNotOkResult(i18n, onRegister)}
        onUpdateSuccess={() => {
          notifyInfo(i18n.str`Password changed`);
          setAction(undefined);
        }}
        onCancel={() => {
          setAction(undefined);
        }}
      />
      case "remove-account": return <RemoveAccount
        account={action.account}
        onLoadNotOk={handleNotOkResult(i18n, onRegister)}
        onUpdateSuccess={() => {
          notifyInfo(i18n.str`Account removed`);
          setAction(undefined);
        }}
        onCancel={() => {
          setAction(undefined);
        }}
      />
      case "show-details": return <ShowAccountDetails
        account={action.account}
        onLoadNotOk={handleNotOkResult(i18n, onRegister)}
        onChangePassword={() => {
          setAction({
            type: "update-password",
            account: action.account,
          })
        }}
        onUpdateSuccess={() => {
          notifyInfo(i18n.str`Account updated`);
          setAction(undefined);
        }}
        onClear={() => {
          setAction(undefined);
        }}
      />
    }
  }

  if (createAccount) {
    return (
      <CreateNewAccount
        onCancel={() => setCreateAccount(false)}
        onCreateSuccess={(password) => {
          notifyInfo(
            i18n.str`Account created with password "${password}". The user must change the password on the next login.`,
          );
          setCreateAccount(false);
        }}
      />
    );
  }

  return (
    <Fragment>

      <AccountList
        onCreateAccount={() => {
          setCreateAccount(true);
        }}
        account={undefined}
        onAction={(type, account) => setAction({ account, type })}
        onRegister={onRegister}
      />

      <AdminAccount onRegister={onRegister} />

    </Fragment>
  );
}