forked from fahrPool/fahrPool
feat: Eingaben bleiben bei Netzausfall beim Absenden erhalten
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
c7d9131f56
commit
906b822250
|
|
@ -12,6 +12,7 @@ import { FotoGalerie } from "@/components/auftraege/FotoGalerie";
|
|||
import { SchadenFotoFelder } from "@/components/auftraege/SchadenFotoFelder";
|
||||
import { UnterschriftCanvas } from "@/components/auftraege/UnterschriftCanvas";
|
||||
import { uebernehmen, fahrtBeginnen, schadenFotoNachtragen, fahrtBeenden, stornieren } from "./actions";
|
||||
import { NetzSicheresFormular } from "@/components/pwa/NetzSicheresFormular";
|
||||
import { Card } from "@/components/ui/Card";
|
||||
import { StatusBadge } from "@/components/ui/StatusBadge";
|
||||
import { Button, buttonClasses } from "@/components/ui/Button";
|
||||
|
|
@ -201,15 +202,15 @@ export default async function AuftragDetailPage({
|
|||
|
||||
{auftrag.status === "offen" && (
|
||||
<Card className="mb-6">
|
||||
<form action={uebernehmenAction}>
|
||||
<NetzSicheresFormular action={uebernehmenAction}>
|
||||
<Button type="submit">Auftrag übernehmen</Button>
|
||||
</form>
|
||||
</NetzSicheresFormular>
|
||||
</Card>
|
||||
)}
|
||||
|
||||
{auftrag.status === "zugewiesen" && istEigenerAuftrag && (
|
||||
<Card className="mb-6">
|
||||
<form action={fahrtBeginnenAction}>
|
||||
<NetzSicheresFormular action={fahrtBeginnenAction}>
|
||||
<h2 className="mb-4 text-lg font-semibold text-asphalt-grey">Fahrt beginnen</h2>
|
||||
<FormField label="KM-Stand" htmlFor="start_km">
|
||||
<input id="start_km" name="start_km" type="number" min="0" required className={FIELD_CLASSES} />
|
||||
|
|
@ -246,13 +247,13 @@ export default async function AuftragDetailPage({
|
|||
))}
|
||||
<SchadenFotoFelder />
|
||||
<Button type="submit">Fahrt beginnen</Button>
|
||||
</form>
|
||||
</NetzSicheresFormular>
|
||||
</Card>
|
||||
)}
|
||||
|
||||
{auftrag.status === "unterwegs" && istEigenerAuftrag && (
|
||||
<Card className="mb-6">
|
||||
<form action={fahrtBeendenAction}>
|
||||
<NetzSicheresFormular action={fahrtBeendenAction}>
|
||||
<h2 className="mb-4 text-lg font-semibold text-asphalt-grey">Fahrt beenden</h2>
|
||||
<FormField label="End-KM-Stand" htmlFor="end_km">
|
||||
<input id="end_km" name="end_km" type="number" min="0" required className={FIELD_CLASSES} />
|
||||
|
|
@ -270,17 +271,17 @@ export default async function AuftragDetailPage({
|
|||
<UnterschriftCanvas name="unterschrift_data_url" />
|
||||
</FormField>
|
||||
<Button type="submit">Übergabe bestätigen</Button>
|
||||
</form>
|
||||
</NetzSicheresFormular>
|
||||
</Card>
|
||||
)}
|
||||
|
||||
{auftrag.status === "unterwegs" && istEigenerAuftrag && (
|
||||
<Card className="mb-6">
|
||||
<form action={schadenFotoNachtragenAction}>
|
||||
<NetzSicheresFormular action={schadenFotoNachtragenAction}>
|
||||
<h2 className="mb-4 text-lg font-semibold text-asphalt-grey">Schadensfoto nachtragen</h2>
|
||||
<SchadenFotoFelder />
|
||||
<Button type="submit">Foto nachtragen</Button>
|
||||
</form>
|
||||
</NetzSicheresFormular>
|
||||
</Card>
|
||||
)}
|
||||
|
||||
|
|
@ -297,11 +298,11 @@ export default async function AuftragDetailPage({
|
|||
|
||||
{kannStornieren && auftrag.status !== "abgeschlossen" && auftrag.status !== "storniert" && (
|
||||
<Card>
|
||||
<form action={stornierenAction}>
|
||||
<NetzSicheresFormular action={stornierenAction}>
|
||||
<Button type="submit" variant="destructive">
|
||||
Auftrag stornieren
|
||||
</Button>
|
||||
</form>
|
||||
</NetzSicheresFormular>
|
||||
</Card>
|
||||
)}
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,53 @@
|
|||
"use client";
|
||||
|
||||
import { useRef, useState, useTransition } from "react";
|
||||
import { Alert } from "@/components/ui/Alert";
|
||||
import { istNetzwerkFehler, NETZ_FEHLER_TEXT } from "@/lib/pwa/netzwerk-fehler";
|
||||
|
||||
/**
|
||||
* Formular, das bei Netzausfall alle Eingaben (inkl. gewaehlter Dateien)
|
||||
* behaelt: Es wird kein Reset ausgefuehrt, bei Netzwerkfehler erscheint nur
|
||||
* eine Meldung. Redirects der Action laufen ueber Next weiter.
|
||||
*/
|
||||
export function NetzSicheresFormular({
|
||||
action,
|
||||
children,
|
||||
className,
|
||||
}: {
|
||||
action: (formData: FormData) => Promise<void>;
|
||||
children: React.ReactNode;
|
||||
className?: string;
|
||||
}) {
|
||||
const [netzFehler, setNetzFehler] = useState(false);
|
||||
const [isPending, startTransition] = useTransition();
|
||||
const laeuft = useRef(false);
|
||||
|
||||
function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
|
||||
event.preventDefault();
|
||||
if (laeuft.current) return;
|
||||
laeuft.current = true;
|
||||
const formData = new FormData(event.currentTarget);
|
||||
setNetzFehler(false);
|
||||
startTransition(async () => {
|
||||
try {
|
||||
await action(formData);
|
||||
} catch (e) {
|
||||
if (istNetzwerkFehler(e, navigator.onLine)) setNetzFehler(true);
|
||||
else throw e;
|
||||
} finally {
|
||||
laeuft.current = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return (
|
||||
<form onSubmit={handleSubmit} aria-busy={isPending} className={className}>
|
||||
{netzFehler && (
|
||||
<div className="mb-4">
|
||||
<Alert>{NETZ_FEHLER_TEXT}</Alert>
|
||||
</div>
|
||||
)}
|
||||
{children}
|
||||
</form>
|
||||
);
|
||||
}
|
||||
|
|
@ -5,6 +5,7 @@ import { useRouter } from "next/navigation";
|
|||
import { kommen, gehen } from "@/app/zeiterfassung/actions";
|
||||
import { Button } from "@/components/ui/Button";
|
||||
import { Alert } from "@/components/ui/Alert";
|
||||
import { istNetzwerkFehler, NETZ_FEHLER_TEXT } from "@/lib/pwa/netzwerk-fehler";
|
||||
|
||||
export function KommenGehenButton({ offeneSchichtId }: { offeneSchichtId: string | null }) {
|
||||
const router = useRouter();
|
||||
|
|
@ -13,7 +14,16 @@ export function KommenGehenButton({ offeneSchichtId }: { offeneSchichtId: string
|
|||
|
||||
function handleClick() {
|
||||
startTransition(async () => {
|
||||
const ergebnis = offeneSchichtId ? await gehen(offeneSchichtId) : await kommen();
|
||||
let ergebnis;
|
||||
try {
|
||||
ergebnis = offeneSchichtId ? await gehen(offeneSchichtId) : await kommen();
|
||||
} catch (e) {
|
||||
if (istNetzwerkFehler(e, navigator.onLine)) {
|
||||
setFehler(NETZ_FEHLER_TEXT);
|
||||
return;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
if (!ergebnis.success) {
|
||||
setFehler(ergebnis.error);
|
||||
return;
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import { useRouter } from "next/navigation";
|
|||
import { schichtKorrigieren } from "@/app/zeiterfassung/actions";
|
||||
import { Button } from "@/components/ui/Button";
|
||||
import { Alert } from "@/components/ui/Alert";
|
||||
import { istNetzwerkFehler, NETZ_FEHLER_TEXT } from "@/lib/pwa/netzwerk-fehler";
|
||||
import { FIELD_CLASSES } from "@/components/ui/FormField";
|
||||
|
||||
export function KorrekturFormular({ schichtId }: { schichtId: string }) {
|
||||
|
|
@ -12,9 +13,20 @@ export function KorrekturFormular({ schichtId }: { schichtId: string }) {
|
|||
const [fehler, setFehler] = useState<string | null>(null);
|
||||
const [isPending, startTransition] = useTransition();
|
||||
|
||||
function handleSubmit(formData: FormData) {
|
||||
function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
|
||||
event.preventDefault();
|
||||
const formData = new FormData(event.currentTarget);
|
||||
startTransition(async () => {
|
||||
const ergebnis = await schichtKorrigieren(schichtId, formData);
|
||||
let ergebnis;
|
||||
try {
|
||||
ergebnis = await schichtKorrigieren(schichtId, formData);
|
||||
} catch (e) {
|
||||
if (istNetzwerkFehler(e, navigator.onLine)) {
|
||||
setFehler(NETZ_FEHLER_TEXT);
|
||||
return;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
if (!ergebnis.success) {
|
||||
setFehler(ergebnis.error);
|
||||
return;
|
||||
|
|
@ -25,7 +37,7 @@ export function KorrekturFormular({ schichtId }: { schichtId: string }) {
|
|||
}
|
||||
|
||||
return (
|
||||
<form action={handleSubmit} className="mt-3 flex flex-wrap items-end gap-3">
|
||||
<form onSubmit={handleSubmit} className="mt-3 flex flex-wrap items-end gap-3">
|
||||
{fehler && (
|
||||
<div className="w-full">
|
||||
<Alert>{fehler}</Alert>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,24 @@
|
|||
import { describe, it, expect } from "vitest";
|
||||
import { istNetzwerkFehler, NETZ_FEHLER_TEXT } from "./netzwerk-fehler";
|
||||
|
||||
describe("istNetzwerkFehler", () => {
|
||||
it("erkennt fetch-Fehler der Browser", () => {
|
||||
expect(istNetzwerkFehler(new TypeError("Failed to fetch"), true)).toBe(true);
|
||||
expect(istNetzwerkFehler(new TypeError("Load failed"), true)).toBe(true);
|
||||
expect(istNetzwerkFehler(new TypeError("NetworkError when attempting to fetch resource."), true)).toBe(true);
|
||||
});
|
||||
it("wertet offline immer als Netzwerkfehler", () => {
|
||||
expect(istNetzwerkFehler(new Error("x"), false)).toBe(true);
|
||||
});
|
||||
it("ignoriert Next-interne Fehler mit digest", () => {
|
||||
const e = Object.assign(new Error("NEXT_REDIRECT"), { digest: "NEXT_REDIRECT;replace;/a;307;" });
|
||||
expect(istNetzwerkFehler(e, false)).toBe(false);
|
||||
});
|
||||
it("ignoriert sonstige Fehler wenn online", () => {
|
||||
expect(istNetzwerkFehler(new Error("x"), true)).toBe(false);
|
||||
expect(istNetzwerkFehler(new TypeError("x is not a function"), true)).toBe(false);
|
||||
});
|
||||
it("hat den vorgegebenen Text", () => {
|
||||
expect(NETZ_FEHLER_TEXT).toBe("Kein Netz — Eingaben sind noch da, bitte erneut senden.");
|
||||
});
|
||||
});
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
export const NETZ_FEHLER_TEXT = "Kein Netz — Eingaben sind noch da, bitte erneut senden.";
|
||||
|
||||
const FETCH_MELDUNG = /fetch|network|load failed|networkerror/i;
|
||||
|
||||
/**
|
||||
* Erkennt, ob ein Fehler beim Aufruf einer Server Action vom fehlenden Netz
|
||||
* kommt. Next-interne Fehler (NEXT_REDIRECT, NEXT_NOT_FOUND) tragen ein
|
||||
* `digest` und sind nie Netzwerkfehler, auch nicht bei offline.
|
||||
*/
|
||||
export function istNetzwerkFehler(fehler: unknown, online: boolean): boolean {
|
||||
if (typeof fehler === "object" && fehler !== null && "digest" in fehler) return false;
|
||||
if (!online) return true;
|
||||
return fehler instanceof TypeError && FETCH_MELDUNG.test(fehler.message);
|
||||
}
|
||||
Loading…
Reference in New Issue