Skip to content

Commit 43c0c69

Browse files
committed
Trigger form submit and use action prop to process data entered in a form.
1 parent 7d12110 commit 43c0c69

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

src/app/[lang]/edit/(crag)/[cragSlug]/sectors/components/edit-sectors/sector-dialog.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
FormEvent,
66
SetStateAction,
77
useEffect,
8+
useRef,
89
useState,
910
} from "react";
1011
import createSectorAction from "../../server-actions/create-sector-action";
@@ -56,6 +57,8 @@ function SectorDialog({
5657
}
5758
}, [sector, formType]);
5859

60+
const formRef = useRef<HTMLFormElement>(null);
61+
5962
const [nameError, setNameError] = useState("");
6063
const [loading, setLoading] = useState(false);
6164

@@ -70,7 +73,11 @@ function SectorDialog({
7073
setNameError("");
7174
};
7275

73-
const handleConfirm = async () => {
76+
const handleConfirm = () => {
77+
formRef.current?.requestSubmit();
78+
};
79+
80+
const handleFormAction = async () => {
7481
setLoading(true);
7582

7683
// validate form
@@ -115,11 +122,6 @@ function SectorDialog({
115122
router.refresh();
116123
};
117124

118-
const handleFormSubmit = (e: FormEvent<HTMLFormElement>) => {
119-
e.preventDefault();
120-
handleConfirm();
121-
};
122-
123125
return (
124126
<Dialog
125127
title={formType === "new" ? "Dodajanje sektorja" : "Urejanje sektorja"}
@@ -138,7 +140,7 @@ function SectorDialog({
138140
dontCloseOnConfirm: true,
139141
}}
140142
>
141-
<form onSubmit={handleFormSubmit}>
143+
<form action={handleFormAction} ref={formRef}>
142144
<TextField
143145
label="Ime sektorja"
144146
value={name}

src/app/[lang]/edit/(crag)/components/crag-form.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ function CragForm({ formType, countriesWithAreas, crag }: TCragFormProps) {
165165
setCragCoordinatesError("");
166166
};
167167

168-
const handleSaveClick = async () => {
168+
const handleFormAction = async () => {
169169
setLoading(true);
170170

171171
// Validate form
@@ -258,7 +258,7 @@ function CragForm({ formType, countriesWithAreas, crag }: TCragFormProps) {
258258
return (
259259
<div className="flex justify-center px-4 xs:px-8 mt-7">
260260
<div className="w-full max-w-2xl">
261-
<form onSubmit={(e) => e.preventDefault()}>
261+
<form action={handleFormAction}>
262262
{/* Main grid for inputs layout */}
263263
<div className="grid grid-cols-2 gap-x-4 gap-y-6">
264264
{/* Crag name */}
@@ -548,7 +548,7 @@ function CragForm({ formType, countriesWithAreas, crag }: TCragFormProps) {
548548
<Button variant="secondary" disabled={loading}>
549549
Prekliči
550550
</Button>
551-
<Button onClick={handleSaveClick} loading={loading}>
551+
<Button type="submit" loading={loading}>
552552
Shrani
553553
</Button>
554554
</div>

0 commit comments

Comments
 (0)