This repository has been archived by the owner on Jul 14, 2022. It is now read-only.
/
RegisterForm.tsx
71 lines (64 loc) · 1.88 KB
/
RegisterForm.tsx
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
import "./scss/index.scss";
import * as React from "react";
import { Button, Form, TextField } from "../..";
import { maybe } from "../../../core/utils";
import { TypedCustomerRegisterMutation } from "./queries";
import { RegisterCutomer } from "./types/RegisterCutomer";
import { AlertManager, useAlert } from "react-alert";
const showSuccessNotification = (
data: RegisterCutomer,
hide: () => void,
alert: AlertManager
) => {
const successful = maybe(() => !data.accountRegister.errors.length);
if (successful) {
hide();
alert.show(
{
title: "New user has been created",
},
{ type: "success" }
);
}
};
const RegisterForm: React.FC<{ hide: () => void }> = ({ hide }) => {
const alert = useAlert();
return (
<TypedCustomerRegisterMutation
onCompleted={data => showSuccessNotification(data, hide, alert)}
>
{(registerCustomer, { loading, data }) => {
return (
<Form
errors={maybe(() => data.accountRegister.errors, [])}
onSubmit={(event, { email, password }) => {
event.preventDefault();
registerCustomer({ variables: { email, password } });
}}
>
<TextField
name="email"
autoComplete="email"
label="Email Address"
type="email"
required
/>
<TextField
name="password"
autoComplete="password"
label="Password"
type="password"
required
/>
<div className="login__content__button">
<Button type="submit" {...(loading && { disabled: true })}>
{loading ? "Loading" : "Register"}
</Button>
</div>
</Form>
);
}}
</TypedCustomerRegisterMutation>
);
};
export default RegisterForm;