From e5adbe69b6c1afaf731375951be9a022df418b96 Mon Sep 17 00:00:00 2001 From: Loup Theron Date: Fri, 30 Aug 2024 11:04:13 +0200 Subject: [PATCH] Improve cache of species --- .../FormikFishingCatchesMultiSelect/index.tsx | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/frontend/src/features/PriorNotification/components/ManualPriorNotificationForm/fields/FormikFishingCatchesMultiSelect/index.tsx b/frontend/src/features/PriorNotification/components/ManualPriorNotificationForm/fields/FormikFishingCatchesMultiSelect/index.tsx index 0ef9ccdb35..1ec6eb0d4d 100644 --- a/frontend/src/features/PriorNotification/components/ManualPriorNotificationForm/fields/FormikFishingCatchesMultiSelect/index.tsx +++ b/frontend/src/features/PriorNotification/components/ManualPriorNotificationForm/fields/FormikFishingCatchesMultiSelect/index.tsx @@ -30,19 +30,11 @@ export function FormikFishingCatchesMultiSelect({ isReadOnly }: FormikFishingCat const validationError = getFishingsCatchesValidationError(errors) - const filteredSpeciesAsOptions = useMemo( - () => - speciesAsOptions?.filter(specyOption => - values.fishingCatches.every(fishingCatch => fishingCatch.specyCode !== specyOption.value.code) - ) ?? [], - [speciesAsOptions, values.fishingCatches] - ) - const customSearch = useMemo( () => - filteredSpeciesAsOptions.length + speciesAsOptions?.length ? new CustomSearch( - filteredSpeciesAsOptions, + speciesAsOptions, [ { name: 'value.code', @@ -56,10 +48,17 @@ export function FormikFishingCatchesMultiSelect({ isReadOnly }: FormikFishingCat { cacheKey: 'PNO_SPECIES_AS_OPTIONS', isStrict: true } ) : undefined, - [filteredSpeciesAsOptions] + [speciesAsOptions] ) const add = (nextSpecy: Specy | undefined) => { + const specyIsAlreadyInCatches = values.fishingCatches?.find( + fishingCatch => fishingCatch.specyCode === nextSpecy?.code + ) + if (specyIsAlreadyInCatches) { + return + } + const specyOption = speciesAsOptions?.find(({ value }) => value.code === nextSpecy?.code) if (!specyOption) { return @@ -89,22 +88,24 @@ export function FormikFishingCatchesMultiSelect({ isReadOnly }: FormikFishingCat setFieldValue('fishingCatches', nextFishingCatches) } - if (!filteredSpeciesAsOptions.length || !customSearch) { + if (!speciesAsOptions?.length || !customSearch) { return } return ( <>