first commit
This commit is contained in:
106
app/(admin)/admin/players/PlayerForm.tsx
Normal file
106
app/(admin)/admin/players/PlayerForm.tsx
Normal file
@@ -0,0 +1,106 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
|
||||
type Country = { id: string; name: string };
|
||||
|
||||
export default function PlayerForm({
|
||||
countries,
|
||||
initial,
|
||||
playerId,
|
||||
}: {
|
||||
countries: Country[];
|
||||
initial?: { name: string; position: string; countryId: string; price: number };
|
||||
playerId?: string;
|
||||
}) {
|
||||
const router = useRouter();
|
||||
const [form, setForm] = useState({
|
||||
name: initial?.name ?? "",
|
||||
position: initial?.position ?? "FWD",
|
||||
countryId: initial?.countryId ?? "",
|
||||
price: initial?.price ?? 5.0,
|
||||
});
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState("");
|
||||
|
||||
async function handleSubmit(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
setLoading(true);
|
||||
const res = await fetch(playerId ? `/api/players/${playerId}` : "/api/players", {
|
||||
method: playerId ? "PUT" : "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(form),
|
||||
});
|
||||
if (res.ok) {
|
||||
router.push("/admin/players");
|
||||
router.refresh();
|
||||
} else {
|
||||
const d = await res.json();
|
||||
setError(d.error ?? "خطا");
|
||||
}
|
||||
setLoading(false);
|
||||
}
|
||||
|
||||
return (
|
||||
<form onSubmit={handleSubmit} className="bg-white rounded-2xl shadow p-6 flex flex-col gap-4">
|
||||
{error && <p className="text-red-500 text-sm">{error}</p>}
|
||||
<div>
|
||||
<label className="block text-sm font-medium mb-1">نام بازیکن</label>
|
||||
<input
|
||||
type="text"
|
||||
value={form.name}
|
||||
onChange={(e) => setForm({ ...form, name: e.target.value })}
|
||||
className="w-full border rounded-xl px-4 py-2.5 focus:outline-none focus:ring-2 focus:ring-green-500"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium mb-1">پست</label>
|
||||
<select
|
||||
value={form.position}
|
||||
onChange={(e) => setForm({ ...form, position: e.target.value })}
|
||||
className="w-full border rounded-xl px-4 py-2.5 focus:outline-none focus:ring-2 focus:ring-green-500"
|
||||
>
|
||||
<option value="GK">دروازهبان</option>
|
||||
<option value="DEF">مدافع</option>
|
||||
<option value="MID">هافبک</option>
|
||||
<option value="FWD">مهاجم</option>
|
||||
</select>
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium mb-1">تیم ملی</label>
|
||||
<select
|
||||
value={form.countryId}
|
||||
onChange={(e) => setForm({ ...form, countryId: e.target.value })}
|
||||
className="w-full border rounded-xl px-4 py-2.5 focus:outline-none focus:ring-2 focus:ring-green-500"
|
||||
required
|
||||
>
|
||||
<option value="">انتخاب کنید</option>
|
||||
{countries.map((c) => (
|
||||
<option key={c.id} value={c.id}>{c.name}</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium mb-1">قیمت (میلیون)</label>
|
||||
<input
|
||||
type="number"
|
||||
step="0.5"
|
||||
min="4"
|
||||
max="15"
|
||||
value={form.price}
|
||||
onChange={(e) => setForm({ ...form, price: parseFloat(e.target.value) })}
|
||||
className="w-full border rounded-xl px-4 py-2.5 focus:outline-none focus:ring-2 focus:ring-green-500"
|
||||
/>
|
||||
</div>
|
||||
<button
|
||||
type="submit"
|
||||
disabled={loading}
|
||||
className="bg-green-700 text-white py-3 rounded-xl font-bold hover:bg-green-800 transition disabled:opacity-50"
|
||||
>
|
||||
{loading ? "در حال ذخیره..." : playerId ? "ذخیره تغییرات" : "افزودن بازیکن"}
|
||||
</button>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user