Location Edit/Add/Delete form migrated back to server-side component
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { LocationEditForm } from '@/app/ui/LocationEditForm';
|
||||
import { YearMonth } from '@/app/lib/db-types';
|
||||
|
||||
export default function LocationAddPage({ yearMonth }: { yearMonth:YearMonth }) {
|
||||
export default async function LocationAddPage({ yearMonth }: { yearMonth:YearMonth }) {
|
||||
return (<LocationEditForm yearMonth={yearMonth} />);
|
||||
}
|
||||
@@ -1,11 +1,6 @@
|
||||
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 (
|
||||
|
||||
@@ -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} />);
|
||||
}
|
||||
@@ -1,17 +1,14 @@
|
||||
import { Main } from '@/app/ui/Main';
|
||||
import dynamic from 'next/dynamic'
|
||||
|
||||
const LocationDeletePage = dynamic(
|
||||
() => import('./LocationDeletePage'),
|
||||
{ ssr: false }
|
||||
)
|
||||
|
||||
import { notFound } from 'next/navigation';
|
||||
import { fetchLocationById } from '@/app/lib/actions/locationActions';
|
||||
import { LocationDeleteForm } from '@/app/ui/LocationDeleteForm';
|
||||
|
||||
export default async function Page({ params:{ id } }: { params: { id:string } }) {
|
||||
|
||||
return (
|
||||
<Main>
|
||||
<LocationDeletePage locationId={id} />
|
||||
</Main>
|
||||
);
|
||||
const location = await fetchLocationById(id);
|
||||
|
||||
if (!location) {
|
||||
return(notFound());
|
||||
}
|
||||
|
||||
return (<LocationDeleteForm location={location} />);
|
||||
}
|
||||
@@ -1,53 +1,16 @@
|
||||
"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';
|
||||
import { LocationEditForm } from '@/app/ui/LocationEditForm';
|
||||
import { fetchLocationById } from '@/app/lib/actions/locationActions';
|
||||
|
||||
export default async function LocationEditPage({ 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<BillingLocation>;
|
||||
}
|
||||
const location = await fetchLocationById(locationId);
|
||||
|
||||
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>);
|
||||
if (!location) {
|
||||
return(notFound());
|
||||
}
|
||||
|
||||
const result = <LocationEditForm location={location} />;
|
||||
|
||||
return (result);
|
||||
}
|
||||
@@ -1,17 +1,15 @@
|
||||
import { Suspense } from 'react';
|
||||
import LocationEditPage from './LocationEditPage';
|
||||
import { Main } from '@/app/ui/Main';
|
||||
import dynamic from 'next/dynamic'
|
||||
|
||||
const LocationEditPage = dynamic(
|
||||
() => import('./LocationEditPage'),
|
||||
{ ssr: false }
|
||||
)
|
||||
|
||||
import { LocationEditFormSkeleton } from '@/app/ui/LocationEditForm';
|
||||
|
||||
export default async function Page({ params:{ id } }: { params: { id:string } }) {
|
||||
|
||||
return (
|
||||
<Main>
|
||||
<LocationEditPage locationId={id} />
|
||||
<Suspense fallback={<LocationEditFormSkeleton />}>
|
||||
<LocationEditPage locationId={id} />
|
||||
</Suspense>
|
||||
</Main>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user