Skip to content

Commit 79fac16

Browse files
authored
Merge pull request #1026 from cultuurnet/fix-price-information
Fix price information
2 parents 1a68ac1 + 7d3b0c1 commit 79fac16

File tree

1 file changed

+9
-23
lines changed

1 file changed

+9
-23
lines changed

src/pages/steps/AdditionalInformationStep/PriceInformation.tsx

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { yupResolver } from '@hookform/resolvers/yup';
2-
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
2+
import { useCallback, useEffect, useMemo, useRef } from 'react';
33
import { useFieldArray, useForm } from 'react-hook-form';
44
import { useTranslation } from 'react-i18next';
5+
import { useQueryClient } from 'react-query';
56
import * as yup from 'yup';
67
import { 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

Comments
 (0)