From 0bb4c1206197650c6e73002adae9e74a7ccac448 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Dere=C5=BEi=C4=87?= Date: Thu, 8 Feb 2024 13:50:48 +0100 Subject: [PATCH] LocationEditForm: migrated to client-side rendering --- app/api/locations/by-id/route.ts | 13 +++++ app/location/[id]/edit/LocationEditPage.tsx | 57 +++++++++++++++++---- app/location/[id]/edit/page.tsx | 6 +-- app/ui/LocationEditForm.tsx | 17 +++--- 4 files changed, 71 insertions(+), 22 deletions(-) create mode 100644 app/api/locations/by-id/route.ts diff --git a/app/api/locations/by-id/route.ts b/app/api/locations/by-id/route.ts new file mode 100644 index 0000000..605b5d6 --- /dev/null +++ b/app/api/locations/by-id/route.ts @@ -0,0 +1,13 @@ +import { fetchLocationById } from '@/app/lib/actions/locationActions'; +import type { NextApiRequest } from 'next' +import { NextResponse } from 'next/server'; + +export const GET = async ( + req: NextApiRequest, +) => { + const url = new URL(req.url as string); + const locationId = url.searchParams.get('id'); + const location = await fetchLocationById(locationId as string); + + return NextResponse.json({ location }); +} diff --git a/app/location/[id]/edit/LocationEditPage.tsx b/app/location/[id]/edit/LocationEditPage.tsx index 505f634..df97fad 100644 --- a/app/location/[id]/edit/LocationEditPage.tsx +++ b/app/location/[id]/edit/LocationEditPage.tsx @@ -1,16 +1,53 @@ +"use client"; + import { notFound } from 'next/navigation'; -import { LocationEditForm } from '@/app/ui/LocationEditForm'; -import { fetchLocationById } from '@/app/lib/actions/locationActions'; +import { LocationEditForm, LocationEditFormSkeleton } from '@/app/ui/LocationEditForm'; +import { useEffect, useState } from 'react'; +import { WithId } from 'mongodb'; +import { BillingLocation } from '@/app/lib/db-types'; -export default async function LocationEditPage({ locationId }: { locationId:string }) { - const location = await fetchLocationById(locationId); +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; +} - if (!location) { - return(notFound()); +export default function LocationEditPage({ 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
); } - - const result = ; - - return (result); } \ No newline at end of file diff --git a/app/location/[id]/edit/page.tsx b/app/location/[id]/edit/page.tsx index 251f944..7e63bcb 100644 --- a/app/location/[id]/edit/page.tsx +++ b/app/location/[id]/edit/page.tsx @@ -1,15 +1,11 @@ -import { Suspense } from 'react'; import LocationEditPage from './LocationEditPage'; import { Main } from '@/app/ui/Main'; -import { LocationEditFormSkeleton } from '@/app/ui/LocationEditForm'; export default async function Page({ params:{ id } }: { params: { id:string } }) { return (
- }> - - +
); } \ No newline at end of file diff --git a/app/ui/LocationEditForm.tsx b/app/ui/LocationEditForm.tsx index 97148bd..1c1f86a 100644 --- a/app/ui/LocationEditForm.tsx +++ b/app/ui/LocationEditForm.tsx @@ -65,10 +65,9 @@ export const LocationEditForm:FC = ({ location, yearMonth

} -
- - Cancel + + Cancel
@@ -79,10 +78,14 @@ export const LocationEditForm:FC = ({ location, yearMonth export const LocationEditFormSkeleton:FC = () => { return( -
-
- - +
+
+
+
+
+
+
+
)