diff --git a/app/location/[id]/add/page.tsx b/app/location/[id]/add/page.tsx index 3f032fc..87d0ddf 100644 --- a/app/location/[id]/add/page.tsx +++ b/app/location/[id]/add/page.tsx @@ -1,6 +1,11 @@ import { parseYearMonth } from '@/app/lib/format'; -import LocationAddPage from './LocationAddPage'; import { Main } from '@/app/ui/Main'; +import dynamic from 'next/dynamic' + +const LocationAddPage = dynamic( + () => import('./LocationAddPage'), + { ssr: false } + ) export default async function Page({ params:{ id } }: { params: { id:string } }) { return ( diff --git a/app/location/[id]/delete/LocationDeletePage.tsx b/app/location/[id]/delete/LocationDeletePage.tsx index 7377733..ec8663c 100644 --- a/app/location/[id]/delete/LocationDeletePage.tsx +++ b/app/location/[id]/delete/LocationDeletePage.tsx @@ -1,14 +1,54 @@ +"use client"; + import { notFound } from 'next/navigation'; -import { fetchLocationById } from '@/app/lib/actions/locationActions'; -import { LocationDeleteForm } from '@/app/ui/LocationDeleteForm'; +import { LocationDeleteForm, LocationDeleteFormSkeleton } from '@/app/ui/LocationDeleteForm'; +import { WithId } from 'mongodb'; +import { BillingLocation } from '@/app/lib/db-types'; +import { useEffect, useState } from 'react'; -export const LocationDeletePage = async ({ locationId }: { locationId:string }) => { +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 location = await fetchLocationById(locationId); +const LocationDeletePage = ({ locationId }: { locationId:string }) => { + + const [state, stateSet] = useState<{ + status: 'loading' | 'error' | 'success'; + location?: WithId; + error?: string; + }>({ status: 'loading' }); - if (!location) { - return(notFound()); + 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
); } +} - return (); -} \ No newline at end of file +export default LocationDeletePage; \ No newline at end of file diff --git a/app/location/[id]/delete/page.tsx b/app/location/[id]/delete/page.tsx index 6ae17d5..ca0db96 100644 --- a/app/location/[id]/delete/page.tsx +++ b/app/location/[id]/delete/page.tsx @@ -1,17 +1,17 @@ -import { notFound } from 'next/navigation'; -import { fetchLocationById } from '@/app/lib/actions/locationActions'; -import { LocationDeleteForm } from '@/app/ui/LocationDeleteForm'; import { Main } from '@/app/ui/Main'; -import { Suspense } from 'react'; -import { LocationDeletePage } from './LocationDeletePage'; +import dynamic from 'next/dynamic' + +const LocationDeletePage = dynamic( + () => import('./LocationDeletePage'), + { ssr: false } + ) + export default async function Page({ params:{ id } }: { params: { id:string } }) { return (
- Loading...}> - - +
); } \ No newline at end of file diff --git a/app/location/[id]/edit/page.tsx b/app/location/[id]/edit/page.tsx index 7e63bcb..0fb2160 100644 --- a/app/location/[id]/edit/page.tsx +++ b/app/location/[id]/edit/page.tsx @@ -1,5 +1,11 @@ -import LocationEditPage from './LocationEditPage'; import { Main } from '@/app/ui/Main'; +import dynamic from 'next/dynamic' + +const LocationEditPage = dynamic( + () => import('./LocationEditPage'), + { ssr: false } + ) + export default async function Page({ params:{ id } }: { params: { id:string } }) { diff --git a/app/ui/LocationDeleteForm.tsx b/app/ui/LocationDeleteForm.tsx index 35eb104..7f6651d 100644 --- a/app/ui/LocationDeleteForm.tsx +++ b/app/ui/LocationDeleteForm.tsx @@ -29,11 +29,23 @@ export const LocationDeleteForm:FC = ({ location }) => Please confirm deletion of location “{location.name}”.

- - Cancel + + Cancel
); } + + +export const LocationDeleteFormSkeleton:FC = () => +
+
+

+
+
+
+
+
+
\ No newline at end of file diff --git a/app/ui/LocationEditForm.tsx b/app/ui/LocationEditForm.tsx index 1c1f86a..b1fdc29 100644 --- a/app/ui/LocationEditForm.tsx +++ b/app/ui/LocationEditForm.tsx @@ -47,7 +47,7 @@ export const LocationEditForm:FC = ({ location, yearMonth ))} - +
{state.errors?.locationNotes && state.errors.locationNotes.map((error: string) => ( @@ -79,12 +79,12 @@ export const LocationEditFormSkeleton:FC = () => { return(
-
-
-
+
+
+
-
-
+
+