-
Notifications
You must be signed in to change notification settings - Fork 0
/
Step2.tsx
108 lines (102 loc) · 3.17 KB
/
Step2.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
import { SubmitHandler, useForm } from "react-hook-form";
import { useNavigate } from "react-router-dom";
import { validate, ValidationResult } from "../../api/ValidationApi";
import Button from "../base/Button";
import {
AddEmployeeWizardInput,
useAddEmployeeWizardState,
WizardStepProps
} from "./AddEmployeeWizard";
import TextInput from "./TextInput";
/**
* Second step of the wizard allowing the user to define the title and role of a new employee.
*/
const Step2 = (props: WizardStepProps) => {
const navigate = useNavigate();
const state = useAddEmployeeWizardState((state) => state);
const {
register,
handleSubmit,
setError,
formState: { errors },
} = useForm<AddEmployeeWizardInput>();
/**
* Map the validation result to the error state of the form.
*/
const mapErrors = (result: ValidationResult) => {
if (result.errors?.title) {
setError("title", { type: "server", message: result.errors.title });
}
if (result.errors?.role) {
setError("role", { type: "server", message: result.errors.role });
}
};
/**
* Handle the submit of the form.
*
* Triggers the server side validation. If the validation succeeds the user is forwarded to the
* next step. If it fails the user is shown the error messages.
*/
const onSubmit: SubmitHandler<AddEmployeeWizardInput> = async (data) => {
const validationResult = await validate(data);
if (!validationResult.isValid && validationResult.errors) {
mapErrors(validationResult);
} else if (props.nextStepPath) {
// Send the user to the next step if the validation succeeded
navigate(props.nextStepPath);
}
};
return (
<form
className="space-y-8 divide-y divide-gray-200"
onSubmit={handleSubmit(onSubmit)}
>
<div className="pt-10 space-y-5">
<div>
<h3 className="text-lg leading-6 font-medium text-gray-900">
Job Details
</h3>
</div>
<div className="space-y-5">
{/* Title */}
<TextInput
label="Title"
{...register("title", {
onChange: (event) => {
state.updateTitle(event.currentTarget.value);
},
required: { value: true, message: "Title must be filled" },
})}
defaultValue={state.title}
error={errors.title?.message}
/>
{/* Role */}
<TextInput
label="Role"
{...register("role", {
onChange: (event) => {
state.updateRole(event.currentTarget.value);
},
required: { value: true, message: "Role must be filled" },
})}
defaultValue={state.role}
error={errors.role?.message}
/>
</div>
</div>
<div className="py-5">
<div className="flex justify-end">
<Button
text="Cancel"
onClick={() => {
state.reset();
navigate("/employees");
}}
/>
{props.nextStepPath && <Button primary text="Next" type="submit" />}
</div>
</div>
</form>
);
};
export default Step2;