-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAcceptRejectDialog.tsx
More file actions
192 lines (181 loc) · 4.98 KB
/
AcceptRejectDialog.tsx
File metadata and controls
192 lines (181 loc) · 4.98 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
191
192
import { forwardRef, useEffect } from 'react';
import { isAdmin } from 'helpers/LocalStorageHelper';
import { Button } from 'primereact/button';
import { Dialog } from 'primereact/dialog';
import type { DialogProps } from 'primereact/dialog';
import { SelectButton } from 'primereact/selectbutton';
import { ToggleButton } from 'primereact/togglebutton';
import { classNames } from 'primereact/utils';
import type { IconType } from 'primereact/utils';
import { Controller, useForm } from 'react-hook-form';
interface AcceptRejectDialogProps extends DialogProps {
accepted: boolean | null | undefined;
canceled: boolean | null | undefined;
failed: boolean | null | undefined;
loading: boolean;
onSave(data: {
accepted: boolean | null;
canceled: boolean | null;
failed: boolean | null;
}): void;
}
type AcceptOption = {
className: string;
icon: IconType<AcceptOption>;
label: string;
value: boolean;
};
const AcceptRejectDialog = forwardRef<
React.Ref<HTMLDivElement>,
AcceptRejectDialogProps
>(
(
{ accepted, canceled, failed, loading, onHide, onSave, visible, ...props },
ref,
) => {
const {
control,
formState: { isDirty },
handleSubmit,
reset,
} = useForm<{
accepted: boolean | null;
canceled: boolean | null;
failed: boolean | null;
}>();
useEffect(() => {
const defaultValues = {
accepted: accepted === undefined ? null : accepted,
canceled: canceled || null,
failed: failed || null,
};
reset({ ...defaultValues });
}, [accepted, canceled, failed, reset, visible]);
const acceptOptions: AcceptOption[] = [
{
className: 'w-6',
icon: 'bi bi-hand-thumbs-up-fill',
label: 'Elfogadva',
value: true,
},
{
className: 'w-6',
icon: 'bi bi-hand-thumbs-down-fill',
label: 'Elutasítva',
value: false,
},
];
const acceptTemplate = (option: AcceptOption) => {
return (
<>
<i
className={classNames(
'p-button-icon p-c p-button-icon-left',
option.icon,
)}
/>
<span className="p-button-label p-c text-center">{option.label}</span>
</>
);
};
const renderFooter = () => {
if (!isAdmin()) return;
return (
<div>
<Button
className="p-button-secondary p-button-text"
disabled={!isDirty || loading}
icon="pi pi-refresh"
label="Visszaállítás"
onClick={() => {
reset();
}}
/>
<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(onSave)}
/>
</div>
);
};
return (
<Dialog
contentClassName="align-item-center flex flex-column justify-content-center"
breakpoints={{ '768px': '95vw' }}
footer={renderFooter}
header="Felkérés elfogadása"
onHide={onHide}
style={{ width: '50vw' }}
visible={visible}
{...props}
{...ref}
>
<Controller
control={control}
disabled={!isAdmin()}
name="accepted"
render={({ field }) => (
<SelectButton
{...field}
className="my-2"
id={field.name}
itemTemplate={acceptTemplate}
optionLabel="label"
options={acceptOptions}
/>
)}
/>
<Controller
control={control}
disabled={!isAdmin()}
name="canceled"
render={({ field }) => (
<ToggleButton
{...field}
checked={field.value || false}
className="my-2"
id={field.name}
offIcon="bi bi-person-fill-x"
offLabel="Szervezők által lemondva"
onChange={field.onChange}
onIcon="bi bi-person-fill-x"
onLabel="Szervezők által lemondva"
value={undefined}
/>
)}
/>
<Controller
control={control}
disabled={!isAdmin()}
name="failed"
render={({ field }) => (
<ToggleButton
{...field}
checked={field.value || false}
className="mt-2"
id={field.name}
offIcon="bi bi-fire"
offLabel="Meghiúsult"
onChange={field.onChange}
onIcon="bi bi-fire"
onLabel="Meghiúsult"
value={undefined}
/>
)}
/>
</Dialog>
);
},
);
AcceptRejectDialog.displayName = 'AcceptRejectDialog';
export default AcceptRejectDialog;