import React, { useState, useEffect } from 'react'; import Layout from "../../../components/layout"; import { Carousel } from 'react-responsive-carousel'; import "react-responsive-carousel/lib/styles/carousel.min.css"; // requires a loader import { GetServerSideProps } from 'next'; import { Location, UserRole } from "@prisma/client"; import axiosServer from '../../../src/axiosServer'; import { useTranslations, createTranslator } from 'next-intl'; import ReactMarkdown from 'react-markdown'; import rehypeRaw from 'rehype-raw'; import remarkGfm from 'remark-gfm'; // import { getTranslations } from 'next-intl/server'; const ViewLocationPage: React.FC = ({ location }) => { const [activeTab, setActiveTab] = useState('mainLocation'); const [activeImage, setActiveImage] = useState(0); const [images, setImages] = useState([]); const [mainLocationImageCount, setMainLocationImageCount] = useState(0); const t = useTranslations('content'); useEffect(() => { const mainLocationImages = [location.picture1, location.picture2, location.picture3].filter(Boolean); const backupLocationImages = location.backupLocationImages?.filter(Boolean) ?? []; setImages([...mainLocationImages, ...backupLocationImages]); setMainLocationImageCount(mainLocationImages.length); }, [location.picture1, location.picture2, location.picture3, location.backupLocationImages]); const handleTabChange = (tab: string) => { setActiveTab(tab); //show the proper image in the carousel if (tab === 'backupLocation') { setActiveImage(mainLocationImageCount); } else { setActiveImage(0); } }; const handleCarouselChange = (index) => { // Switch to backupLocation tab if the current carousel image index is from the backup location if (index >= mainLocationImageCount) { setActiveTab('backupLocation'); } else { setActiveTab('mainLocation'); } setActiveImage(index); }; return (
{/* Tabs */}
{/* Main Location Tab */} {/* Backup Location Tab */} {location.backupLocationId !== null && ( )}
{/* Carousel */} {images.length > 0 && ( {images.map((src, index) => (
{`Slide
))}
)} {/* Tab Content */} {(location.content || location.backupLocationContent) && (
{activeTab === 'mainLocation' && ( //
( ), a: ({ node, ...props }) => ( ) }} > {location.content}
)} {activeTab === 'backupLocation' && location.backupLocationContent && (
)}
)}
); }; const common = require("../../../src/helpers/common"); export const getServerSideProps: GetServerSideProps = async (context) => { const axios = await axiosServer(context); // Get the locale from context or use default const locale = context.locale || 'en'; const messages = (await import(`../../../content/i18n/${locale}.json`)).default; const t = createTranslator({ locale, messages }); // Function to replace placeholders in HTML content. Placeholders are in the format {key} const replacePlaceholders = (content: string) => { if (!content) return ''; const placeholderPattern = /{([^}]+)}/g; return content.replace(placeholderPattern, (match, key) => { try { const translation = t('content.' + key); // Check if translation exists and is not empty if (translation && translation !== 'content.' + key) { return translation; } // Return formatted placeholder if translation not found return `[${locale}:${key}]`; } catch (error) { // Return formatted placeholder on error return `[${locale}:'content.'${key}]`; } }); }; const prismaClient = common.getPrismaClient(); const location = await getLocation(prismaClient, context); location.content = replacePlaceholders(location.content); location.title = t("content.location." + location.name + ".title"); if (location.backupLocationId !== null) { // const { data: backupLocation } = await axios.get( // process.env.NEXT_PUBLIC_PUBLIC_URL + "/api/data/locations/" + location.backupLocationId // ); const backupLocation = await prismaClient.location.findFirst({ where: { id: location.backupLocationId, }, }); location.backupLocationName = backupLocation.name; location.backupLocationContent = backupLocation ? replacePlaceholders(backupLocation.content) : ""; location.backupLocationImages = backupLocation ? [backupLocation.picture1, backupLocation.picture2, backupLocation.picture3].filter(Boolean) : []; } context.res.setHeader("Cache-Control", "s-maxage=1, stale-while-revalidate"); return { props: { location: location, }, }; }; async function getLocation(prismaClient, context) { // Try to parse the ID as a number const numericId = parseInt(context.params.id); let location; // If it's a valid number, search by ID if (!isNaN(numericId)) { location = await prismaClient.location.findFirst({ where: { id: numericId } }); } // If no location found by ID or if ID is not numeric, search by name if (!location) { location = await prismaClient.location.findFirst({ where: { name: context.params.id } }); } return location; } export default ViewLocationPage;