"use client"; import { notFound } from 'next/navigation'; import { LocationDeleteForm, LocationDeleteFormSkeleton } from '@/app/ui/LocationDeleteForm'; import { WithId } from 'mongodb'; import { BillingLocation } from '@/app/lib/db-types'; import { useEffect, useState } from 'react'; const fetchLocationById = async (locationId: string) => { const response = await fetch(`/api/locations/by-id?id=${locationId}`); const json = await response.json(); return json.location as WithId; } const LocationDeletePage = ({ locationId }: { locationId:string }) => { const [state, stateSet] = useState<{ status: 'loading' | 'error' | 'success'; location?: WithId; error?: string; }>({ status: 'loading' }); useEffect(() => { const fetchLocation = async () => { try { const location = await fetchLocationById(locationId); stateSet({ location, status: 'success' }); } catch(error:any) { stateSet({ status: 'error', error: error.message }); } }; fetchLocation(); }, [locationId]); switch(state.status) { case "error": return(
Error: {state.error}
); case "loading": return(); case "success": if (!state.location) { return(notFound()); } return(); default: return(
Error: Unknown status
); } } export default LocationDeletePage;