From 72e3a2762b27bb8bab9f43c02ce05aaa5551f2d8 Mon Sep 17 00:00:00 2001 From: gikf <60067306+gikf@users.noreply.github.com> Date: Thu, 6 Jan 2022 11:12:41 +0100 Subject: [PATCH] fix: prevent duplicating sponsors in ui --- .../dashboard/Events/components/EventForm.tsx | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/client/src/modules/dashboard/Events/components/EventForm.tsx b/client/src/modules/dashboard/Events/components/EventForm.tsx index a4e25eedc0..10fce8f263 100644 --- a/client/src/modules/dashboard/Events/components/EventForm.tsx +++ b/client/src/modules/dashboard/Events/components/EventForm.tsx @@ -197,14 +197,24 @@ const EventForm: React.FC = (props) => { defaultValue={getValues(`sponsors.${index}.id`)} {...register(`sponsors.${index}.id` as const, { required: true, + valueAsNumber: true, })} > {sponsorData.sponsors - ?.filter( - //Filtering out the options of only selected type - (item) => - item.type === watchSponsorsArray[index]?.type, - ) + ?.filter((sponsor) => { + const isRightSponsorType = + sponsor.type === watchSponsorsArray[index]?.type; + if (!isRightSponsorType) { + return false; + } + const selectionIndex = watchSponsorsArray.findIndex( + (selectedSponsor) => + selectedSponsor.id === sponsor.id, + ); + const sponsorNotSelectedElsewhere = + selectionIndex === -1 || selectionIndex === index; + return sponsorNotSelectedElsewhere; + }) .map((s) => (