-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
register-form.tsx
119 lines (113 loc) · 3.47 KB
/
register-form.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
import * as React from 'react';
import { Link, useSearchParams } from 'react-router-dom';
import { Button } from '@/components/ui/button';
import { Form, Input, Select, Label, Switch } from '@/components/ui/form';
import { paths } from '@/config/paths';
import { useRegister, registerInputSchema } from '@/lib/auth';
import { Team } from '@/types/api';
type RegisterFormProps = {
onSuccess: () => void;
chooseTeam: boolean;
setChooseTeam: () => void;
teams?: Team[];
};
export const RegisterForm = ({
onSuccess,
chooseTeam,
setChooseTeam,
teams,
}: RegisterFormProps) => {
const registering = useRegister({ onSuccess });
const [searchParams] = useSearchParams();
const redirectTo = searchParams.get('redirectTo');
return (
<div>
<Form
onSubmit={(values) => {
registering.mutate(values);
}}
schema={registerInputSchema}
options={{
shouldUnregister: true,
}}
>
{({ register, formState }) => (
<>
<Input
type="text"
label="First Name"
error={formState.errors['firstName']}
registration={register('firstName')}
/>
<Input
type="text"
label="Last Name"
error={formState.errors['lastName']}
registration={register('lastName')}
/>
<Input
type="email"
label="Email Address"
error={formState.errors['email']}
registration={register('email')}
/>
<Input
type="password"
label="Password"
error={formState.errors['password']}
registration={register('password')}
/>
<div className="flex items-center space-x-2">
<Switch
checked={chooseTeam}
onCheckedChange={setChooseTeam}
className={`${
chooseTeam ? 'bg-blue-600' : 'bg-gray-200'
} relative inline-flex h-6 w-11 items-center rounded-full transition-colors focus:outline-none focus:ring-2 focus:ring-slate-500 focus:ring-offset-2`}
id="choose-team"
/>
<Label htmlFor="airplane-mode">Join Existing Team</Label>
</div>
{chooseTeam && teams ? (
<Select
label="Team"
error={formState.errors['teamId']}
registration={register('teamId')}
options={teams?.map((team) => ({
label: team.name,
value: team.id,
}))}
/>
) : (
<Input
type="text"
label="Team Name"
error={formState.errors['teamName']}
registration={register('teamName')}
/>
)}
<div>
<Button
isLoading={registering.isPending}
type="submit"
className="w-full"
>
Register
</Button>
</div>
</>
)}
</Form>
<div className="mt-2 flex items-center justify-end">
<div className="text-sm">
<Link
to={paths.auth.login.getHref(redirectTo)}
className="font-medium text-blue-600 hover:text-blue-500"
>
Log In
</Link>
</div>
</div>
</div>
);
};