Location Edit/Add/Delete form migrated back to server-side component

This commit is contained in:
2024-02-09 09:43:55 +01:00
parent 27b696faab
commit 9609f7da54
6 changed files with 36 additions and 125 deletions

View File

@@ -1,54 +1,14 @@
"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';
import { fetchLocationById } from '@/app/lib/actions/locationActions';
import { LocationDeleteForm } from '@/app/ui/LocationDeleteForm';
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<BillingLocation>;
}
export const LocationDeletePage = async ({ locationId }: { locationId:string }) => {
const LocationDeletePage = ({ locationId }: { locationId:string }) => {
const [state, stateSet] = useState<{
status: 'loading' | 'error' | 'success';
location?: WithId<BillingLocation>;
error?: string;
}>({ status: 'loading' });
const location = await fetchLocationById(locationId);
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(<div>Error: {state.error}</div>);
case "loading":
return(<LocationDeleteFormSkeleton />);
case "success":
if (!state.location) {
return(notFound());
}
return(<LocationDeleteForm location={state.location} />);
default:
return(<div>Error: Unknown status</div>);
if (!location) {
return(notFound());
}
}
export default LocationDeletePage;
return (<LocationDeleteForm location={location} />);
}