53 lines
1.6 KiB
TypeScript
53 lines
1.6 KiB
TypeScript
"use client";
|
|
|
|
import { notFound } from 'next/navigation';
|
|
import { LocationEditForm, LocationEditFormSkeleton } from '@/app/ui/LocationEditForm';
|
|
import { useEffect, useState } from 'react';
|
|
import { WithId } from 'mongodb';
|
|
import { BillingLocation } from '@/app/lib/db-types';
|
|
|
|
|
|
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 default function LocationEditPage({ locationId }: { locationId:string }) {
|
|
|
|
const [state, stateSet] = useState<{
|
|
status: 'loading' | 'error' | 'success';
|
|
location?: WithId<BillingLocation>;
|
|
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(<div>Error: {state.error}</div>);
|
|
case "loading":
|
|
return(<LocationEditFormSkeleton />);
|
|
case "success":
|
|
if (!state.location) {
|
|
return(notFound());
|
|
}
|
|
|
|
return(<LocationEditForm location={state.location} />);
|
|
default:
|
|
return(<div>Error: Unknown status</div>);
|
|
}
|
|
} |