-
Notifications
You must be signed in to change notification settings - Fork 0
/
RegisterSection.tsx
161 lines (148 loc) · 5.92 KB
/
RegisterSection.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
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
import React, { useEffect, useState } from "react";
import { useInterval } from "usehooks-ts";
import { useForm } from "react-hook-form";
import { Container, Box, Typography, Button, TextField } from "@mui/material";
import { useAppDispatch, useAppSelector } from "store/store";
import { fetchPrice } from "store/slices/price.slice";
import { showSnackbar } from "store/slices/snackbar.slice";
import type { Tier } from "store/slices/tiers.slice";
import type { RegisterForm } from "store/slices/register.slice";
import "../index.css";
const REFRESH_TIME = 30;
interface Props {
tier: Tier,
onClickPay: (form: RegisterForm) => void,
submitForm: boolean
}
const RegisterSection = (props: Props) => {
const dispatch = useAppDispatch();
const { price } = useAppSelector((state) => state.price);
const { processing, errorMessage } = useAppSelector((state) => state.register);
const { register, handleSubmit, formState: { errors } } = useForm<RegisterForm>();
const [count, setCount] = useState<number>(0);
const [total, setTotal] = useState<number>(0);
useEffect(() => {
if (errorMessage !== null) {
dispatch(showSnackbar({
message: errorMessage,
severity: 'error'
}));
}
}, [errorMessage]);
useEffect(() => {
if (props.tier !== null) {
setCount(REFRESH_TIME);
dispatch(fetchPrice({}));
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [props.tier]);
useEffect(() => {
if (props.tier !== null && price > 0) {
setTotal(props.tier.usdPrice / price);
} else {
setTotal(0);
}
}, [props.tier, price]);
useInterval(
() => {
if (count <= 0) {
setCount(REFRESH_TIME);
dispatch(fetchPrice({}));
} else {
setCount(count - 1);
}
},
props.tier !== null && !processing ? 1000 : null
);
const onSubmit = (form: RegisterForm) => {
props.onClickPay({
...form,
linkedin: form.linkedin!.length > 0 ? form.linkedin : undefined,
twitter: form.twitter!.length > 0 ? form.twitter : undefined,
website: form.website!.length > 0 ? form.website : undefined,
})
}
return (
<>
<Box className="white-box pt-24 text-center">
<Typography variant="h4" className="title-text mb-2">
Auditor profile
</Typography>
<Typography variant="subtitle1" className="subtitle-text mb-16">
Please complete your user profile information
</Typography>
<Container maxWidth="sm">
<form onSubmit={handleSubmit(onSubmit)}>
<TextField
className="mb-6"
fullWidth variant="standard"
label="Company name *" type="text"
error={errors.companyName !== undefined}
helperText={errors.companyName !== undefined ? 'The field value is invalid' : undefined}
{...register("companyName", { required: true })}
/>
<TextField
className="mb-6"
fullWidth variant="standard"
label="Contact email *" type="email"
error={errors.contactEmail !== undefined}
helperText={errors.contactEmail !== undefined ? 'The field value is invalid' : undefined}
{...register("contactEmail", { required: true, pattern: /^[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-_]+\.[A-Za-z]{2,64}$/i })}
/>
<TextField
className="mb-6"
fullWidth variant="standard"
label="Company Email *" type="email"
error={errors.email !== undefined}
helperText={errors.email !== undefined ? 'The field value is invalid' : undefined}
{...register("email", { required: true, pattern: /^[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-_]+\.[A-Za-z]{2,64}$/i })}
/>
<TextField
className="mb-6"
fullWidth variant="standard"
label="Full name *" type="text"
error={errors.fullName !== undefined}
helperText={errors.fullName !== undefined ? 'The field value is invalid' : undefined}
{...register("fullName", { required: true })}
/>
<TextField
className="mb-6"
fullWidth variant="standard"
label="Twitter" type="text"
error={errors.twitter !== undefined}
helperText={errors.twitter !== undefined ? 'The field value is invalid' : undefined}
{...register("twitter", { pattern: /^[A-Za-z0-9_]{1,15}$/i })}
/>
<TextField
className="mb-6"
fullWidth variant="standard"
label="LinkedIn" type="text"
error={errors.linkedin !== undefined}
helperText={errors.linkedin !== undefined ? 'The field value is invalid' : undefined}
{...register("linkedin", { pattern: /^(http(s)?:\/\/)?([\w]+\.)?linkedin\.com\/(pub|in|profile|company)\/([a-zA-Z0-9_-]+)$/i })}
/>
<TextField
className="mb-6"
fullWidth variant="standard"
label="Website" type="text"
error={errors.website !== undefined}
helperText={errors.website !== undefined ? 'The field value is invalid' : undefined}
{...register("website", { pattern: /^(https?:\/\/)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,255}\.[a-z]{2,6}(\b([-a-zA-Z0-9@:%_\+.~#()?&\/\/=]*))?$/i })}
/>
<Button
type="submit" variant="contained" size="large"
className="mt-8 py-3 px-14 font-medium button-contained-main"
disabled={total <= 0 || processing}
>
Pay (₳{total.toFixed(2)})
</Button>
<Typography variant="body1" className="price-text">
Price will refresh in {count} second{count === 1 ? '' : 's'}
</Typography>
</form>
</Container>
</Box>
</>
);
}
export default RegisterSection;