forked from fahrPool/fahrPool
feat: add Fahrzeuge page with inline-editable table
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
parent
f8d466ccbf
commit
16b0300488
|
|
@ -0,0 +1,21 @@
|
|||
import { redirect } from "next/navigation";
|
||||
import { loadProfile } from "@/lib/auth/profile";
|
||||
import { kannStammdatenPflegen } from "@/lib/auth/roles";
|
||||
import { listFahrzeuge } from "@/lib/auftraege/queries";
|
||||
import { FahrzeugeTabelle } from "@/components/fahrzeuge/FahrzeugeTabelle";
|
||||
|
||||
export default async function FahrzeugePage() {
|
||||
const profileResult = await loadProfile();
|
||||
if (profileResult.status !== "ok" || !kannStammdatenPflegen(profileResult.profile.role)) {
|
||||
redirect(`/auftraege?error=${encodeURIComponent("Nur Disponent/Admin duerfen Stammdaten pflegen.")}`);
|
||||
}
|
||||
|
||||
const fahrzeuge = await listFahrzeuge();
|
||||
|
||||
return (
|
||||
<div className="mx-auto max-w-4xl p-4 md:p-8">
|
||||
<h1 className="mb-6 text-xl font-semibold text-asphalt-grey">Fahrzeuge</h1>
|
||||
<FahrzeugeTabelle fahrzeuge={fahrzeuge} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
@ -0,0 +1,118 @@
|
|||
"use client";
|
||||
|
||||
import { useState, useTransition } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import type { Fahrzeug } from "@/lib/auftraege/queries";
|
||||
import { updateFahrzeug } from "@/app/fahrzeuge/actions";
|
||||
import { Card } from "@/components/ui/Card";
|
||||
import { Button } from "@/components/ui/Button";
|
||||
import { Alert } from "@/components/ui/Alert";
|
||||
import { FormField, FIELD_CLASSES } from "@/components/ui/FormField";
|
||||
|
||||
export function FahrzeugZeile({ fahrzeug }: { fahrzeug: Fahrzeug }) {
|
||||
const router = useRouter();
|
||||
const [bearbeiten, setBearbeiten] = useState(false);
|
||||
const [fehler, setFehler] = useState<string | null>(null);
|
||||
const [isPending, startTransition] = useTransition();
|
||||
|
||||
function handleSubmit(formData: FormData) {
|
||||
startTransition(async () => {
|
||||
const ergebnis = await updateFahrzeug(fahrzeug.id, formData);
|
||||
if (!ergebnis.success) {
|
||||
setFehler(ergebnis.error);
|
||||
return;
|
||||
}
|
||||
setFehler(null);
|
||||
setBearbeiten(false);
|
||||
router.refresh();
|
||||
});
|
||||
}
|
||||
|
||||
if (!bearbeiten) {
|
||||
return (
|
||||
<Card
|
||||
role="row"
|
||||
className="md:grid md:grid-cols-[1fr_1fr_1fr_auto] md:items-center md:gap-4 md:rounded-none md:border-0 md:border-b md:p-0 md:px-4 md:py-3 md:shadow-none"
|
||||
>
|
||||
<div role="cell">
|
||||
<span className="text-xs font-medium text-asphalt-grey md:hidden">Kennzeichen</span>
|
||||
<p className="text-asphalt-grey">{fahrzeug.kennzeichen}</p>
|
||||
</div>
|
||||
<div role="cell" className="mt-2 md:mt-0">
|
||||
<span className="text-xs font-medium text-asphalt-grey md:hidden">Marke</span>
|
||||
<p className="text-asphalt-grey">{fahrzeug.marke ?? "—"}</p>
|
||||
</div>
|
||||
<div role="cell" className="mt-2 md:mt-0">
|
||||
<span className="text-xs font-medium text-asphalt-grey md:hidden">Modell</span>
|
||||
<p className="text-asphalt-grey">{fahrzeug.modell ?? "—"}</p>
|
||||
</div>
|
||||
<div role="cell" className="mt-3 md:mt-0">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setBearbeiten(true)}
|
||||
className="min-h-11 text-sm text-performance-blue hover:underline"
|
||||
>
|
||||
Bearbeiten
|
||||
</button>
|
||||
</div>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<Card role="row" className="md:rounded-none md:border-0 md:border-b">
|
||||
<form
|
||||
action={handleSubmit}
|
||||
className="flex flex-col gap-3 md:grid md:grid-cols-[1fr_1fr_1fr_auto_auto] md:items-end md:gap-4"
|
||||
>
|
||||
{fehler && (
|
||||
<div className="md:col-span-5">
|
||||
<Alert>{fehler}</Alert>
|
||||
</div>
|
||||
)}
|
||||
<FormField label="Kennzeichen" htmlFor={`kennzeichen-${fahrzeug.id}`}>
|
||||
<input
|
||||
id={`kennzeichen-${fahrzeug.id}`}
|
||||
name="kennzeichen"
|
||||
type="text"
|
||||
required
|
||||
defaultValue={fahrzeug.kennzeichen}
|
||||
className={FIELD_CLASSES}
|
||||
/>
|
||||
</FormField>
|
||||
<FormField label="Marke" htmlFor={`marke-${fahrzeug.id}`}>
|
||||
<input
|
||||
id={`marke-${fahrzeug.id}`}
|
||||
name="marke"
|
||||
type="text"
|
||||
defaultValue={fahrzeug.marke ?? ""}
|
||||
className={FIELD_CLASSES}
|
||||
/>
|
||||
</FormField>
|
||||
<FormField label="Modell" htmlFor={`modell-${fahrzeug.id}`}>
|
||||
<input
|
||||
id={`modell-${fahrzeug.id}`}
|
||||
name="modell"
|
||||
type="text"
|
||||
defaultValue={fahrzeug.modell ?? ""}
|
||||
className={FIELD_CLASSES}
|
||||
/>
|
||||
</FormField>
|
||||
<Button type="submit" fullWidth={false} disabled={isPending}>
|
||||
Speichern
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
variant="secondary"
|
||||
fullWidth={false}
|
||||
onClick={() => {
|
||||
setBearbeiten(false);
|
||||
setFehler(null);
|
||||
}}
|
||||
>
|
||||
Abbrechen
|
||||
</Button>
|
||||
</form>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
import type { Fahrzeug } from "@/lib/auftraege/queries";
|
||||
import { FahrzeugZeile } from "./FahrzeugZeile";
|
||||
import { NeuerFahrzeug } from "./NeuerFahrzeug";
|
||||
|
||||
export function FahrzeugeTabelle({ fahrzeuge }: { fahrzeuge: Fahrzeug[] }) {
|
||||
return (
|
||||
<div role="table" aria-label="Fahrzeuge" className="w-full">
|
||||
<div
|
||||
role="row"
|
||||
className="hidden border-b border-aluminium-grey px-4 py-2 text-sm font-semibold text-asphalt-grey md:grid md:grid-cols-[1fr_1fr_1fr_auto]"
|
||||
>
|
||||
<div role="columnheader">Kennzeichen</div>
|
||||
<div role="columnheader">Marke</div>
|
||||
<div role="columnheader">Modell</div>
|
||||
<div role="columnheader">
|
||||
<span className="sr-only">Aktionen</span>
|
||||
</div>
|
||||
</div>
|
||||
<div role="rowgroup" className="flex flex-col gap-3 md:gap-0">
|
||||
{fahrzeuge.map((fahrzeug) => (
|
||||
<FahrzeugZeile key={fahrzeug.id} fahrzeug={fahrzeug} />
|
||||
))}
|
||||
<NeuerFahrzeug />
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
@ -0,0 +1,81 @@
|
|||
"use client";
|
||||
|
||||
import { useState, useTransition } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { createFahrzeug } from "@/app/fahrzeuge/actions";
|
||||
import { Card } from "@/components/ui/Card";
|
||||
import { Button } from "@/components/ui/Button";
|
||||
import { Alert } from "@/components/ui/Alert";
|
||||
import { FormField, FIELD_CLASSES } from "@/components/ui/FormField";
|
||||
|
||||
export function NeuerFahrzeug() {
|
||||
const router = useRouter();
|
||||
const [offen, setOffen] = useState(false);
|
||||
const [fehler, setFehler] = useState<string | null>(null);
|
||||
const [isPending, startTransition] = useTransition();
|
||||
|
||||
function handleSubmit(formData: FormData) {
|
||||
startTransition(async () => {
|
||||
const ergebnis = await createFahrzeug(formData);
|
||||
if (!ergebnis.success) {
|
||||
setFehler(ergebnis.error);
|
||||
return;
|
||||
}
|
||||
setFehler(null);
|
||||
setOffen(false);
|
||||
router.refresh();
|
||||
});
|
||||
}
|
||||
|
||||
if (!offen) {
|
||||
return (
|
||||
<div role="row" className="p-4 md:px-4 md:py-3">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setOffen(true)}
|
||||
className="min-h-11 text-sm text-performance-blue hover:underline"
|
||||
>
|
||||
+ Neues Fahrzeug anlegen
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<Card role="row" className="md:rounded-none md:border-0 md:border-b">
|
||||
<form
|
||||
action={handleSubmit}
|
||||
className="flex flex-col gap-3 md:grid md:grid-cols-[1fr_1fr_1fr_auto_auto] md:items-end md:gap-4"
|
||||
>
|
||||
{fehler && (
|
||||
<div className="md:col-span-5">
|
||||
<Alert>{fehler}</Alert>
|
||||
</div>
|
||||
)}
|
||||
<FormField label="Kennzeichen" htmlFor="neu-fahrzeug-kennzeichen">
|
||||
<input id="neu-fahrzeug-kennzeichen" name="kennzeichen" type="text" required className={FIELD_CLASSES} />
|
||||
</FormField>
|
||||
<FormField label="Marke" htmlFor="neu-fahrzeug-marke">
|
||||
<input id="neu-fahrzeug-marke" name="marke" type="text" className={FIELD_CLASSES} />
|
||||
</FormField>
|
||||
<FormField label="Modell" htmlFor="neu-fahrzeug-modell">
|
||||
<input id="neu-fahrzeug-modell" name="modell" type="text" className={FIELD_CLASSES} />
|
||||
</FormField>
|
||||
<Button type="submit" fullWidth={false} disabled={isPending}>
|
||||
Anlegen
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
variant="secondary"
|
||||
fullWidth={false}
|
||||
onClick={() => {
|
||||
setOffen(false);
|
||||
setFehler(null);
|
||||
}}
|
||||
>
|
||||
Abbrechen
|
||||
</Button>
|
||||
</form>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
Loading…
Reference in New Issue