11import { yupResolver } from '@hookform/resolvers/yup' ;
2- import { useCallback , useEffect , useMemo , useRef , useState } from 'react' ;
2+ import { useCallback , useEffect , useMemo , useRef } from 'react' ;
33import { useFieldArray , useForm } from 'react-hook-form' ;
44import { useTranslation } from 'react-i18next' ;
5+ import { useQueryClient } from 'react-query' ;
56import * as yup from 'yup' ;
67import { ValidationError } from 'yup' ;
78
@@ -171,13 +172,15 @@ const PriceInformation = ({
171172 handleSubmit,
172173 watch,
173174 reset,
174- formState : { errors, dirtyFields } ,
175+ formState : { errors } ,
175176 } = useForm < FormData > ( {
176177 resolver : yupResolver ( schema ) ,
177178 defaultValues : defaultPriceInfoValues ,
178179 shouldFocusError : false ,
179180 } ) ;
180181
182+ const queryClient = useQueryClient ( ) ;
183+
181184 const { fields, replace, append, remove } = useFieldArray ( {
182185 name : 'rates' ,
183186 control,
@@ -206,11 +209,9 @@ const PriceInformation = ({
206209 } , [ fields , rates ] ) ;
207210
208211 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 } ] ) ;
214215 } ,
215216 useErrorBoundary : false ,
216217 } ) ;
@@ -236,11 +237,6 @@ const PriceInformation = ({
236237 [ rates ] ,
237238 ) ;
238239
239- const hasRates = useMemo (
240- ( ) => ! ! controlledRates . find ( ( rate ) => rate . price !== '' ) ,
241- [ controlledRates ] ,
242- ) ;
243-
244240 const hasBasePriceInfo = ! ! offer ?. priceInfo ?. find (
245241 ( price ) => price . category === PriceCategory . BASE ,
246242 ) ;
@@ -258,14 +254,7 @@ const PriceInformation = ({
258254 ? isUitpasOrganizer ( offer ?. organizer )
259255 : false ;
260256
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 ) {
269258 return onValidationChange (
270259 hasUitpasLabel || isCultuurkuurEvent
271260 ? ValidationStatus . WARNING
@@ -287,7 +276,6 @@ const PriceInformation = ({
287276 } , [
288277 offer ?. priceInfo ,
289278 field ,
290- hasRates ,
291279 hasMultiplePrices ,
292280 isCultuurkuurEvent ,
293281 offer ,
@@ -410,7 +398,6 @@ const PriceInformation = ({
410398 shouldValidate : false ,
411399 } ) ;
412400 onSubmit ( ) ;
413- getOfferByIdQuery . remove ( ) ;
414401 } }
415402 width = "8rem"
416403 >
@@ -435,7 +422,6 @@ const PriceInformation = ({
435422 shouldValidate : false ,
436423 } ) ;
437424 onSubmit ( ) ;
438- getOfferByIdQuery . remove ( ) ;
439425 } }
440426 >
441427 { t ( 'create.additionalInformation.price_info.free' ) }
0 commit comments