admin
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
import { useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import Image from "next/image";
|
||||
|
||||
type Country = { id: string; name: string };
|
||||
|
||||
@@ -11,7 +12,7 @@ export default function PlayerForm({
|
||||
playerId,
|
||||
}: {
|
||||
countries: Country[];
|
||||
initial?: { name: string; position: string; countryId: string; price: number };
|
||||
initial?: { name: string; position: string; countryId: string; price: number; image?: string | null };
|
||||
playerId?: string;
|
||||
}) {
|
||||
const router = useRouter();
|
||||
@@ -20,10 +21,42 @@ export default function PlayerForm({
|
||||
position: initial?.position ?? "FWD",
|
||||
countryId: initial?.countryId ?? "",
|
||||
price: initial?.price ?? 5.0,
|
||||
image: initial?.image ?? "",
|
||||
});
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [uploading, setUploading] = useState(false);
|
||||
const [error, setError] = useState("");
|
||||
|
||||
async function handleImageUpload(e: React.ChangeEvent<HTMLInputElement>) {
|
||||
const file = e.target.files?.[0];
|
||||
if (!file) return;
|
||||
|
||||
setUploading(true);
|
||||
setError("");
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append("file", file);
|
||||
|
||||
try {
|
||||
const res = await fetch("/api/upload/player-image", {
|
||||
method: "POST",
|
||||
body: formData,
|
||||
});
|
||||
|
||||
if (res.ok) {
|
||||
const data = await res.json();
|
||||
setForm({ ...form, image: data.fileName });
|
||||
} else {
|
||||
const data = await res.json();
|
||||
setError(data.error ?? "خطا در آپلود تصویر");
|
||||
}
|
||||
} catch (err) {
|
||||
setError("خطا در آپلود تصویر");
|
||||
} finally {
|
||||
setUploading(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleSubmit(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
setLoading(true);
|
||||
@@ -45,6 +78,33 @@ export default function PlayerForm({
|
||||
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>
|
||||
<div className="flex items-center gap-4">
|
||||
{form.image && (
|
||||
<div className="relative w-24 h-24 rounded-xl overflow-hidden border">
|
||||
<Image
|
||||
src={`/uploads/players/${form.image}`}
|
||||
alt={form.name}
|
||||
fill
|
||||
className="object-cover"
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
<div className="flex-1">
|
||||
<input
|
||||
type="file"
|
||||
accept="image/*"
|
||||
onChange={handleImageUpload}
|
||||
disabled={uploading}
|
||||
className="w-full border rounded-xl px-4 py-2.5 focus:outline-none focus:ring-2 focus:ring-green-500"
|
||||
/>
|
||||
{uploading && <p className="text-sm text-gray-500 mt-1">در حال آپلود...</p>}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-sm font-medium mb-1">نام بازیکن</label>
|
||||
<input
|
||||
|
||||
Reference in New Issue
Block a user