From 8832e452dd0b6ccbfa2072c3355f4d4bd6cfc13d Mon Sep 17 00:00:00 2001 From: gikf <60067306+gikf@users.noreply.github.com> Date: Sun, 9 Jan 2022 22:22:44 +0100 Subject: [PATCH 1/7] fix: disable button when all sponsors are added --- .../src/modules/dashboard/Events/components/EventForm.tsx | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/client/src/modules/dashboard/Events/components/EventForm.tsx b/client/src/modules/dashboard/Events/components/EventForm.tsx index 3c83e427ef..6ac78185b7 100644 --- a/client/src/modules/dashboard/Events/components/EventForm.tsx +++ b/client/src/modules/dashboard/Events/components/EventForm.tsx @@ -148,6 +148,14 @@ const EventForm: React.FC = (props) => { id: firstSponsorId, }); }} + isDisabled={ + loadingSponsors || + errorSponsor || + !sponsorData || + sponsorFields.length < sponsorData.sponsors.length + ? false + : true + } > Add From b41afd1c8fd6472db051da1700a5d1206aacc81e Mon Sep 17 00:00:00 2001 From: gikf <60067306+gikf@users.noreply.github.com> Date: Sun, 9 Jan 2022 22:27:51 +0100 Subject: [PATCH 2/7] refactor: move sponsorType to Utils --- .../modules/dashboard/Events/components/EventFormUtils.ts | 8 ++++++++ .../dashboard/Events/components/EventSponsorCard.tsx | 8 +------- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/client/src/modules/dashboard/Events/components/EventFormUtils.ts b/client/src/modules/dashboard/Events/components/EventFormUtils.ts index 61953ab349..cade1812f5 100644 --- a/client/src/modules/dashboard/Events/components/EventFormUtils.ts +++ b/client/src/modules/dashboard/Events/components/EventFormUtils.ts @@ -9,6 +9,14 @@ export interface Field { isRequired: boolean; } +export type sponsorType = { + name: string; + logo_path: string; + website: string; + type: string; + id: number; +}; + export interface EventSponsorInput { id: number; type: string; diff --git a/client/src/modules/dashboard/Events/components/EventSponsorCard.tsx b/client/src/modules/dashboard/Events/components/EventSponsorCard.tsx index fdcf23fd83..a08aeeb64b 100644 --- a/client/src/modules/dashboard/Events/components/EventSponsorCard.tsx +++ b/client/src/modules/dashboard/Events/components/EventSponsorCard.tsx @@ -1,12 +1,6 @@ import { Box, Heading, Flex, Spacer, Link, Badge } from '@chakra-ui/react'; import React from 'react'; -type sponsorType = { - name: string; - logo_path: string; - website: string; - type: string; - id: number; -}; +import { sponsorType } from './EventFormUtils'; interface SponsorProps { sponsor: sponsorType; From ee1b89047162f4f86003ee585e8e2dcf8f81809b Mon Sep 17 00:00:00 2001 From: gikf <60067306+gikf@users.noreply.github.com> Date: Sun, 9 Jan 2022 22:30:48 +0100 Subject: [PATCH 3/7] feat: add sponsor types --- .../Events/components/EventFormUtils.ts | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/client/src/modules/dashboard/Events/components/EventFormUtils.ts b/client/src/modules/dashboard/Events/components/EventFormUtils.ts index cade1812f5..fafad62832 100644 --- a/client/src/modules/dashboard/Events/components/EventFormUtils.ts +++ b/client/src/modules/dashboard/Events/components/EventFormUtils.ts @@ -17,6 +17,26 @@ export type sponsorType = { id: number; }; +export interface EventSponsorTypeInput { + name: string; + type: string; +} + +export const sponsorTypes: EventSponsorTypeInput[] = [ + { + name: 'Food', + type: 'FOOD', + }, + { + name: 'Venue', + type: 'VENUE', + }, + { + name: 'Other', + type: 'OTHER', + }, +]; + export interface EventSponsorInput { id: number; type: string; From 44d31b6a79fcb6c430524fba8cc82b3d013db30f Mon Sep 17 00:00:00 2001 From: gikf <60067306+gikf@users.noreply.github.com> Date: Sun, 9 Jan 2022 22:32:08 +0100 Subject: [PATCH 4/7] fix: sponsor field should not be empty --- .../dashboard/Events/components/EventForm.tsx | 104 +++++++++++++----- 1 file changed, 77 insertions(+), 27 deletions(-) diff --git a/client/src/modules/dashboard/Events/components/EventForm.tsx b/client/src/modules/dashboard/Events/components/EventForm.tsx index 6ac78185b7..d32426ae39 100644 --- a/client/src/modules/dashboard/Events/components/EventForm.tsx +++ b/client/src/modules/dashboard/Events/components/EventForm.tsx @@ -23,6 +23,9 @@ import { fields, formatValue, EventFormData, + EventSponsorInput, + EventSponsorTypeInput, + sponsorTypes, } from './EventFormUtils'; const EventForm: React.FC = (props) => { @@ -77,10 +80,61 @@ const EventForm: React.FC = (props) => { control, }); + const filterAllowedSponsorTypes = ( + sponsorTypes: EventSponsorTypeInput[], + sponsorFieldId: number, + ) => { + const allowedSponsorTypes = + sponsorTypes.filter(({ type }) => { + const allSponsorsForType = sponsorData?.sponsors.filter( + (sponsor) => sponsor.type === type, + ); + const allowedSponsorsForType = + allSponsorsForType?.filter((sponsor) => { + const selectedSponsorId = watchSponsorsArray.findIndex( + (selectedSponsor) => selectedSponsor.id === sponsor.id, + ); + return ( + selectedSponsorId === -1 || selectedSponsorId === sponsorFieldId + ); + }) ?? []; + return allowedSponsorsForType.length > 0; + }) ?? []; + return allowedSponsorTypes; + }; + + const filterAllowedSponsorsForType = (sponsorType: string) => { + return ( + sponsorData?.sponsors + .filter((sponsor) => sponsor.type === sponsorType) + .filter( + (sponsor) => + watchSponsorsArray.findIndex( + (selectedSponsor) => selectedSponsor.id === sponsor.id, + ) === -1, + ) ?? [] + ); + }; + + const filterSponsors = ( + sponsor: EventSponsorInput, + sponsorFieldId: number, + ) => { + const isRightSponsorType = + sponsor.type === watchSponsorsArray[sponsorFieldId]?.type; + if (!isRightSponsorType) { + return false; + } + const selectedSponsorId = watchSponsorsArray.findIndex( + (selectedSponsor) => selectedSponsor.id === sponsor.id, + ); + const sponsorNotSelectedElsewhere = + selectedSponsorId === -1 || selectedSponsorId === sponsorFieldId; + return sponsorNotSelectedElsewhere; + }; + const watchSponsorsArray = watch('sponsors'); const inviteOnly = watch('invite_only'); - const firstSponsorType = sponsorData?.sponsors[0]?.type as string | undefined; - const firstSponsorId = sponsorData?.sponsors[0]?.id as number | undefined; return (
= (props) => {