forked from fahrPool/fahrPool
fix: use onSubmit instead of form action to prevent field reset on error, rename ActionResult type
Finding 1: React 19's native <form action={fn}> resets uncontrolled
fields once fn resolves without throwing, regardless of an app-level
{success:false} result. Since handleSubmit never throws, every failed
submit (e.g. duplicate Kennzeichen) silently wiped user edits. Switched
all four Standorte/Fahrzeuge inline-edit and create forms to a plain
onSubmit handler that calls event.preventDefault() and builds FormData
from the submitted element manually, sidestepping React's action-reset
machinery.
Finding 2: web/app/standorte/actions.ts and web/app/fahrzeuge/actions.ts
each declared their own MutationResult type shadowing the differently
shaped MutationResult exported from lib/orte-fahrzeuge/mutations.ts.
Renamed the local type to ActionResult in both files to remove the
name collision.
This commit is contained in:
parent
c8be1c0c00
commit
6677679eb9
|
|
@ -5,9 +5,9 @@ import { kannStammdatenPflegen } from "@/lib/auth/roles";
|
|||
import { validateFahrzeug } from "@/lib/orte-fahrzeuge/validate-fahrzeug";
|
||||
import { insertFahrzeug, updateFahrzeugZeile } from "@/lib/orte-fahrzeuge/mutations";
|
||||
|
||||
export type MutationResult = { success: true } | { success: false; error: string };
|
||||
export type ActionResult = { success: true } | { success: false; error: string };
|
||||
|
||||
export async function createFahrzeug(formData: FormData): Promise<MutationResult> {
|
||||
export async function createFahrzeug(formData: FormData): Promise<ActionResult> {
|
||||
const profileResult = await loadProfile();
|
||||
if (profileResult.status !== "ok" || !kannStammdatenPflegen(profileResult.profile.role)) {
|
||||
return { success: false, error: "Nur Disponent/Admin duerfen Stammdaten pflegen." };
|
||||
|
|
@ -34,7 +34,7 @@ export async function createFahrzeug(formData: FormData): Promise<MutationResult
|
|||
return { success: true };
|
||||
}
|
||||
|
||||
export async function updateFahrzeug(fahrzeugId: string, formData: FormData): Promise<MutationResult> {
|
||||
export async function updateFahrzeug(fahrzeugId: string, formData: FormData): Promise<ActionResult> {
|
||||
const profileResult = await loadProfile();
|
||||
if (profileResult.status !== "ok" || !kannStammdatenPflegen(profileResult.profile.role)) {
|
||||
return { success: false, error: "Nur Disponent/Admin duerfen Stammdaten pflegen." };
|
||||
|
|
|
|||
|
|
@ -6,9 +6,9 @@ import { kannStammdatenPflegen } from "@/lib/auth/roles";
|
|||
import { validateOrt } from "@/lib/orte-fahrzeuge/validate-ort";
|
||||
import { insertOrt, updateOrtZeile } from "@/lib/orte-fahrzeuge/mutations";
|
||||
|
||||
export type MutationResult = { success: true } | { success: false; error: string };
|
||||
export type ActionResult = { success: true } | { success: false; error: string };
|
||||
|
||||
export async function createOrt(formData: FormData): Promise<MutationResult> {
|
||||
export async function createOrt(formData: FormData): Promise<ActionResult> {
|
||||
const profileResult = await loadProfile();
|
||||
if (profileResult.status !== "ok" || !kannStammdatenPflegen(profileResult.profile.role)) {
|
||||
return { success: false, error: "Nur Disponent/Admin duerfen Stammdaten pflegen." };
|
||||
|
|
@ -42,7 +42,7 @@ export async function createOrt(formData: FormData): Promise<MutationResult> {
|
|||
return { success: true };
|
||||
}
|
||||
|
||||
export async function updateOrt(ortId: string, formData: FormData): Promise<MutationResult> {
|
||||
export async function updateOrt(ortId: string, formData: FormData): Promise<ActionResult> {
|
||||
const profileResult = await loadProfile();
|
||||
if (profileResult.status !== "ok" || !kannStammdatenPflegen(profileResult.profile.role)) {
|
||||
return { success: false, error: "Nur Disponent/Admin duerfen Stammdaten pflegen." };
|
||||
|
|
|
|||
|
|
@ -15,7 +15,9 @@ export function FahrzeugZeile({ fahrzeug }: { fahrzeug: Fahrzeug }) {
|
|||
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 updateFahrzeug(fahrzeug.id, formData);
|
||||
if (!ergebnis.success) {
|
||||
|
|
@ -62,7 +64,7 @@ export function FahrzeugZeile({ fahrzeug }: { fahrzeug: Fahrzeug }) {
|
|||
return (
|
||||
<Card role="row" className="md:rounded-none md:border-0 md:border-b">
|
||||
<form
|
||||
action={handleSubmit}
|
||||
onSubmit={handleSubmit}
|
||||
className="flex flex-col gap-3 md:grid md:grid-cols-[1fr_1fr_1fr_auto_auto] md:items-end md:gap-4"
|
||||
>
|
||||
{fehler && (
|
||||
|
|
|
|||
|
|
@ -14,7 +14,9 @@ export function NeuerFahrzeug() {
|
|||
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 createFahrzeug(formData);
|
||||
if (!ergebnis.success) {
|
||||
|
|
@ -44,7 +46,7 @@ export function NeuerFahrzeug() {
|
|||
return (
|
||||
<Card role="row" className="md:rounded-none md:border-0 md:border-b">
|
||||
<form
|
||||
action={handleSubmit}
|
||||
onSubmit={handleSubmit}
|
||||
className="flex flex-col gap-3 md:grid md:grid-cols-[1fr_1fr_1fr_auto_auto] md:items-end md:gap-4"
|
||||
>
|
||||
{fehler && (
|
||||
|
|
|
|||
|
|
@ -14,7 +14,9 @@ export function NeuerStandort() {
|
|||
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 createOrt(formData);
|
||||
if (!ergebnis.success) {
|
||||
|
|
@ -44,7 +46,7 @@ export function NeuerStandort() {
|
|||
return (
|
||||
<Card role="row" className="md:rounded-none md:border-0 md:border-b">
|
||||
<form
|
||||
action={handleSubmit}
|
||||
onSubmit={handleSubmit}
|
||||
className="flex flex-col gap-3 md:grid md:grid-cols-[1fr_1fr_auto_auto] md:items-end md:gap-4"
|
||||
>
|
||||
{fehler && (
|
||||
|
|
|
|||
|
|
@ -15,7 +15,9 @@ export function StandortZeile({ ort }: { ort: Ort }) {
|
|||
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 updateOrt(ort.id, formData);
|
||||
if (!ergebnis.success) {
|
||||
|
|
@ -58,7 +60,7 @@ export function StandortZeile({ ort }: { ort: Ort }) {
|
|||
return (
|
||||
<Card role="row" className="md:rounded-none md:border-0 md:border-b">
|
||||
<form
|
||||
action={handleSubmit}
|
||||
onSubmit={handleSubmit}
|
||||
className="flex flex-col gap-3 md:grid md:grid-cols-[1fr_1fr_auto_auto] md:items-end md:gap-4"
|
||||
>
|
||||
{fehler && (
|
||||
|
|
|
|||
Loading…
Reference in New Issue