aboutsummaryrefslogtreecommitdiff
path: root/packages/aml-backoffice-ui/src/pages/UnlockAccount.tsx
blob: 72656bb986a3b743bd7d4d463ebe0e36c888dc59 (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
/*
 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 {
  Button,
  InputLine,
  LocalNotificationBanner,
  UIHandlerId,
  useLocalNotificationHandler,
  useTranslationContext,
} from "@gnu-taler/web-util/browser";
import { VNode, h } from "preact";
import { FormErrors, useFormState } from "../hooks/form.js";
import { useOfficer } from "../hooks/officer.js";
import { undefinedIfEmpty } from "./CreateAccount.js";

type FormType = {
  password: string;
};

export function UnlockAccount(): VNode {
  const { i18n } = useTranslationContext();

  const officer = useOfficer();
  const [notification, withErrorHandler] = useLocalNotificationHandler();

  const { handler, status } = useFormState<FormType>(
    [".password"] as Array<UIHandlerId>,
    {
      password: undefined,
    },
    (state) => {
      const errors = undefinedIfEmpty<FormErrors<FormType>>({
        password: !state.password ? i18n.str`required` : undefined,
      });
      if (errors === undefined) {
        return {
          status: "ok",
          result: state as FormType,
          errors,
        };
      }
      return {
        status: "fail",
        result: state,
        errors,
      };
    },
  );

  const unlockHandler =
    status.status === "fail" || officer.state !== "locked"
      ? undefined
      : withErrorHandler(
          async () => officer.tryUnlock(handler.password!.value!),
          () => {},
        );

  const forgetHandler =
    status.status === "fail" || officer.state !== "locked"
      ? undefined
      : withErrorHandler(
          async () => officer.forget(),
          () => {},
        );

  return (
    <div class="flex min-h-full flex-col ">
      <LocalNotificationBanner notification={notification} />

      <div class="sm:mx-auto sm:w-full sm:max-w-md">
        <h1 class="mt-6 text-center text-2xl font-bold leading-9 tracking-tight text-gray-900">
          <i18n.Translate>Account locked</i18n.Translate>
        </h1>
        <p class="mt-6 text-lg leading-8 text-gray-600">
          <i18n.Translate>
            Your account is normally locked anytime you reload. To unlock type
            your password again.
          </i18n.Translate>
        </p>
      </div>

      <div class="mt-10 sm:mx-auto sm:w-full sm:max-w-[480px] ">
        <div class="bg-gray-100 px-6 py-6 shadow sm:rounded-lg sm:px-12">
          <div class="mb-4">
            <InputLine<FormType, "password">
              label={i18n.str`Password`}
              name="password"
              type="password"
              required
              handler={handler.password}
            />
          </div>

          <div class="mt-8">
            <Button
              type="submit"
              handler={unlockHandler}
              disabled={!unlockHandler}
              class="disabled:opacity-50 disabled:cursor-default  flex w-full justify-center rounded-md bg-indigo-600 px-3 py-1.5 text-sm font-semibold leading-6 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"
            >
              <i18n.Translate>Unlock</i18n.Translate>
            </Button>
          </div>
        </div>
        <Button
          type="button"
          handler={forgetHandler}
          disabled={!forgetHandler}
          class="disabled:opacity-50 disabled:cursor-default m-4 block rounded-md bg-red-600 px-3 py-2 text-center text-sm  text-white shadow-sm hover:bg-red-500 "
        >
          <i18n.Translate>Forget account</i18n.Translate>
        </Button>
      </div>
    </div>
  );
}