1
+ import { useCallback , useMemo } from 'react' ;
1
2
import Select from 'react-select' ;
2
3
import { useFormik } from 'formik' ;
3
4
import * as Yup from 'yup' ;
@@ -15,38 +16,99 @@ import {
15
16
DialogTitle ,
16
17
} from '@/components/ui/dialog' ;
17
18
import {
18
- IFilterFormProps ,
19
+ IFilterFormikProps ,
19
20
IFilterProps ,
20
21
ShelterAvailabilityStatus ,
21
22
} from './types' ;
22
23
import { priorityOptions } from '@/lib/utils' ;
23
24
import { ISupply , SupplyPriority } from '@/service/supply/types' ;
24
- import { useCallback } from 'react' ;
25
+
26
+ const ShelterAvailabilityStatusMapped : Record <
27
+ ShelterAvailabilityStatus ,
28
+ string
29
+ > = {
30
+ available : 'Abrigo Disponivel' ,
31
+ unavailable : 'Abrigo Indisponivel' ,
32
+ waiting : 'Sem informação de disponibilidade' ,
33
+ } ;
34
+
35
+ const priorityOpts = Object . entries ( priorityOptions ) . reduce (
36
+ ( prev , [ priority , label ] ) =>
37
+ priority === `${ SupplyPriority . NotNeeded } `
38
+ ? prev
39
+ : { ...prev , [ priority ] : label } ,
40
+ { } as Record < SupplyPriority , string >
41
+ ) ;
25
42
26
43
const Filter = ( props : IFilterProps ) => {
27
44
const { data, onClose, onSubmit, open } = props ;
28
45
const { data : supplies , loading : loadingSupplies } = useSupplies ( ) ;
29
46
const { data : supplyCategories , loading : loadingSupplyCategories } =
30
47
useSupplyCategories ( ) ;
31
- const { handleSubmit, values, setFieldValue } = useFormik < IFilterFormProps > ( {
32
- initialValues : data ,
33
- enableReinitialize : true ,
34
- validateOnChange : false ,
35
- validateOnBlur : false ,
36
- validateOnMount : false ,
37
- validationSchema : Yup . object ( ) . shape ( {
38
- search : Yup . string ( ) ,
39
- } ) ,
40
- onSubmit,
41
- } ) ;
48
+ const mappedSupplyCategories = useMemo ( ( ) => {
49
+ return supplyCategories . reduce (
50
+ ( prev , current ) => ( { ...prev , [ current . id ] : current } ) ,
51
+ { } as Record < string , ISupplyCategory >
52
+ ) ;
53
+ } , [ supplyCategories ] ) ;
54
+ const mappedSupplies = useMemo ( ( ) => {
55
+ return supplies . reduce (
56
+ ( prev , current ) => ( { ...prev , [ current . id ] : current } ) ,
57
+ { } as Record < string , ISupply >
58
+ ) ;
59
+ } , [ supplies ] ) ;
60
+ const { handleSubmit, values, setFieldValue } = useFormik < IFilterFormikProps > (
61
+ {
62
+ initialValues : {
63
+ priority : {
64
+ value : data . priority ?? SupplyPriority . Urgent ,
65
+ label : priorityOpts [ data . priority ?? SupplyPriority . Urgent ] ,
66
+ } ,
67
+ search : data . search ,
68
+ shelterStatus : data . shelterStatus . map ( ( s ) => ( {
69
+ label : ShelterAvailabilityStatusMapped [ s ] ,
70
+ value : s ,
71
+ } ) ) ,
72
+ supplyCategories : data . supplyCategoryIds . map ( ( id ) => ( {
73
+ label : mappedSupplyCategories [ id ] ?. name ,
74
+ value : id ,
75
+ } ) ) ,
76
+ supplies : data . supplyIds . map ( ( id ) => ( {
77
+ value : id ,
78
+ label : mappedSupplies [ id ] ?. name ,
79
+ } ) ) ,
80
+ } ,
81
+ enableReinitialize : true ,
82
+ validateOnChange : false ,
83
+ validateOnBlur : false ,
84
+ validateOnMount : false ,
85
+ validationSchema : Yup . object ( ) . shape ( {
86
+ search : Yup . string ( ) ,
87
+ } ) ,
88
+ onSubmit : ( values ) => {
89
+ const { priority, search, shelterStatus, supplies, supplyCategories } =
90
+ values ;
91
+ onSubmit ( {
92
+ priority : priority ?. value ? + priority . value : null ,
93
+ search,
94
+ shelterStatus : shelterStatus . map ( ( s ) => s . value ) ,
95
+ supplyCategoryIds : supplyCategories . map ( ( s ) => s . value ) ,
96
+ supplyIds : supplies . map ( ( s ) => s . value ) ,
97
+ } ) ;
98
+ } ,
99
+ }
100
+ ) ;
42
101
43
102
const handleToggleShelterStatus = useCallback (
44
- ( checked : boolean , value : ShelterAvailabilityStatus ) => {
103
+ ( checked : boolean , status : ShelterAvailabilityStatus ) => {
45
104
setFieldValue (
46
105
'shelterStatus' ,
47
106
checked
48
- ? [ ...values . shelterStatus , value ]
49
- : values . shelterStatus . filter ( ( s ) => s !== value )
107
+ ? [
108
+ ...values . shelterStatus ,
109
+ { label : ShelterAvailabilityStatusMapped [ status ] , value : status } ,
110
+ ]
111
+ : values . shelterStatus . filter ( ( s ) => s . value !== status )
50
112
) ;
51
113
} ,
52
114
[ setFieldValue , values . shelterStatus ]
@@ -88,31 +150,38 @@ const Filter = (props: IFilterProps) => {
88
150
</ label >
89
151
< Select
90
152
placeholder = "Selecione"
91
- options = { Object . entries ( priorityOptions ) . map (
92
- ( [ priority , label ] ) => ( { label, value : priority } as any )
153
+ value = { {
154
+ label :
155
+ priorityOpts [
156
+ values . priority ?. value ?? SupplyPriority . Urgent
157
+ ] ,
158
+ value : values . priority ?. value ?? SupplyPriority . Needing ,
159
+ } }
160
+ options = { Object . entries ( priorityOpts ) . map (
161
+ ( [ priority , label ] ) => ( { label, value : + priority } as any )
93
162
) }
94
- onChange = { (
95
- v : { label : string ; value : SupplyPriority } | null
96
- ) => setFieldValue ( 'priority' , v ?. value ) }
163
+ onChange = { ( v ) => {
164
+ const newValue = {
165
+ ...v ,
166
+ value : v ? + v . value : SupplyPriority . Urgent ,
167
+ } ;
168
+ setFieldValue ( 'priority' , newValue ) ;
169
+ } }
97
170
/>
98
171
</ div >
99
172
< div className = "flex flex-col gap-1 w-full" >
100
173
< label className = "text-muted-foreground text-sm md:text-lg font-medium" >
101
174
Categoria
102
175
</ label >
103
176
< Select
177
+ value = { values . supplyCategories }
104
178
placeholder = "Selecione"
105
179
isMulti
106
180
options = { supplyCategories . map ( ( el : ISupplyCategory ) => ( {
107
181
label : el . name ,
108
182
value : el . id ,
109
183
} ) ) }
110
- onChange = { ( v ) =>
111
- setFieldValue (
112
- 'supplyCategoryIds' ,
113
- v . map ( ( s ) => s . value )
114
- )
115
- }
184
+ onChange = { ( v ) => setFieldValue ( 'supplyCategories' , v ) }
116
185
/>
117
186
</ div >
118
187
< div className = "flex flex-col w-full" >
@@ -122,16 +191,12 @@ const Filter = (props: IFilterProps) => {
122
191
< Select
123
192
placeholder = "Selecione"
124
193
isMulti
194
+ value = { values . supplies }
125
195
options = { supplies . map ( ( el : ISupply ) => ( {
126
196
label : el . name ,
127
197
value : el . id ,
128
198
} ) ) }
129
- onChange = { ( v ) =>
130
- setFieldValue (
131
- 'supplyIds' ,
132
- v . map ( ( s ) => s . value )
133
- )
134
- }
199
+ onChange = { ( v ) => setFieldValue ( 'supplies' , v ) }
135
200
/>
136
201
</ div >
137
202
</ div >
@@ -148,7 +213,9 @@ const Filter = (props: IFilterProps) => {
148
213
onChange = { ( ev ) =>
149
214
handleToggleShelterStatus ( ev . target . checked , 'available' )
150
215
}
151
- defaultChecked = { values . shelterStatus . includes ( 'available' ) }
216
+ defaultChecked = { values . shelterStatus . some (
217
+ ( s ) => s . value === 'available'
218
+ ) }
152
219
/>
153
220
Abrigo Disponivel
154
221
</ label >
@@ -164,8 +231,8 @@ const Filter = (props: IFilterProps) => {
164
231
'unavailable'
165
232
)
166
233
}
167
- defaultChecked = { values . shelterStatus . includes (
168
- 'unavailable'
234
+ defaultChecked = { values . shelterStatus . some (
235
+ ( s ) => s . value === 'unavailable'
169
236
) }
170
237
/>
171
238
Abrigo Indisponível
@@ -179,9 +246,11 @@ const Filter = (props: IFilterProps) => {
179
246
onChange = { ( ev ) =>
180
247
handleToggleShelterStatus ( ev . target . checked , 'waiting' )
181
248
}
182
- defaultChecked = { values . shelterStatus . includes ( 'waiting' ) }
249
+ defaultChecked = { values . shelterStatus . some (
250
+ ( s ) => s . value === 'waiting'
251
+ ) }
183
252
/>
184
- Aguardando disponibilidade
253
+ Sem informação de disponibilidade
185
254
</ label >
186
255
</ div >
187
256
</ div >
0 commit comments