Location Edit / Add / Delete migrated to client-side rendering
This commit is contained in:
@@ -1,6 +1,11 @@
|
|||||||
import { parseYearMonth } from '@/app/lib/format';
|
import { parseYearMonth } from '@/app/lib/format';
|
||||||
import LocationAddPage from './LocationAddPage';
|
|
||||||
import { Main } from '@/app/ui/Main';
|
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 } }) {
|
export default async function Page({ params:{ id } }: { params: { id:string } }) {
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -1,14 +1,54 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
import { notFound } from 'next/navigation';
|
import { notFound } from 'next/navigation';
|
||||||
import { fetchLocationById } from '@/app/lib/actions/locationActions';
|
import { LocationDeleteForm, LocationDeleteFormSkeleton } from '@/app/ui/LocationDeleteForm';
|
||||||
import { LocationDeleteForm } 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<BillingLocation>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const LocationDeletePage = ({ 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);
|
const location = await fetchLocationById(locationId);
|
||||||
|
stateSet({ location, status: 'success' });
|
||||||
|
} catch(error:any) {
|
||||||
|
stateSet({ status: 'error', error: error.message });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if (!location) {
|
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(notFound());
|
||||||
}
|
}
|
||||||
|
|
||||||
return (<LocationDeleteForm location={location} />);
|
return(<LocationDeleteForm location={state.location} />);
|
||||||
|
default:
|
||||||
|
return(<div>Error: Unknown status</div>);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default LocationDeletePage;
|
||||||
@@ -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 { Main } from '@/app/ui/Main';
|
||||||
import { Suspense } from 'react';
|
import dynamic from 'next/dynamic'
|
||||||
import { LocationDeletePage } from './LocationDeletePage';
|
|
||||||
|
const LocationDeletePage = dynamic(
|
||||||
|
() => import('./LocationDeletePage'),
|
||||||
|
{ ssr: false }
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
export default async function Page({ params:{ id } }: { params: { id:string } }) {
|
export default async function Page({ params:{ id } }: { params: { id:string } }) {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Main>
|
<Main>
|
||||||
<Suspense fallback={<div>Loading...</div>}>
|
|
||||||
<LocationDeletePage locationId={id} />
|
<LocationDeletePage locationId={id} />
|
||||||
</Suspense>
|
|
||||||
</Main>
|
</Main>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,11 @@
|
|||||||
import LocationEditPage from './LocationEditPage';
|
|
||||||
import { Main } from '@/app/ui/Main';
|
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 } }) {
|
export default async function Page({ params:{ id } }: { params: { id:string } }) {
|
||||||
|
|
||||||
|
|||||||
@@ -29,11 +29,23 @@ export const LocationDeleteForm:FC<LocationDeleteFormProps> = ({ location }) =>
|
|||||||
Please confirm deletion of location “<strong>{location.name}</strong>”.
|
Please confirm deletion of location “<strong>{location.name}</strong>”.
|
||||||
</p>
|
</p>
|
||||||
<div className="pt-4 text-center">
|
<div className="pt-4 text-center">
|
||||||
<button className="btn btn-primary">Confim</button>
|
<button className="btn btn-primary w-[5.5em]">Confim</button>
|
||||||
<Link className="btn btn-neutral ml-3" href={`/location/${location._id}/edit/`}>Cancel</Link>
|
<Link className="btn btn-neutral w-[5.5em] ml-3" href={`/location/${location._id}/edit/`}>Cancel</Link>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export const LocationDeleteFormSkeleton:FC = () =>
|
||||||
|
<div className="card card-compact card-bordered min-w-[20em] max-w-[90em] bg-base-100 shadow-s my-1">
|
||||||
|
<div className="card-body">
|
||||||
|
<p className="py-6 px-6"></p>
|
||||||
|
<div className="pt-4 text-center">
|
||||||
|
<div className="btn skeleton w-[5.5em]"></div>
|
||||||
|
<div className="btn ml-3 skeleton w-[5.5em]"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
@@ -47,7 +47,7 @@ export const LocationEditForm:FC<LocationEditFormProps> = ({ location, yearMonth
|
|||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<textarea id="locationNotes" name="locationNotes" className="textarea textarea-bordered my-1 w-full block" placeholder="Opis" defaultValue={location?.notes ?? ""}></textarea>
|
<textarea id="locationNotes" name="locationNotes" className="textarea textarea-bordered my-1 w-full block h-[8em]" placeholder="Opis" defaultValue={location?.notes ?? ""}></textarea>
|
||||||
<div id="status-error" aria-live="polite" aria-atomic="true">
|
<div id="status-error" aria-live="polite" aria-atomic="true">
|
||||||
{state.errors?.locationNotes &&
|
{state.errors?.locationNotes &&
|
||||||
state.errors.locationNotes.map((error: string) => (
|
state.errors.locationNotes.map((error: string) => (
|
||||||
@@ -79,12 +79,12 @@ export const LocationEditFormSkeleton:FC = () =>
|
|||||||
{
|
{
|
||||||
return(
|
return(
|
||||||
<div className="card card-compact card-bordered min-w-[20em] max-w-[90em] bg-base-100 shadow-s my-1">
|
<div className="card card-compact card-bordered min-w-[20em] max-w-[90em] bg-base-100 shadow-s my-1">
|
||||||
<div className="card-body skeleton">
|
<div className="card-body">
|
||||||
<div id="locationName" className="input input-bordered w-full"></div>
|
<div id="locationName" className="input w-full skeleton"></div>
|
||||||
<div id="locationNotes" className="textarea textarea-bordered my-1 w-full block"></div>
|
<div id="locationNotes" className="textarea my-1 w-full block h-[8em] skeleton"></div>
|
||||||
<div className="pt-4">
|
<div className="pt-4">
|
||||||
<div className="btn btn-neutral w-[5.5em]"></div>
|
<div className="btn w-[5.5em] skeleton"></div>
|
||||||
<div className="btn btn-neutral w-[5.5em] ml-3"></div>
|
<div className="btn w-[5.5em] ml-3 skeleton"></div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user