fix: upload image
This commit is contained in:
+117
-43
@@ -19,6 +19,7 @@ import {
|
||||
import { EditIcon, PlusIcon, TrashIcon } from "@/components/icons";
|
||||
import { formatDuration } from "@/lib/utils";
|
||||
import { MediaPreview } from "@/components/MediaPreview";
|
||||
import { ImagePicker } from "@/components/ImagePicker";
|
||||
import { pickUrl, SOUND_KEYS, VIDEO_KEYS, IMAGE_KEYS } from "@/lib/media";
|
||||
|
||||
interface Category {
|
||||
@@ -30,18 +31,58 @@ interface Media {
|
||||
id: number;
|
||||
title?: string;
|
||||
caption?: string;
|
||||
category_id?: number;
|
||||
category?: { id?: number; name?: string };
|
||||
// The API returns a `categories` array (many-to-many).
|
||||
categories?: Category[];
|
||||
subcategories?: Category[];
|
||||
subCategories?: Category[];
|
||||
type?: string;
|
||||
duration?: number;
|
||||
visibility?: string;
|
||||
is_premium?: boolean;
|
||||
external_url?: string | null;
|
||||
image_id?: number | null;
|
||||
}
|
||||
|
||||
// Compact multi-select rendered as toggle chips.
|
||||
function ChipMultiSelect({
|
||||
options,
|
||||
selected,
|
||||
onToggle,
|
||||
}: {
|
||||
options: Category[];
|
||||
selected: number[];
|
||||
onToggle: (id: number) => void;
|
||||
}) {
|
||||
if (!options.length)
|
||||
return <p className="text-xs text-muted">موردی برای انتخاب نیست.</p>;
|
||||
return (
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{options.map((o) => {
|
||||
const on = selected.includes(o.id);
|
||||
return (
|
||||
<button
|
||||
key={o.id}
|
||||
type="button"
|
||||
onClick={() => onToggle(o.id)}
|
||||
className={`rounded-full border px-3 py-1 text-xs transition ${
|
||||
on
|
||||
? "border-primary bg-primary text-white"
|
||||
: "border-border bg-surface text-foreground hover:bg-surface-muted"
|
||||
}`}
|
||||
>
|
||||
{o.name ?? `#${o.id}`}
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default function MediaPage() {
|
||||
const [search, setSearch] = useState("");
|
||||
const { data, loading, error, reload } = useList<Media>("/media", { search });
|
||||
const { data: categories } = useList<Category>("/categories");
|
||||
const { data: subCategories } = useList<Category>("/sub-categories");
|
||||
const toast = useToast();
|
||||
|
||||
const [editing, setEditing] = useState<Media | null>(null);
|
||||
@@ -50,12 +91,16 @@ export default function MediaPage() {
|
||||
|
||||
const [title, setTitle] = useState("");
|
||||
const [caption, setCaption] = useState("");
|
||||
const [categoryId, setCategoryId] = useState("");
|
||||
const [categoryIds, setCategoryIds] = useState<number[]>([]);
|
||||
const [subcategoryIds, setSubcategoryIds] = useState<number[]>([]);
|
||||
const [type, setType] = useState("audio");
|
||||
const [duration, setDuration] = useState("");
|
||||
const [visibility, setVisibility] = useState("public");
|
||||
const [isPremium, setIsPremium] = useState(false);
|
||||
const [file, setFile] = useState<File | null>(null);
|
||||
const [externalUrl, setExternalUrl] = useState("");
|
||||
const [imageId, setImageId] = useState<number | null>(null);
|
||||
const [imageFile, setImageFile] = useState<File | null>(null);
|
||||
|
||||
const [deleting, setDeleting] = useState<Media | null>(null);
|
||||
const [removing, setRemoving] = useState(false);
|
||||
@@ -63,12 +108,16 @@ export default function MediaPage() {
|
||||
function resetForm() {
|
||||
setTitle("");
|
||||
setCaption("");
|
||||
setCategoryId("");
|
||||
setCategoryIds([]);
|
||||
setSubcategoryIds([]);
|
||||
setType("audio");
|
||||
setDuration("");
|
||||
setVisibility("public");
|
||||
setIsPremium(false);
|
||||
setFile(null);
|
||||
setExternalUrl("");
|
||||
setImageId(null);
|
||||
setImageFile(null);
|
||||
}
|
||||
|
||||
function openCreate() {
|
||||
@@ -81,21 +130,25 @@ export default function MediaPage() {
|
||||
setEditing(row);
|
||||
setTitle(row.title ?? "");
|
||||
setCaption(row.caption ?? "");
|
||||
setCategoryId(
|
||||
row.category_id != null
|
||||
? String(row.category_id)
|
||||
: row.category?.id != null
|
||||
? String(row.category.id)
|
||||
: "",
|
||||
setCategoryIds((row.categories ?? []).map((c) => c.id));
|
||||
setSubcategoryIds(
|
||||
(row.subcategories ?? row.subCategories ?? []).map((c) => c.id),
|
||||
);
|
||||
setType(row.type ?? "audio");
|
||||
setDuration(row.duration != null ? String(row.duration) : "");
|
||||
setVisibility(row.visibility ?? "public");
|
||||
setIsPremium(!!row.is_premium);
|
||||
setFile(null);
|
||||
setExternalUrl(row.external_url ?? "");
|
||||
setImageId(row.image_id ?? null);
|
||||
setImageFile(null);
|
||||
setOpen(true);
|
||||
}
|
||||
|
||||
function toggleId(list: number[], set: (v: number[]) => void, id: number) {
|
||||
set(list.includes(id) ? list.filter((x) => x !== id) : [...list, id]);
|
||||
}
|
||||
|
||||
async function save(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
setSaving(true);
|
||||
@@ -103,12 +156,16 @@ export default function MediaPage() {
|
||||
const body = toFormData({
|
||||
title,
|
||||
caption,
|
||||
category_id: categoryId,
|
||||
category_ids: categoryIds,
|
||||
subcategory_ids: subcategoryIds,
|
||||
type,
|
||||
duration,
|
||||
visibility,
|
||||
is_premium: isPremium,
|
||||
file,
|
||||
external_url: externalUrl,
|
||||
image_id: imageId,
|
||||
image: imageFile,
|
||||
});
|
||||
if (editing) {
|
||||
// File-bearing update: POST /media/:id (multipart, file optional)
|
||||
@@ -165,9 +222,12 @@ export default function MediaPage() {
|
||||
},
|
||||
{ key: "title", header: "عنوان", render: (r) => r.title ?? "—" },
|
||||
{
|
||||
key: "category",
|
||||
key: "categories",
|
||||
header: "دستهبندی",
|
||||
render: (r) => r.category?.name ?? "—",
|
||||
render: (r) =>
|
||||
r.categories?.length
|
||||
? r.categories.map((c) => c.name).filter(Boolean).join("، ")
|
||||
: "—",
|
||||
},
|
||||
{
|
||||
key: "type",
|
||||
@@ -275,19 +335,30 @@ export default function MediaPage() {
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field label="دستهبندی" required>
|
||||
<Select
|
||||
value={categoryId}
|
||||
onChange={(e) => setCategoryId(e.target.value)}
|
||||
required
|
||||
>
|
||||
<option value="">انتخاب دستهبندی</option>
|
||||
{categories.map((c) => (
|
||||
<option key={c.id} value={c.id}>
|
||||
{c.name ?? `#${c.id}`}
|
||||
</option>
|
||||
))}
|
||||
</Select>
|
||||
<Field label="تصویر شاخص" hint="انتخاب از کتابخانه یا بارگذاری تصویر جدید">
|
||||
<ImagePicker
|
||||
imageId={imageId}
|
||||
onPickId={setImageId}
|
||||
file={imageFile}
|
||||
onPickFile={setImageFile}
|
||||
existingUrl={editing ? pickUrl(editing, IMAGE_KEYS) : null}
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field label="دستهبندیها">
|
||||
<ChipMultiSelect
|
||||
options={categories}
|
||||
selected={categoryIds}
|
||||
onToggle={(id) => toggleId(categoryIds, setCategoryIds, id)}
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field label="زیردستهها">
|
||||
<ChipMultiSelect
|
||||
options={subCategories}
|
||||
selected={subcategoryIds}
|
||||
onToggle={(id) => toggleId(subcategoryIds, setSubcategoryIds, id)}
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field label="نوع" required>
|
||||
@@ -297,6 +368,26 @@ export default function MediaPage() {
|
||||
</Select>
|
||||
</Field>
|
||||
|
||||
<Field
|
||||
label={editing ? "فایل (در صورت تغییر)" : "فایل"}
|
||||
hint="فایل صوتی/تصویری را بارگذاری کنید یا از «لینک خارجی» استفاده کنید."
|
||||
>
|
||||
<Input
|
||||
type="file"
|
||||
accept="audio/*,video/*"
|
||||
onChange={(e) => setFile(e.target.files?.[0] ?? null)}
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field label="لینک خارجی" hint="اگر فایل آپلود نمیکنید، آدرس مستقیم فایل را وارد کنید.">
|
||||
<Input
|
||||
dir="ltr"
|
||||
value={externalUrl}
|
||||
onChange={(e) => setExternalUrl(e.target.value)}
|
||||
placeholder="https://..."
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field label="مدت (ثانیه)">
|
||||
<Input
|
||||
type="number"
|
||||
@@ -318,23 +409,6 @@ export default function MediaPage() {
|
||||
</Select>
|
||||
</Field>
|
||||
|
||||
<Field
|
||||
label={editing ? "فایل (در صورت تغییر)" : "فایل"}
|
||||
hint={
|
||||
editing
|
||||
? "در صورت خالی بودن، فایل قبلی حفظ میشود."
|
||||
: undefined
|
||||
}
|
||||
required={!editing}
|
||||
>
|
||||
<Input
|
||||
type="file"
|
||||
accept="audio/*,video/*"
|
||||
onChange={(e) => setFile(e.target.files?.[0] ?? null)}
|
||||
required={!editing}
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Switch
|
||||
checked={isPremium}
|
||||
onChange={setIsPremium}
|
||||
|
||||
Reference in New Issue
Block a user