From 4c9d343bd7131463b09f1ae3b958f83192f18f4d Mon Sep 17 00:00:00 2001 From: Martin Gerritsen Date: Wed, 6 Nov 2024 23:52:45 +0100 Subject: [PATCH 1/3] feat(CookieConsent) Added Cookie consent banner --- src/app/casos-activos/solicitudes/page.js | 19 ++--- src/app/voluntometro/page.js | 17 ++--- src/components/CookieBanner/CookieBanner.tsx | 77 ++++++++++++++++++++ src/components/{Modal.js => Modal.tsx} | 38 ++++++++-- src/components/auth/PhoneNumberDialog.js | 8 +- src/components/layout/EmergencyLayout.tsx | 2 + src/components/map/map.tsx | 11 +-- src/context/EmergencyProvider.tsx | 30 +++++--- src/helpers/constants.ts | 2 + 9 files changed, 161 insertions(+), 43 deletions(-) create mode 100644 src/components/CookieBanner/CookieBanner.tsx rename src/components/{Modal.js => Modal.tsx} (53%) diff --git a/src/app/casos-activos/solicitudes/page.js b/src/app/casos-activos/solicitudes/page.js index cc41c002..0120dd32 100644 --- a/src/app/casos-activos/solicitudes/page.js +++ b/src/app/casos-activos/solicitudes/page.js @@ -10,7 +10,9 @@ import { useRouter, useSearchParams } from 'next/navigation'; import { tiposAyudaOptions } from '@/helpers/constants'; import Modal from '@/components/Modal'; import { useModal } from '@/context/EmergencyProvider'; -import { useTowns } from '../../../context/TownProvider'; +import { useTowns } from '@/context/TownProvider'; + +const MODAL_NAME = 'solicitudes'; export default function Solicitudes() { const towns = useTowns(); @@ -23,10 +25,10 @@ export default function Solicitudes() { const [data, setData] = useState([]); const [currentPage, setCurrentPage] = useState(Number(searchParams.get('page')) || 1); const [currentCount, setCurrentCount] = useState(0); - const { showModal, toggleModal } = useModal(); + const { toggleModal } = useModal(); const closeModal = () => { - toggleModal(false); + toggleModal(MODAL_NAME, false); }; const itemsPerPage = 10; const numPages = (count) => { @@ -172,7 +174,7 @@ export default function Solicitudes() { + + + + + ); +}; + +export default CookieBanner; diff --git a/src/components/Modal.js b/src/components/Modal.tsx similarity index 53% rename from src/components/Modal.js rename to src/components/Modal.tsx index 10b6ae89..e7b8950e 100644 --- a/src/components/Modal.js +++ b/src/components/Modal.tsx @@ -1,13 +1,39 @@ import { useModal } from '@/context/EmergencyProvider'; +import { MouseEvent, FC, ReactNode } from 'react'; -export const Modal = ({ children, maxWidth = 'max-w-2xl', allowClose = true }) => { - const { showModal, toggleModal } = useModal(); +type TailwindMaxWidth = + | 'max-w-xs' + | 'max-w-sm' + | 'max-w-md' + | 'max-w-lg' + | 'max-w-xl' + | 'max-w-2xl' + | 'max-w-3xl' + | 'max-w-4xl' + | 'max-w-5xl' + | 'max-w-6xl' + | 'max-w-7xl' + | 'max-w-full' + | 'max-w-screen-sm' + | 'max-w-screen-md' + | 'max-w-screen-lg' + | 'max-w-screen-xl' + | 'max-w-screen-2xl'; - if (!showModal) return null; +type ModalProps = { + id: string; + children: ReactNode; + maxWidth?: TailwindMaxWidth; + allowClose?: boolean; +}; + +const Modal: FC = ({ id, children, maxWidth = 'max-w-2xl', allowClose = true }) => { + const { isModalOpen, toggleModal } = useModal(); + if (!isModalOpen[id]) return null; - const handleBackdropClick = (e) => { + const handleBackdropClick = (e: MouseEvent) => { if (allowClose && e.target === e.currentTarget) { - toggleModal(); + toggleModal(id); } }; @@ -19,7 +45,7 @@ export const Modal = ({ children, maxWidth = 'max-w-2xl', allowClose = true }) =
{allowClose && (
); diff --git a/src/components/map/map.tsx b/src/components/map/map.tsx index 2f123efb..9b28e071 100644 --- a/src/components/map/map.tsx +++ b/src/components/map/map.tsx @@ -6,9 +6,10 @@ import 'maplibre-gl/dist/maplibre-gl.css'; import { Marker } from 'react-map-gl/maplibre'; import { MapPin } from 'lucide-react'; import { useModal } from '@/context/EmergencyProvider'; -// @ts-ignore import Modal from '@/components/Modal'; +const MODAL_NAME = `map-marker`; + const urgencyToColor = { alta: 'text-red-500', media: 'text-amber-500', @@ -34,7 +35,7 @@ const DEFAULT_ZOOM = 12; const Map: FC = ({ markers = [] }) => { const [selectedMarker, setSelectedMarker] = useState(null); - const { showModal, toggleModal } = useModal(); + const { toggleModal } = useModal(); console.log(selectedMarker); return ( @@ -50,11 +51,11 @@ const Map: FC = ({ markers = [] }) => { {markers.map((m) => { return ( { - toggleModal(true); + toggleModal(MODAL_NAME, true); setSelectedMarker(m); }} anchor="bottom" @@ -63,7 +64,7 @@ const Map: FC = ({ markers = [] }) => { ); })} - {selectedMarker && showModal && {selectedMarker.popup}} + {selectedMarker && {selectedMarker.popup}} ); }; diff --git a/src/context/EmergencyProvider.tsx b/src/context/EmergencyProvider.tsx index fcd02baa..76a109fe 100644 --- a/src/context/EmergencyProvider.tsx +++ b/src/context/EmergencyProvider.tsx @@ -1,24 +1,32 @@ 'use client'; -import React, { createContext, ReactNode, useContext, useState } from 'react'; +import React, { createContext, FC, ReactNode, useContext, useState } from 'react'; -const EmergencyContext = createContext({ showModal: false, toggleModal: () => {} }); - -type EmergencyCtx = { - showModal: boolean; - toggleModal: (force: boolean) => void; +type EmergencyContextType = { + isModalOpen: { [key: string]: boolean }; + toggleModal: (id: string, force?: boolean) => void; }; -type SessionProviderProps = { +const EmergencyContext = createContext({ + isModalOpen: {}, + toggleModal: () => {}, +}); + +type EmergencyProviderProps = { children: ReactNode; }; -export const EmergencyProvider: React.FC = ({ children }) => { - const [showModal, setShowModal] = useState(false); +export const EmergencyProvider: FC = ({ children }) => { + const [isModalOpen, setIsModalOpen] = useState<{ [key: string]: boolean }>({}); - const toggleModal = (force: boolean) => setShowModal((prev) => (force !== undefined ? force : !prev)); + const toggleModal = (id: string, force?: boolean) => { + setIsModalOpen((prev) => ({ + ...prev, + [id]: force !== undefined ? force : !prev[id], + })); + }; - return {children}; + return {children}; }; export const useModal = () => { diff --git a/src/helpers/constants.ts b/src/helpers/constants.ts index 431ddc2f..d0eb3d6e 100644 --- a/src/helpers/constants.ts +++ b/src/helpers/constants.ts @@ -18,3 +18,5 @@ export const mapToIdAndLabel = (data: any) => { label: data[key], })); }; + +export const COOKIE_CONSENT_KEY = 'ajudana_cookie_consent'; From 2e70a972637e21987791322f8fafea991c352d7b Mon Sep 17 00:00:00 2001 From: Martin Gerritsen Date: Wed, 6 Nov 2024 23:56:03 +0100 Subject: [PATCH 2/3] More clear name on the cookie consent key --- src/components/CookieBanner/CookieBanner.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/CookieBanner/CookieBanner.tsx b/src/components/CookieBanner/CookieBanner.tsx index b7f79932..edffe6be 100644 --- a/src/components/CookieBanner/CookieBanner.tsx +++ b/src/components/CookieBanner/CookieBanner.tsx @@ -8,7 +8,7 @@ import { usePathname } from 'next/navigation'; const MODAL_NAME = 'cookie-banner'; const POLICY_URL = '/politica-privacidad'; -const COOKIE_CONSENT_KEY = 'cookieConsentAccepted'; +const COOKIE_CONSENT_KEY = 'ajudadanaCookieConsentAccepted'; const CookieBanner: FC = () => { const { toggleModal } = useModal(); From 078fd941c89976a40b42271e45c8254308f03607 Mon Sep 17 00:00:00 2001 From: Martin Gerritsen Date: Thu, 7 Nov 2024 08:14:50 +0100 Subject: [PATCH 3/3] Removed Cookie key in constant --- src/components/CookieBanner/CookieBanner.tsx | 2 +- src/helpers/constants.ts | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/components/CookieBanner/CookieBanner.tsx b/src/components/CookieBanner/CookieBanner.tsx index edffe6be..d99de933 100644 --- a/src/components/CookieBanner/CookieBanner.tsx +++ b/src/components/CookieBanner/CookieBanner.tsx @@ -8,7 +8,7 @@ import { usePathname } from 'next/navigation'; const MODAL_NAME = 'cookie-banner'; const POLICY_URL = '/politica-privacidad'; -const COOKIE_CONSENT_KEY = 'ajudadanaCookieConsentAccepted'; +const COOKIE_CONSENT_KEY = 'ajudaDanaCookieConsentAccepted'; const CookieBanner: FC = () => { const { toggleModal } = useModal(); diff --git a/src/helpers/constants.ts b/src/helpers/constants.ts index 5d9b87cf..f89eded4 100644 --- a/src/helpers/constants.ts +++ b/src/helpers/constants.ts @@ -20,5 +20,3 @@ export const mapToIdAndLabel = (data: any) => { label: data[key], })); }; - -export const COOKIE_CONSENT_KEY = 'ajudana_cookie_consent';