1
1
import { yupResolver } from '@hookform/resolvers/yup' ;
2
- import { useCallback , useEffect , useMemo , useRef , useState } from 'react' ;
2
+ import { useCallback , useEffect , useMemo , useRef } from 'react' ;
3
3
import { useFieldArray , useForm } from 'react-hook-form' ;
4
4
import { useTranslation } from 'react-i18next' ;
5
+ import { useQueryClient } from 'react-query' ;
5
6
import * as yup from 'yup' ;
6
7
import { ValidationError } from 'yup' ;
7
8
@@ -171,13 +172,15 @@ const PriceInformation = ({
171
172
handleSubmit,
172
173
watch,
173
174
reset,
174
- formState : { errors, dirtyFields } ,
175
+ formState : { errors } ,
175
176
} = useForm < FormData > ( {
176
177
resolver : yupResolver ( schema ) ,
177
178
defaultValues : defaultPriceInfoValues ,
178
179
shouldFocusError : false ,
179
180
} ) ;
180
181
182
+ const queryClient = useQueryClient ( ) ;
183
+
181
184
const { fields, replace, append, remove } = useFieldArray ( {
182
185
name : 'rates' ,
183
186
control,
@@ -206,11 +209,9 @@ const PriceInformation = ({
206
209
} , [ fields , rates ] ) ;
207
210
208
211
const addPriceInfoMutation = useAddOfferPriceInfoMutation ( {
209
- onSuccess : ( data ) => {
210
- const isFormDirty = Object . keys ( dirtyFields ) . length > 0 ;
211
- if ( typeof data === 'undefined' || ! isFormDirty ) return ;
212
-
213
- return setTimeout ( ( ) => onSuccessfulChange ( ) , 1000 ) ;
212
+ onSuccess : async ( ) => {
213
+ setTimeout ( ( ) => onSuccessfulChange ( ) , 1000 ) ;
214
+ await queryClient . invalidateQueries ( [ scope , { id : offerId } ] ) ;
214
215
} ,
215
216
useErrorBoundary : false ,
216
217
} ) ;
@@ -236,11 +237,6 @@ const PriceInformation = ({
236
237
[ rates ] ,
237
238
) ;
238
239
239
- const hasRates = useMemo (
240
- ( ) => ! ! controlledRates . find ( ( rate ) => rate . price !== '' ) ,
241
- [ controlledRates ] ,
242
- ) ;
243
-
244
240
const hasBasePriceInfo = ! ! offer ?. priceInfo ?. find (
245
241
( price ) => price . category === PriceCategory . BASE ,
246
242
) ;
@@ -258,14 +254,7 @@ const PriceInformation = ({
258
254
? isUitpasOrganizer ( offer ?. organizer )
259
255
: false ;
260
256
261
- if ( ! hasRates ) {
262
- replace (
263
- priceInfo . length
264
- ? ( priceInfo as FormData [ 'rates' ] )
265
- : defaultPriceInfoValues . rates ,
266
- ) ;
267
- reset ( { } , { keepValues : true } ) ;
268
-
257
+ if ( priceInfo . length === 0 ) {
269
258
return onValidationChange (
270
259
hasUitpasLabel || isCultuurkuurEvent
271
260
? ValidationStatus . WARNING
@@ -287,7 +276,6 @@ const PriceInformation = ({
287
276
} , [
288
277
offer ?. priceInfo ,
289
278
field ,
290
- hasRates ,
291
279
hasMultiplePrices ,
292
280
isCultuurkuurEvent ,
293
281
offer ,
@@ -410,7 +398,6 @@ const PriceInformation = ({
410
398
shouldValidate : false ,
411
399
} ) ;
412
400
onSubmit ( ) ;
413
- getOfferByIdQuery . remove ( ) ;
414
401
} }
415
402
width = "8rem"
416
403
>
@@ -435,7 +422,6 @@ const PriceInformation = ({
435
422
shouldValidate : false ,
436
423
} ) ;
437
424
onSubmit ( ) ;
438
- getOfferByIdQuery . remove ( ) ;
439
425
} }
440
426
>
441
427
{ t ( 'create.additionalInformation.price_info.free' ) }
0 commit comments