2025-11-09 13:09:22 +01:00
|
|
|
import React, {useState} from "react";
|
|
|
|
|
import { useNavigate } from "react-router";
|
|
|
|
|
import Alert from "~/components/ui/Alert";
|
|
|
|
|
import {updateDish} from "~/utils/api/dishApi";
|
|
|
|
|
import {DishType} from "~/types/DishType";
|
|
|
|
|
import useRoutes from "~/hooks/useRoutes";
|
|
|
|
|
import Spinner from "~/components/Spinner";
|
|
|
|
|
import Button from "~/components/ui/Button"
|
2025-10-13 14:57:11 +02:00
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
|
dish: DishType
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-09 13:09:22 +01:00
|
|
|
const EditDishForm = ({ dish }: Props) => {
|
2025-10-13 14:57:11 +02:00
|
|
|
const [name, setName] = useState<string>(dish.name);
|
|
|
|
|
const [error, setError] = useState<string>("");
|
2025-11-09 13:09:22 +01:00
|
|
|
const navigate = useNavigate()
|
2025-10-13 14:57:11 +02:00
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
|
const routes = useRoutes();
|
|
|
|
|
|
|
|
|
|
const validateForm = () => {
|
|
|
|
|
if (!name.trim()) {
|
|
|
|
|
setError("Dish name cannot be empty.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const submitForm = async (e: React.FormEvent<HTMLFormElement>) => {
|
|
|
|
|
e.preventDefault()
|
|
|
|
|
|
|
|
|
|
if (!validateForm()) return;
|
|
|
|
|
|
|
|
|
|
setError("");
|
|
|
|
|
setLoading(true);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const result = await updateDish(dish.id, name);
|
|
|
|
|
if (result) {
|
2025-11-09 13:09:22 +01:00
|
|
|
navigate(routes.dish.index())
|
2025-10-13 14:57:11 +02:00
|
|
|
}
|
|
|
|
|
} catch (error: unknown) {
|
|
|
|
|
setError(error instanceof Error ? error.message : "An unexpected error occurred");
|
|
|
|
|
} finally {
|
|
|
|
|
setLoading(false); // Reset loading state
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (loading) {
|
|
|
|
|
return <Spinner />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<form className="space-y-4" onSubmit={submitForm}>
|
|
|
|
|
{
|
|
|
|
|
error != '' && <Alert type="error" >{ error }</Alert>
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-13 20:22:47 +02:00
|
|
|
{/* Dish name input */}
|
|
|
|
|
<div>
|
|
|
|
|
<label htmlFor="name" className="block text-sm font-medium">Dish Name</label>
|
|
|
|
|
<input
|
|
|
|
|
type="text"
|
|
|
|
|
id="name"
|
|
|
|
|
name="name"
|
|
|
|
|
value={name}
|
|
|
|
|
onChange={(e) => setName(e.target.value)} // Update the name state on change
|
|
|
|
|
className="p-2 border rounded w-full bg-gray-500 border-secondary background-secondary"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2025-10-13 14:57:11 +02:00
|
|
|
|
2025-10-13 20:22:47 +02:00
|
|
|
{/* Save button */}
|
2025-10-13 14:57:11 +02:00
|
|
|
<Button
|
|
|
|
|
type="submit"
|
|
|
|
|
disabled={loading}
|
|
|
|
|
className={loading ? "bg-gray-400" : ''}
|
|
|
|
|
variant="primary"
|
|
|
|
|
appearance="solid"
|
|
|
|
|
>
|
|
|
|
|
{loading ? "Saving..." : "Save"}
|
|
|
|
|
</Button>
|
|
|
|
|
</form>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default EditDishForm;
|