Skip to content

Commit

Permalink
Dispo check
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucieo committed Feb 23, 2024
1 parent 885152d commit 0136112
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"name": "Apache 2.0",
"url": "https://www.apache.org/licenses/LICENSE-2.0.html"
},
"x-generation-date": "02/22/2024 8:24:57 PM"
"x-generation-date": "02/23/2024 1:16:02 PM"
},
"x-strapi-config": {
"path": "/documentation",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ import { Espace } from '~typings/api'
import { format } from '~utils/date'
import { ROUTE_ACCOUNT_PLACE_DETAIL } from '~constants'
import Link from '~components/Link'
import useCampaignDispo from '~hooks/useCampaignDispo'

const CampaignDisponibilitiesInfo = ({ place }: { place: Espace }) => {
const { t } = useTranslation('place')
const { currentCampaign } = useCampaignContext()
const campaignDispo = place?.disponibilities?.filter(
//@ts-expect-error
(d) => d.campaign === currentCampaign?.id,
)?.length
const { campaignDisposNum } = useCampaignDispo(place?.disponibilities)

return (
<VStack>
Expand Down Expand Up @@ -52,7 +50,7 @@ const CampaignDisponibilitiesInfo = ({ place }: { place: Espace }) => {
<Box flex={2}>
<Text>
{`${t(`list.slots_filled`, {
nb: campaignDispo,
nb: campaignDisposNum,
nbTotal: currentCampaign?.disponibilities_max,
})}`}
</Text>
Expand Down
8 changes: 3 additions & 5 deletions web/components/Account/Place/ListItem/PlaceListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import PlaceListItemInfo from '~components/Account/Place/ListItem/PlaceListItemI
import PlaceListItemImage from '~components/Account/Place/ListItem/PlaceListItemImage'
import CampaignDisponibilitiesInfo from '~components/Account/Place/ListItem/CampaignDisponibilitiesInfo'
import useCampaignContext from '~components/Campaign/useCampaignContext'
import useCampaignDispo from '~hooks/useCampaignDispo'

interface Props {
place: Espace
Expand All @@ -29,12 +30,9 @@ const PlaceListItem = ({ place, setVisible, isFirst }: Props) => {
}
}, [isComplete])

const { campaignDisposNum } = useCampaignDispo(place?.disponibilities)
const showCampaignDisponibilities =
currentCampaign?.mode === 'disponibilities' ||
(place?.disponibilities &&
place?.disponibilities?.some(
(d) => d?.campaign?.toString() === currentCampaign?.id.toString(),
))
currentCampaign?.mode === 'disponibilities' || campaignDisposNum

return (
<Flex
Expand Down
11 changes: 3 additions & 8 deletions web/components/Account/Place/PlaceTabList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useTranslation } from 'next-i18next'
import { Espace } from '~typings/api'
import { DisponibilityStatus } from '~@types/disponibility.d'
import useCampaignContext from '~components/Campaign/useCampaignContext'
import useCampaignDispo from '~hooks/useCampaignDispo'

const TabPlace = ({ isDisabled, onClick = null, children }) => {
return (
Expand Down Expand Up @@ -49,13 +50,7 @@ const PlaceTabList = ({
).length
}, [place?.disponibilities])

const campaignDispo = useMemo(() => {
if (!place || !place.disponibilities) return 0
return place.disponibilities.filter(
//@ts-expect-error
(dispo) => dispo.campaign === currentCampaign?.id,
).length
}, [place])
const { campaignDisposNum } = useCampaignDispo(place.disponibilities)

return (
<TabList
Expand Down Expand Up @@ -94,7 +89,7 @@ const PlaceTabList = ({
: 'tabs.slot_campaign_applications',
{
title: currentCampaign?.title,
nb: campaignDispo,
nb: campaignDisposNum,
nbTotal: currentCampaign?.disponibilities_max,
},
)}
Expand Down
2 changes: 1 addition & 1 deletion web/components/Campaign/CampaignTag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const CampaignTag = ({
</Box>
)
}

console.log(hasCampaignDispo && !isCampaignTab)
if (hasCampaignDispo && !isCampaignTab) {
return (
<Box position={isGrid ? 'relative' : undefined}>
Expand Down
20 changes: 11 additions & 9 deletions web/components/Campaign/Places/Admin/CampaignScheduleInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import ScheduleContext from '~components/Account/Place/ScheduleContext'
import ScheduleDelete from '~components/Account/Place/ScheduleDelete'
import Link from '~components/Link'
import { ROUTE_ACCOUNT_APPLICATIONS } from '~constants'
import useCampaignDispo from '~hooks/useCampaignDispo'

interface Props {
place: Espace
Expand All @@ -18,10 +19,10 @@ interface Props {
const CampaignScheduleInfo = ({ place, showForm }: Props) => {
const { t } = useTranslation('place')
const { currentCampaign } = useCampaignContext()
const campaignDispo = place?.disponibilities?.filter(
//@ts-expect-error
(d) => d.campaign === currentCampaign?.id,
const { campaignDisposNum, campaignDispos } = useCampaignDispo(
place?.disponibilities,
)

const isMobile = useBreakpointValue({ base: true, sm: false })
const { eventsIdToDelete } = useContext(ScheduleContext)

Expand Down Expand Up @@ -51,8 +52,8 @@ const CampaignScheduleInfo = ({ place, showForm }: Props) => {
<Box width="100%">
<Text fontFamily="mabry medium" fontSize="3xl" lineHeight="1">
{currentCampaign?.mode === 'disponibilities'
? `${campaignDispo?.length}/${currentCampaign?.disponibilities_max}`
: campaignDispo?.length}
? `${campaignDisposNum}/${currentCampaign?.disponibilities_max}`
: campaignDisposNum}
</Text>
<Text>
{currentCampaign?.mode === 'disponibilities'
Expand All @@ -62,7 +63,7 @@ const CampaignScheduleInfo = ({ place, showForm }: Props) => {
)
: t(
`campaign.helpers.applications.schedule.${
campaignDispo?.length <= 1
campaignDisposNum <= 1
? 'open_applications_one'
: 'open_applications_many'
}`,
Expand All @@ -76,20 +77,21 @@ const CampaignScheduleInfo = ({ place, showForm }: Props) => {
mt={6}
onClick={showForm}
disabled={
campaignDispo?.length === currentCampaign?.disponibilities_max
currentCampaign &&
campaignDisposNum === currentCampaign?.disponibilities_max
}
>
{isMobile ? t(`list.add`) : t(`schedule.add`)}
</Button>
) : campaignDispo?.length > 0 ? (
) : campaignDisposNum > 0 ? (
<Button
size="lg"
alignSelf="flex-start"
mt={6}
variant="outline"
colorScheme="blue"
as={Link}
href={`${ROUTE_ACCOUNT_APPLICATIONS}?espace=${place.id}&disponibility=${campaignDispo[0]?.id}`}
href={`${ROUTE_ACCOUNT_APPLICATIONS}?espace=${place.id}&disponibility=${campaignDispos[0]?.id}`}
>
{t('campaign.helpers.applications.schedule.see_applications')}
</Button>
Expand Down
11 changes: 4 additions & 7 deletions web/components/Campaign/Places/Detail/CampaignPlaceDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { useRouter } from 'next/router'
import CampaignApplicationSchedule from '~components/Campaign/Places/Application/CampaignApplicationSchedule'
import PlaceDetailCalendar from '~components/Place/PlaceDetailPage/PlaceDetailCalendar'
import CampaignDetailSwitcher from '~components/Campaign/Places/Detail/CampaignDetailSwitcher'
import useCampaignContext from '~components/Campaign/useCampaignContext'
import useCampaignDispo from '~hooks/useCampaignDispo'

interface Props {
place: Espace
Expand All @@ -29,7 +29,6 @@ const CampaignPlaceDetail = ({ place }: Props) => {
const isMobile = useBreakpointValue({ base: true, lg: false })
const router = useRouter()
const isCampaignTab = router.query.tab === '1'
const { currentCampaign } = useCampaignContext()

const displayPrecise = useMemo(() => {
if (!place) return false
Expand All @@ -40,6 +39,8 @@ const CampaignPlaceDetail = ({ place }: Props) => {
)
}, [place])

const { campaignDispos } = useCampaignDispo(place?.disponibilities)

return (
<Box>
<PlaceHeader place={place} />
Expand Down Expand Up @@ -69,11 +70,7 @@ const CampaignPlaceDetail = ({ place }: Props) => {
</Stack>

{isCampaignTab ? (
<CampaignApplicationSchedule
disponibilities={place?.disponibilities?.filter(
(d) => d?.campaign?.toString() === currentCampaign?.id.toString(),
)}
/>
<CampaignApplicationSchedule disponibilities={campaignDispos} />
) : (
<PlaceDetailCalendar place={place} />
)}
Expand Down
14 changes: 6 additions & 8 deletions web/components/Place/PlaceGridCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import addWeeks from 'date-fns/addWeeks'
import useCampaignContext from '~components/Campaign/useCampaignContext'
import { format } from '~utils/date'
import CampaignTag from '~components/Campaign/CampaignTag'
import useCampaignDispo from '~hooks/useCampaignDispo'

interface Props {
place: Espace
Expand All @@ -48,12 +49,11 @@ const PlaceGridCard = ({ place, searchParams, isCampaignTab }: Props) => {
disposInRange || place?.disponibilities,
)

const { campaignDisposNum, campaignDispos } = useCampaignDispo(
place?.disponibilities,
)
const hasCampaignDispo =
currentCampaign?.mode === 'applications' &&
place?.disponibilities?.some(
//@ts-expect-error
(d) => d?.campaign === currentCampaign?.id,
)
currentCampaign?.mode === 'applications' && !!campaignDisposNum

return (
<LinkBox>
Expand All @@ -75,9 +75,7 @@ const PlaceGridCard = ({ place, searchParams, isCampaignTab }: Props) => {
<CampaignTag
isGrid
isCampaignTab={isCampaignTab}
disponibilitiesIds={place?.disponibilities
?.filter((el) => el?.campaign)
.map((d) => d?.id)}
disponibilitiesIds={campaignDispos?.map((d) => d.id)}
hasCampaignDispo={hasCampaignDispo}
/>

Expand Down
9 changes: 7 additions & 2 deletions web/components/Place/PlaceListCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import LinkOverlay from '~components/LinkOverlay'
import { format } from '~utils/date'
import CampaignTag from '~components/Campaign/CampaignTag'
import useCampaignContext from '~components/Campaign/useCampaignContext'
import useCampaignDispo from '~hooks/useCampaignDispo'

interface Props {
place: Espace
Expand All @@ -26,7 +27,10 @@ interface Props {
const PlaceCard = ({ place, setFocus, isCampaignTab }: Props) => {
const { t } = useTranslation('place')
const { currentCampaign } = useCampaignContext()

const { campaignDispos } = useCampaignDispo(place?.disponibilities)
console.log(place?.disponibilities, currentCampaign?.mode)
const hasCampaignDispo =
currentCampaign?.mode === 'applications' && !!campaignDispos?.length
return (
<LinkBox
w="100%"
Expand Down Expand Up @@ -69,7 +73,8 @@ const PlaceCard = ({ place, setFocus, isCampaignTab }: Props) => {
<Box>
<CampaignTag
isCampaignTab={isCampaignTab}
disponibilitiesIds={place?.disponibilities?.map((d) => d?.id)}
disponibilitiesIds={campaignDispos?.map((d) => d.id)}
hasCampaignDispo={hasCampaignDispo}
/>
<Text fontFamily="mabry medium" isTruncated>
{place.name}
Expand Down
24 changes: 24 additions & 0 deletions web/hooks/useCampaignDispo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { useMemo } from 'react'
import useCampaignContext from '~components/Campaign/useCampaignContext'
import { Disponibility } from '~typings/api'

const useCampaignDispo = (disponibilities?: Disponibility[]) => {
const { currentCampaign } = useCampaignContext()

const campaignDispos = useMemo(
() =>
disponibilities?.filter(
(d) =>
currentCampaign &&
d?.campaign?.toString() === currentCampaign?.id?.toString(),
),
[disponibilities, currentCampaign],
)

return {
campaignDispos,
campaignDisposNum: campaignDispos?.length,
}
}

export default useCampaignDispo
3 changes: 2 additions & 1 deletion web/hooks/useCurrentUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export const useCurrentUser = () => {
if (userData && currentCampaign) {
//@ts-expect-error
return userData?.data?.companyApplications?.filter(
(application) => application?.campaign === currentCampaign?.id,
(application) =>
currentCampaign && application?.campaign === currentCampaign?.id,
)
}
}, [userData?.data, currentCampaign])
Expand Down
2 changes: 1 addition & 1 deletion web/hooks/useSelectedCampaign.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const useSelectedCampaign = () => {
const { allPlaceCampaigns } = useCampaignContext()
const { query } = useRouter()
const selectedCampaign = allPlaceCampaigns?.find(
(c) => c.id.toString() === query.campaign.toString(),
(c) => c?.id?.toString() === query?.campaign?.toString(),
)

return { selectedCampaign }
Expand Down
37 changes: 18 additions & 19 deletions web/pages/compte/candidatures/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,24 @@ const PlaceApplications = () => {
const { t: tAccount } = useTranslation('account')
const { selectedCampaign } = useSelectedCampaign()

if (selectedCampaign)
return (
<>
<NextSeo title={tAccount('title.requests')} />
{selectedCampaign?.mode === 'applications' && (
<Box paddingY={4}>
<PlacesAdminCampaignHelper
title={t(`place.helper.open_applications_start`, {
title: selectedCampaign?.title,
})}
description={t(`place.helper.open_applications_end`)}
/>
</Box>
)}
<CampaignSelector>
<DisponibilitiesSelector />
</CampaignSelector>
</>
)
return (
<>
<NextSeo title={tAccount('title.requests')} />
{selectedCampaign?.mode === 'applications' && (
<Box paddingY={4}>
<PlacesAdminCampaignHelper
title={t(`place.helper.open_applications_start`, {
title: selectedCampaign?.title,
})}
description={t(`place.helper.open_applications_end`)}
/>
</Box>
)}
<CampaignSelector>
<DisponibilitiesSelector />
</CampaignSelector>
</>
)

return null
}
Expand Down

0 comments on commit 0136112

Please sign in to comment.