-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddCrewDialog.tsx
More file actions
190 lines (178 loc) · 5.8 KB
/
AddCrewDialog.tsx
File metadata and controls
190 lines (178 loc) · 5.8 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { forwardRef, useState } from 'react';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { UserNestedDetail } from 'api/models/user-nested-detail';
import { requestCrewCreateMutation } from 'api/mutations';
import { isAxiosError } from 'axios';
import AutoCompleteStaff from 'components/AutoCompleteStaff/AutoCompleteStaff';
import { getErrorMessage } from 'helpers/ErrorMessageProvider';
import { Button } from 'primereact/button';
import { Dialog } from 'primereact/dialog';
import type { DialogProps } from 'primereact/dialog';
import { useToast } from 'providers/ToastProvider';
import { Controller, useForm } from 'react-hook-form';
import AutoCompleteCrewPosition from './AutoCompleteCrewPosition';
interface AddCrewDialogProps extends DialogProps {
requestId: number;
}
interface ICrewCreate {
member: UserNestedDetail | null;
position: string;
}
const AddCrewDialog = forwardRef<React.Ref<HTMLDivElement>, AddCrewDialogProps>(
({ onHide, requestId, visible, ...props }, ref) => {
const queryClient = useQueryClient();
const [loading, setLoading] = useState<boolean>(false);
const {
control,
formState: { isDirty },
handleSubmit,
setError,
reset,
} = useForm<ICrewCreate>({
defaultValues: { member: null, position: '' },
shouldFocusError: false,
});
const { mutateAsync } = useMutation(requestCrewCreateMutation(requestId));
const { showToast } = useToast();
const onSubmit = async (data: ICrewCreate) => {
if (!data.member) return;
setLoading(true);
await mutateAsync({ ...data, member: data.member.id })
.then(async () => {
await queryClient.invalidateQueries({
queryKey: ['requests', requestId, 'crew'],
});
onHide();
reset();
})
.catch(async (error) => {
if (isAxiosError(error)) {
if (error.response?.status === 404) {
await queryClient.invalidateQueries({
queryKey: ['requests', requestId],
});
onHide();
} else if (error.response?.status === 400) {
for (const [key, value] of Object.entries(error.response.data)) {
// @ts-expect-error: Correct types will be sent in the API error response
setError(key, { message: value, type: 'backend' });
}
return;
}
}
showToast({
detail: getErrorMessage(error),
life: 3000,
severity: 'error',
summary: 'Hiba',
});
})
.finally(() => {
setLoading(false);
});
};
const renderFooter = () => {
return (
<div>
<Button
className="p-button-text"
disabled={loading}
icon="pi pi-times"
label="Mégsem"
onClick={onHide}
/>
<Button
autoFocus
icon="pi pi-check"
label="Mentés"
loading={loading}
onClick={handleSubmit(onSubmit)}
/>
</div>
);
};
return (
<Dialog
closeOnEscape={!isDirty}
breakpoints={{ '768px': '95vw' }}
footer={renderFooter}
header="Új stábtag hozzáadása"
onHide={onHide}
style={{ width: '50vw' }}
visible={visible}
{...props}
{...ref}
>
<form className="formgrid grid p-fluid">
<div className="field col-12">
<label
className="align-items-center flex font-medium text-900 text-sm"
htmlFor="rating"
>
Stábtag
</label>
<Controller
control={control}
disabled={loading}
name="member"
render={({ field, fieldState }) => (
<>
<AutoCompleteStaff
{...field}
className="w-full"
id={field.name}
/>
{fieldState.error ? (
<small className="p-error">
{fieldState.error.message}
</small>
) : (
<small className="p-error"> </small>
)}
</>
)}
rules={{
min: { message: 'A stábtag kiválasztása kötelező!', value: 0 },
required: 'A stábtag kiválasztása kötelező!',
}}
/>
</div>
<div className="field col-12">
<label className="font-medium text-900 text-sm" htmlFor="rating">
Pozíció
</label>
<Controller
control={control}
disabled={loading}
name="position"
render={({ field, fieldState }) => (
<>
<AutoCompleteCrewPosition
{...field}
className="w-full"
id={field.name}
/>
{fieldState.error ? (
<small className="p-error">
{fieldState.error.message}
</small>
) : (
<small className="p-error"> </small>
)}
</>
)}
rules={{
maxLength: { message: 'A pozíció túl hosszú!', value: 20 },
required: 'A pozíció megadása kötelező!',
validate: (value) =>
!!value.trim() || 'A pozíció megadása kötelező!',
}}
/>
</div>
</form>
</Dialog>
);
},
);
AddCrewDialog.displayName = 'AddCrewDialog';
export default AddCrewDialog;