From 72df7d4675e698f3cd59ea0cfdab41f8d4d307fa Mon Sep 17 00:00:00 2001 From: shinework Date: Mon, 16 Sep 2024 17:17:22 +0200 Subject: [PATCH] fix : fix disponibilities solidarity --- back/api/espace/controllers/espace.js | 37 ++++++++---- web/components/Actuality/ActuCard.tsx | 13 ++-- web/components/Home/HomePlacesSolidarity.tsx | 15 +++-- .../BookingRecapInsert/AuthBookingInsert.tsx | 7 +-- .../BookingRecapInsert/BookingRecap.tsx | 9 ++- .../PlaceDetailPage/PlaceDetailCalendar.tsx | 5 +- web/components/Place/PlaceGridCard.tsx | 59 ++++++++++++------- web/components/Place/PlaceList.tsx | 6 +- web/components/Place/PlaceListCard.tsx | 17 +++--- web/components/Place/PlacesCampaignTabs.tsx | 1 - web/components/Place/PlacesPage.tsx | 13 ++-- web/hooks/useDispoInRange.ts | 6 +- web/next.config.js | 1 + web/pages/espaces/index.tsx | 3 +- 14 files changed, 108 insertions(+), 84 deletions(-) diff --git a/back/api/espace/controllers/espace.js b/back/api/espace/controllers/espace.js index 076b5623..782d7532 100644 --- a/back/api/espace/controllers/espace.js +++ b/back/api/espace/controllers/espace.js @@ -9,6 +9,12 @@ const isToday = require("date-fns/isToday"); * to customize this controller */ +const filterUserAttributes = (users_permissions_user) => { + const { id, structureName, email, phone, website, blocked } = users_permissions_user + + return { id, structureName, email, phone, website, blocked } +} + const populate = [ "disponibilities", "disponibilities.booking", @@ -23,7 +29,7 @@ const populate = [ module.exports = { async myPlaces(ctx) { - const {query}=ctx.request; + const { query } = ctx.request; const { id } = ctx.state.user; return strapi.query("espace").find( { @@ -38,6 +44,8 @@ module.exports = { async find(ctx) { const { _sort, perimeter, ...query } = ctx.query; const isSortOnDisponibility = ["dispoAsc", "nbDispoDesc"].includes(_sort); + const isCampaignMode = Boolean(query['disponibilities.campaign']) + if (perimeter && query["city.name_eq"]) { const placesInPerimeter = @@ -51,7 +59,6 @@ module.exports = { } } - let places = await strapi.services.espace .find( { @@ -81,8 +88,11 @@ module.exports = { ); return place; - }); + }).map(p => ({ ...p, users_permissions_user: filterUserAttributes(p.users_permissions_user) })) + const filterDispo = (disponibilities) => disponibilities?.filter(d => { + return isCampaignMode ? d.campaign !== null : d.campaign === null + }) if (isSortOnDisponibility) { if (_sort === "nbDispoDesc") { @@ -92,19 +102,22 @@ module.exports = { } else if (_sort === "dispoAsc") { return places.sort((a, b) => { const dateFirst = - a.disponibilities.length > 0 - ? min(a.disponibilities.map(({ start }) => new Date(start))) + filterDispo(a.disponibilities).length > 0 + ? min(filterDispo(a.disponibilities).map(({ start }) => new Date(start))) : new Date("3000-01-01"); + + const dateSecond = - b.disponibilities.length > 0 - ? min(b.disponibilities.map(({ start }) => new Date(start))) + filterDispo(b.disponibilities).length > 0 + ? min(filterDispo(b.disponibilities).map(({ start }) => new Date(start))) : new Date("3000-01-01"); return dateFirst - dateSecond; }); } } - return places; + + return places }, async update(ctx) { const { id } = ctx.params; @@ -112,13 +125,13 @@ module.exports = { let entity; if (ctx.is("multipart")) { const { data, files, campaign_files } = parseMultipartData(ctx); - entity = await strapi.services.espace.update({ id }, data, {files, campaign_files}); + entity = await strapi.services.espace.update({ id }, data, { files, campaign_files }); } else { - const { files,campaign_files, ...body } = ctx.request.body; - await Promise.all([files, campaign_files].map(async(fileList)=>{ + const { files, campaign_files, ...body } = ctx.request.body; + await Promise.all([files, campaign_files].map(async (fileList) => { if (fileList && fileList.length > 0) { await Promise.all( - fileList.map(async(file) => { + fileList.map(async (file) => { await strapi.plugins["upload"].services.upload.updateFileInfo(file.id, { caption: file.caption, }); diff --git a/web/components/Actuality/ActuCard.tsx b/web/components/Actuality/ActuCard.tsx index 1f7d8c54..6634a1c0 100644 --- a/web/components/Actuality/ActuCard.tsx +++ b/web/components/Actuality/ActuCard.tsx @@ -1,13 +1,11 @@ -import React from 'react' -import { Text, Box, Flex, Button, AspectRatio, LinkBox } from '@chakra-ui/react' -import LinkOverlay from '~components/LinkOverlay' +import { AspectRatio, Box, Button, Flex, Text } from '@chakra-ui/react' +import { useTranslation } from 'next-i18next' +import removeMd from 'remove-markdown' import Image from '~components/Image' import Link from '~components/Link' -import { format } from '~utils/date' import { ROUTE_ACTU_DETAIL } from '~constants' import { Actuality } from '~typings/api' -import { useTranslation } from 'next-i18next' -import removeMd from 'remove-markdown' +import { format } from '~utils/date' interface IActuCard { actu: Actuality @@ -15,6 +13,7 @@ interface IActuCard { const ActuCard = ({ actu }: IActuCard) => { const { t } = useTranslation('actuality') + console.log({ lol: actu?.image.formats }) return ( { { const isLgOrSm = useBreakpointValue({ diff --git a/web/components/Place/Booking/BookingRecapInsert/AuthBookingInsert.tsx b/web/components/Place/Booking/BookingRecapInsert/AuthBookingInsert.tsx index 7af12fd5..46f9fedb 100644 --- a/web/components/Place/Booking/BookingRecapInsert/AuthBookingInsert.tsx +++ b/web/components/Place/Booking/BookingRecapInsert/AuthBookingInsert.tsx @@ -1,9 +1,8 @@ -import React from 'react' -import { Button, Text, Box, ButtonGroup } from '@chakra-ui/react' +import { Box, Button, ButtonGroup, Text } from '@chakra-ui/react' +import { useTranslation } from 'next-i18next' import Link from '~components/Link' -import { ROUTE_SIGNUP } from '~constants' import SigninModal from '~components/Signin/SigninModal' -import { useTranslation } from 'next-i18next' +import { ROUTE_SIGNUP } from '~constants' const AuthBookingInsert = ({ nbSelected, diff --git a/web/components/Place/Booking/BookingRecapInsert/BookingRecap.tsx b/web/components/Place/Booking/BookingRecapInsert/BookingRecap.tsx index d22f5511..c3420c4d 100644 --- a/web/components/Place/Booking/BookingRecapInsert/BookingRecap.tsx +++ b/web/components/Place/Booking/BookingRecapInsert/BookingRecap.tsx @@ -1,12 +1,11 @@ -import React, { useMemo, useContext } from 'react' -import { Flex, Text, Box } from '@chakra-ui/react' -import BookingScheduleContext from '~components/Place/Booking/BookingScheduleContext' +import { Flex } from '@chakra-ui/react' import { useTranslation } from 'next-i18next' -import { useCurrentUser } from '~hooks/useCurrentUser' +import { useContext, useMemo } from 'react' import AuthBookingInsert from '~components/Place/Booking/BookingRecapInsert/AuthBookingInsert' import ConfirmBookingInsert from '~components/Place/Booking/BookingRecapInsert/ConfirmBookingInsert' -import { is } from 'date-fns/locale' import NotConfirmedBookingInsert from '~components/Place/Booking/BookingRecapInsert/NotConfirmedBookingInsert' +import BookingScheduleContext from '~components/Place/Booking/BookingScheduleContext' +import { useCurrentUser } from '~hooks/useCurrentUser' const BookingRecap = ({ isCampaignMode }: { isCampaignMode?: boolean }) => { const { t } = useTranslation('place') diff --git a/web/components/Place/PlaceDetailPage/PlaceDetailCalendar.tsx b/web/components/Place/PlaceDetailPage/PlaceDetailCalendar.tsx index 66568561..f29e22d2 100644 --- a/web/components/Place/PlaceDetailPage/PlaceDetailCalendar.tsx +++ b/web/components/Place/PlaceDetailPage/PlaceDetailCalendar.tsx @@ -1,8 +1,8 @@ import { Box, Flex, Text } from '@chakra-ui/react' -import BookingScheduleContainer from '~components/Place/Booking/BookingScheduleContainer' +import { useTranslation } from 'next-i18next' import Calendar from 'public/assets/img/calendar.svg' +import BookingScheduleContainer from '~components/Place/Booking/BookingScheduleContainer' import { Espace } from '~typings/api' -import { useTranslation } from 'next-i18next' const PlaceDetailCalendar = ({ place }: { place: Espace }) => { const { t } = useTranslation('place') @@ -17,7 +17,6 @@ const PlaceDetailCalendar = ({ place }: { place: Espace }) => { {t('detail.calendar')} - ) diff --git a/web/components/Place/PlaceGridCard.tsx b/web/components/Place/PlaceGridCard.tsx index 909a8816..190b7436 100644 --- a/web/components/Place/PlaceGridCard.tsx +++ b/web/components/Place/PlaceGridCard.tsx @@ -1,29 +1,28 @@ -import React from 'react' import { + AspectRatio, Box, - Flex, - Text, Divider, - AspectRatio, + Flex, LinkBox, + Text, } from '@chakra-ui/react' -import { Espace } from '~typings/api' -import PlaceCardCarousel from '~components/Place/PlaceCardCarousel' -import { ROUTE_PLACE_DETAIL } from '~constants' +import addWeeks from 'date-fns/addWeeks' import { useTranslation } from 'next-i18next' +import { DisponibilityStatus } from '~@types/disponibility.d' +import CampaignTag from '~components/Campaign/CampaignTag' +import useCampaignContext from '~components/Campaign/useCampaignContext' import FallbackImage from '~components/FallbackImage' import LinkOverlay from '~components/LinkOverlay' +import PlaceCardCarousel from '~components/Place/PlaceCardCarousel' import Tag from '~components/Tag' -import { DisponibilityStatus } from '~@types/disponibility.d' -import { SearchQuery } from '~utils/search' +import { ROUTE_PLACE_DETAIL } from '~constants' +import useCampaignDispo from '~hooks/useCampaignDispo' +import { useCurrentUser } from '~hooks/useCurrentUser' import useDispoInRange from '~hooks/useDispoInRange' import useNbDispoPerWeek from '~hooks/useNbDispoPerWeek' -import addWeeks from 'date-fns/addWeeks' -import useCampaignContext from '~components/Campaign/useCampaignContext' +import { Espace } from '~typings/api' import { format } from '~utils/date' -import CampaignTag from '~components/Campaign/CampaignTag' -import useCampaignDispo from '~hooks/useCampaignDispo' -import { useCurrentUser } from '~hooks/useCurrentUser' +import { SearchQuery } from '~utils/search' interface Props { place: Espace @@ -36,28 +35,44 @@ const PlaceGridCard = ({ place, searchParams, gridMode }: Props) => { const { applications } = useCurrentUser() const { t } = useTranslation('place') + const filteredDisponibilities = + place?.disponibilities?.filter((item) => { + if (gridMode === 'campaign') { + return Boolean(item.campaign) + } + + return item.campaign === null + }) ?? [] + const disposInRange = useDispoInRange( - place?.disponibilities, + filteredDisponibilities, searchParams?.['disponibilities.start_gte'], searchParams?.['disponibilities.end_lte'], ) const disposThisWeek = useNbDispoPerWeek( new Date(), - disposInRange || place?.disponibilities, + disposInRange || filteredDisponibilities, ) + const disposNextWeek = useNbDispoPerWeek( addWeeks(new Date(), 1), - disposInRange || place?.disponibilities, + disposInRange || filteredDisponibilities, ) - const { campaignDisposNum, campaignDispos } = useCampaignDispo( - place?.disponibilities, - ) + const { + campaignDisposNum, + campaignDispos, + solidarityDispos, + solidarityDisposNum, + } = useCampaignDispo(place?.disponibilities) + const hasCampaignDispo = currentCampaign?.mode === 'applications' && !!campaignDisposNum - if (!place) return null + if (!place) { + return null + } return ( @@ -120,7 +135,7 @@ const PlaceGridCard = ({ place, searchParams, gridMode }: Props) => { {place?.users_permissions_user?.structureName} - {place?.disponibilities?.length === 0 && gridMode !== 'campaign' && ( + {filteredDisponibilities?.length === 0 && gridMode !== 'campaign' && ( import('~components/Map'), { ssr: false }) diff --git a/web/components/Place/PlaceListCard.tsx b/web/components/Place/PlaceListCard.tsx index 01ea4959..23f18c56 100644 --- a/web/components/Place/PlaceListCard.tsx +++ b/web/components/Place/PlaceListCard.tsx @@ -1,22 +1,21 @@ -import React from 'react' import { + AspectRatio, Box, Flex, - Text, - AspectRatio, LinkBox, SimpleGrid, + Text, } from '@chakra-ui/react' -import { Espace } from '~typings/api' -import PlaceCardCarousel from '~components/Place/PlaceCardCarousel' -import { ROUTE_PLACE_DETAIL } from '~constants' import { useTranslation } from 'next-i18next' -import FallbackImage from '~components/FallbackImage' -import LinkOverlay from '~components/LinkOverlay' -import { format } from '~utils/date' import CampaignTag from '~components/Campaign/CampaignTag' import useCampaignContext from '~components/Campaign/useCampaignContext' +import FallbackImage from '~components/FallbackImage' +import LinkOverlay from '~components/LinkOverlay' +import PlaceCardCarousel from '~components/Place/PlaceCardCarousel' +import { ROUTE_PLACE_DETAIL } from '~constants' import useCampaignDispo from '~hooks/useCampaignDispo' +import { Espace } from '~typings/api' +import { format } from '~utils/date' interface Props { place: Espace diff --git a/web/components/Place/PlacesCampaignTabs.tsx b/web/components/Place/PlacesCampaignTabs.tsx index 2d7f2c4d..eee29c69 100644 --- a/web/components/Place/PlacesCampaignTabs.tsx +++ b/web/components/Place/PlacesCampaignTabs.tsx @@ -107,7 +107,6 @@ const PlacesCampaignTabs = () => { display={{ base: 'none', xl: 'inherit' }} /> - diff --git a/web/components/Place/PlacesPage.tsx b/web/components/Place/PlacesPage.tsx index 4a22578f..647e1abe 100644 --- a/web/components/Place/PlacesPage.tsx +++ b/web/components/Place/PlacesPage.tsx @@ -81,7 +81,10 @@ const PlacesPage = () => { isCampaignTab ? 'campaignPlaces' : 'solidarityPlaces', !campaignLoading, ) - const nbPlaces = useMemo(() => places?.pages?.flat().length, [ + + const filteredPlaces = places?.pages?.flat() ?? [] + + const nbPlaces = useMemo(() => filteredPlaces.length, [ places?.pages, searchParams, ]) @@ -142,11 +145,11 @@ const PlacesPage = () => { )} - {!isLoading && !isFetching && places?.pages?.flat().length === 0 ? ( + {!isLoading && !isFetching && filteredPlaces.length === 0 ? ( ) : ( <> - + {nbPlaces > 0 && ( @@ -249,7 +252,7 @@ const PlacesPage = () => { {isGridView ? ( { /> ) : (