From 454cf4cd9cf43f6fab9f839b57f0d4952f116f27 Mon Sep 17 00:00:00 2001 From: lucieo Date: Fri, 23 Feb 2024 09:49:51 +0100 Subject: [PATCH] Fix selected campaign picking with hook --- .../Place/ApplicationPlaceList.tsx | 10 ++++++---- .../Place/ApplicationPlaceListItem.tsx | 20 ++++++------------- .../ApplicationPlaceHelper.tsx | 9 ++------- .../ApplicationsHelpers/MissingSelections.tsx | 6 +++--- .../CampaignSelector/CampaignSelector.tsx | 5 ++--- web/hooks/useSelectedCampaign.tsx | 14 +++++++++++++ web/pages/compte/candidatures/index.tsx | 16 ++++----------- 7 files changed, 37 insertions(+), 43 deletions(-) create mode 100644 web/hooks/useSelectedCampaign.tsx diff --git a/web/components/Account/Application/Place/ApplicationPlaceList.tsx b/web/components/Account/Application/Place/ApplicationPlaceList.tsx index 9a2c6cda..f3461520 100644 --- a/web/components/Account/Application/Place/ApplicationPlaceList.tsx +++ b/web/components/Account/Application/Place/ApplicationPlaceList.tsx @@ -14,6 +14,7 @@ import Cell from '~components/Account/Booking/Cell' import useCampaignContext from '~components/Campaign/useCampaignContext' import ApplicationPlaceHelper from '~components/Account/Application/Place/ApplicationsHelpers/ApplicationPlaceHelper' import ApplicationPlaceListItem from '~components/Account/Application/Place/ApplicationPlaceListItem' +import useSelectedCampaign from '~hooks/useSelectedCampaign' interface Props { applications: Application[] @@ -24,11 +25,10 @@ const Divider = (props: DividerProps) => ( ) const ApplicationPlaceList = ({ applications = [] }: Props) => { - const { currentCampaign } = useCampaignContext() const { t } = useTranslation('application') const [list, setList] = useState([]) const [isDesc, setDesc] = useState(true) - + const { selectedCampaign } = useSelectedCampaign() useEffect(() => { setList(applications) setDesc(true) @@ -44,7 +44,9 @@ const ApplicationPlaceList = ({ applications = [] }: Props) => { @@ -73,7 +75,7 @@ const ApplicationPlaceList = ({ applications = [] }: Props) => { {t('place.table.head.creation')} - {currentCampaign?.mode === 'preselections' && ( + {['preselections', 'closed']?.includes(selectedCampaign?.mode) && ( diff --git a/web/components/Account/Application/Place/ApplicationPlaceListItem.tsx b/web/components/Account/Application/Place/ApplicationPlaceListItem.tsx index 7f2fa069..4a085f95 100644 --- a/web/components/Account/Application/Place/ApplicationPlaceListItem.tsx +++ b/web/components/Account/Application/Place/ApplicationPlaceListItem.tsx @@ -4,20 +4,15 @@ import { Text, Button, IconButton, HStack } from '@chakra-ui/react' import { useTranslation } from 'next-i18next' import Cell from '~components/Account/Booking/Cell' import Link from '~components/Link' -import useCampaignContext from '~components/Campaign/useCampaignContext' import DownloadApplication from 'public/assets/img/downloadApplication.svg' -import { useRouter } from 'next/router' +import useSelectedCampaign from '~hooks/useSelectedCampaign' interface Props { application: Application } const ApplicationPlaceListItem = ({ application }: Props) => { - const { allPlaceCampaigns } = useCampaignContext() - const { query } = useRouter() - const selectedCampaign = allPlaceCampaigns?.find( - (c) => c.id.toString() === query.campaign.toString(), - ) + const { selectedCampaign } = useSelectedCampaign() const { t } = useTranslation('application') return ( @@ -45,9 +40,8 @@ const ApplicationPlaceListItem = ({ application }: Props) => { {application?.creation_title} - - - {['preselections', 'closed']?.includes(selectedCampaign?.mode) ? ( + {['preselections', 'closed']?.includes(selectedCampaign?.mode) && ( + { {t('place.table.buttons.details')} - ) : ( - '' - )} - + + )} ) } diff --git a/web/components/Account/Application/Place/ApplicationsHelpers/ApplicationPlaceHelper.tsx b/web/components/Account/Application/Place/ApplicationsHelpers/ApplicationPlaceHelper.tsx index 8fc26a86..2fe05cf0 100644 --- a/web/components/Account/Application/Place/ApplicationsHelpers/ApplicationPlaceHelper.tsx +++ b/web/components/Account/Application/Place/ApplicationsHelpers/ApplicationPlaceHelper.tsx @@ -1,25 +1,20 @@ -import useCampaignContext from '~components/Campaign/useCampaignContext' import MissingSelections from '~components/Account/Application/Place/ApplicationsHelpers/MissingSelections' import ConfirmSelections from '~components/Account/Application/Place/ApplicationsHelpers/ConfirmSelections' import ValidatedSelections from '~components/Account/Application/Place/ApplicationsHelpers/ValidatedSelections' import { Application } from '~typings/api' -import { useRouter } from 'next/router' import ClosedCampaign from '~components/Account/Application/Place/ApplicationsHelpers/ClosedCampaign' +import useSelectedCampaign from '~hooks/useSelectedCampaign' const ApplicationPlaceHelper = ({ applications, }: { applications: Application[] }) => { - const { allPlaceCampaigns } = useCampaignContext() const preselections = applications?.filter( (application) => application?.status === 'preselected', ).length - const { query } = useRouter() - const selectedCampaign = allPlaceCampaigns?.find( - (c) => c.id.toString() === query.campaign.toString(), - ) + const { selectedCampaign } = useSelectedCampaign() const missingPreselections = selectedCampaign?.preselections_max - preselections diff --git a/web/components/Account/Application/Place/ApplicationsHelpers/MissingSelections.tsx b/web/components/Account/Application/Place/ApplicationsHelpers/MissingSelections.tsx index 9ed4ac63..af5a42be 100644 --- a/web/components/Account/Application/Place/ApplicationsHelpers/MissingSelections.tsx +++ b/web/components/Account/Application/Place/ApplicationsHelpers/MissingSelections.tsx @@ -1,8 +1,8 @@ import { Box, HStack, Text } from '@chakra-ui/react' import { useTranslation } from 'next-i18next' -import useCampaignContext from '~components/Campaign/useCampaignContext' import { format } from '~utils/date' import PreselectionsWarning from 'public/assets/img/preselectionsWarning.svg' +import useSelectedCampaign from '~hooks/useSelectedCampaign' const MissingSelections = ({ missingPreselections, @@ -10,7 +10,7 @@ const MissingSelections = ({ missingPreselections: number }) => { const { t } = useTranslation('application') - const { currentCampaign } = useCampaignContext() + const { selectedCampaign } = useSelectedCampaign() return ( @@ -20,7 +20,7 @@ const MissingSelections = ({ {t('place.helper.preselection_start', { - date: format(currentCampaign?.preselection_end), + date: format(selectedCampaign?.preselection_end), })} diff --git a/web/components/Account/Application/Place/CampaignSelector/CampaignSelector.tsx b/web/components/Account/Application/Place/CampaignSelector/CampaignSelector.tsx index 6ac25309..883bd28c 100644 --- a/web/components/Account/Application/Place/CampaignSelector/CampaignSelector.tsx +++ b/web/components/Account/Application/Place/CampaignSelector/CampaignSelector.tsx @@ -5,6 +5,7 @@ import { useEffect } from 'react' import CampaignSelectorField from '~components/Account/Application/Place/CampaignSelector/CampaignSelectorField' import useCampaignContext from '~components/Campaign/useCampaignContext' import { ROUTE_ACCOUNT_APPLICATIONS } from '~constants' +import useSelectedCampaign from '~hooks/useSelectedCampaign' const CampaignSelector = ({ children }) => { const router = useRouter() @@ -21,9 +22,7 @@ const CampaignSelector = ({ children }) => { } }, [allPlaceCampaigns]) - const selectedCampaign = allPlaceCampaigns?.find( - (c) => c.id?.toString() === campaign?.toString(), - ) + const { selectedCampaign } = useSelectedCampaign() if (!selectedCampaign) return null diff --git a/web/hooks/useSelectedCampaign.tsx b/web/hooks/useSelectedCampaign.tsx new file mode 100644 index 00000000..dcc44d62 --- /dev/null +++ b/web/hooks/useSelectedCampaign.tsx @@ -0,0 +1,14 @@ +import { useRouter } from 'next/router' +import useCampaignContext from '~components/Campaign/useCampaignContext' + +const useSelectedCampaign = () => { + const { allPlaceCampaigns } = useCampaignContext() + const { query } = useRouter() + const selectedCampaign = allPlaceCampaigns?.find( + (c) => c.id.toString() === query.campaign.toString(), + ) + + return { selectedCampaign } +} + +export default useSelectedCampaign diff --git a/web/pages/compte/candidatures/index.tsx b/web/pages/compte/candidatures/index.tsx index f4265ec5..d8e90c33 100644 --- a/web/pages/compte/candidatures/index.tsx +++ b/web/pages/compte/candidatures/index.tsx @@ -5,24 +5,18 @@ import { serverSideTranslations } from 'next-i18next/serverSideTranslations' import { requireAuth } from '~utils/auth' import { NextSeo } from 'next-seo' import { useTranslation } from 'next-i18next' -import useCampaignContext from '~components/Campaign/useCampaignContext' import { Box } from '@chakra-ui/react' -import { useRouter } from 'next/router' import PlacesAdminCampaignHelper from '~components/Campaign/Places/Admin/PlacesAdminCampaignHelper' import CampaignSelector from '~components/Account/Application/Place/CampaignSelector/CampaignSelector' import DisponibilitiesSelector from '~components/Account/Application/Place/DisponibilitiesSelector/DisponibilitiesSelector' +import useSelectedCampaign from '~hooks/useSelectedCampaign' const PlaceApplications = () => { const { t } = useTranslation('application') const { t: tAccount } = useTranslation('account') - const { allPlaceCampaigns } = useCampaignContext() - const router = useRouter() - const { campaign } = router.query - const selectedCampaign = allPlaceCampaigns?.find( - (c) => c?.id?.toString() === campaign?.toString(), - ) + const { selectedCampaign } = useSelectedCampaign() - if (allPlaceCampaigns) + if (selectedCampaign) return ( <> @@ -30,9 +24,7 @@ const PlaceApplications = () => { c?.id.toString() === campaign?.toString(), - )?.title, + title: selectedCampaign?.title, })} description={t(`place.helper.open_applications_end`)} />