diff --git a/src/app/casos-activos/solicitudes/page.js b/src/app/casos-activos/solicitudes/page.js index b3731a03..c1f78bdf 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 c121fa78..956a2ba3 100644 --- a/src/components/map/map.tsx +++ b/src/components/map/map.tsx @@ -5,10 +5,11 @@ import ReactMap from 'react-map-gl/maplibre'; import 'maplibre-gl/dist/maplibre-gl.css'; import { Marker } from 'react-map-gl/maplibre'; import { useModal } from '@/context/EmergencyProvider'; -// @ts-ignore import Modal from '@/components/Modal'; import { MapPinFilled } from '@/components/icons/MapPinFilled'; +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 = () => {