Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aba556618b | ||
|
|
ec79ea2771 | ||
|
|
376e3e9473 | ||
|
|
01ad88adf9 | ||
|
|
638d1dad52 | ||
|
|
dec17b537e | ||
|
|
2a3bbd30fb | ||
|
|
e0c97d9158 | ||
|
|
8db8fbb143 | ||
|
|
4ac190f161 |
@@ -0,0 +1,316 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { apiFetch, ApiError, toFormData } from "@/lib/api";
|
||||
import { useList } from "@/lib/useResource";
|
||||
import { useToast } from "@/components/toast";
|
||||
import { DataTable, type Column } from "@/components/DataTable";
|
||||
import {
|
||||
Button,
|
||||
ConfirmDialog,
|
||||
Field,
|
||||
Input,
|
||||
Modal,
|
||||
PageHeader,
|
||||
Textarea,
|
||||
} from "@/components/ui";
|
||||
import { EditIcon, PlusIcon, TrashIcon } from "@/components/icons";
|
||||
import { toFa } from "@/lib/utils";
|
||||
import { MediaPreview } from "@/components/MediaPreview";
|
||||
import { ImagePicker } from "@/components/ImagePicker";
|
||||
import { pickUrl, IMAGE_KEYS } from "@/lib/media";
|
||||
|
||||
interface Announcement {
|
||||
id: number;
|
||||
title?: string;
|
||||
description?: string;
|
||||
link?: string;
|
||||
button_text?: string;
|
||||
start_date?: string | null;
|
||||
end_date?: string | null;
|
||||
image_id?: number | null;
|
||||
image?: unknown;
|
||||
}
|
||||
|
||||
// Datetime ISO string -> "YYYY-MM-DD" for a <input type="date">.
|
||||
function toDateInput(value?: string | null): string {
|
||||
return value ? value.slice(0, 10) : "";
|
||||
}
|
||||
|
||||
// "YYYY-MM-DD" Gregorian -> Persian display, or "—".
|
||||
function toFaDate(value?: string | null): string {
|
||||
if (!value) return "—";
|
||||
const d = value.slice(0, 10);
|
||||
try {
|
||||
return new Intl.DateTimeFormat("fa-IR").format(new Date(d));
|
||||
} catch {
|
||||
return d;
|
||||
}
|
||||
}
|
||||
|
||||
export default function AnnouncementsPage() {
|
||||
const { data, loading, error, reload } =
|
||||
useList<Announcement>("/announcements");
|
||||
const toast = useToast();
|
||||
|
||||
const [editing, setEditing] = useState<Announcement | null>(null);
|
||||
const [open, setOpen] = useState(false);
|
||||
const [title, setTitle] = useState("");
|
||||
const [description, setDescription] = useState("");
|
||||
const [link, setLink] = useState("");
|
||||
const [buttonText, setButtonText] = useState("");
|
||||
const [startDate, setStartDate] = useState("");
|
||||
const [endDate, setEndDate] = useState("");
|
||||
const [imageId, setImageId] = useState<number | null>(null);
|
||||
const [imageFile, setImageFile] = useState<File | null>(null);
|
||||
const [saving, setSaving] = useState(false);
|
||||
|
||||
const [deleting, setDeleting] = useState<Announcement | null>(null);
|
||||
const [removing, setRemoving] = useState(false);
|
||||
|
||||
function openCreate() {
|
||||
setEditing(null);
|
||||
setTitle("");
|
||||
setDescription("");
|
||||
setLink("");
|
||||
setButtonText("");
|
||||
setStartDate("");
|
||||
setEndDate("");
|
||||
setImageId(null);
|
||||
setImageFile(null);
|
||||
setOpen(true);
|
||||
}
|
||||
function openEdit(row: Announcement) {
|
||||
setEditing(row);
|
||||
setTitle(row.title ?? "");
|
||||
setDescription(row.description ?? "");
|
||||
setLink(row.link ?? "");
|
||||
setButtonText(row.button_text ?? "");
|
||||
setStartDate(toDateInput(row.start_date));
|
||||
setEndDate(toDateInput(row.end_date));
|
||||
setImageId(row.image_id ?? null);
|
||||
setImageFile(null);
|
||||
setOpen(true);
|
||||
}
|
||||
|
||||
async function save(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
setSaving(true);
|
||||
try {
|
||||
// Multipart (the image is a file). Edit posts to /announcements/:id,
|
||||
// which the backend also accepts as a multipart update.
|
||||
const body = toFormData({
|
||||
title,
|
||||
description,
|
||||
link,
|
||||
button_text: buttonText,
|
||||
start_date: startDate,
|
||||
end_date: endDate,
|
||||
image_id: imageId,
|
||||
image: imageFile,
|
||||
});
|
||||
if (editing) {
|
||||
await apiFetch(`/announcements/${editing.id}`, { method: "POST", body });
|
||||
toast.success("اعلان ویرایش شد.");
|
||||
} else {
|
||||
await apiFetch("/announcements", { method: "POST", body });
|
||||
toast.success("اعلان افزوده شد.");
|
||||
}
|
||||
setOpen(false);
|
||||
reload();
|
||||
} catch (err) {
|
||||
toast.error(err instanceof ApiError ? err.message : "خطا در ذخیرهسازی");
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function confirmDelete() {
|
||||
if (!deleting) return;
|
||||
setRemoving(true);
|
||||
try {
|
||||
await apiFetch(`/announcements/${deleting.id}`, { method: "DELETE" });
|
||||
toast.success("اعلان حذف شد.");
|
||||
setDeleting(null);
|
||||
reload();
|
||||
} catch (err) {
|
||||
toast.error(err instanceof ApiError ? err.message : "خطا در حذف");
|
||||
} finally {
|
||||
setRemoving(false);
|
||||
}
|
||||
}
|
||||
|
||||
const columns: Column<Announcement>[] = [
|
||||
{ key: "id", header: "شناسه", render: (r) => toFa(r.id), className: "w-20" },
|
||||
{
|
||||
key: "thumbnail",
|
||||
header: "تصویر",
|
||||
className: "w-20",
|
||||
render: (r) => (
|
||||
<MediaPreview kind="image" src={pickUrl(r, IMAGE_KEYS)} label={r.title} />
|
||||
),
|
||||
},
|
||||
{ key: "title", header: "عنوان", render: (r) => r.title ?? "—" },
|
||||
{
|
||||
key: "link",
|
||||
header: "لینک",
|
||||
render: (r) =>
|
||||
r.link ? (
|
||||
<span dir="ltr" className="block max-w-[12rem] truncate">
|
||||
{r.link}
|
||||
</span>
|
||||
) : (
|
||||
"—"
|
||||
),
|
||||
},
|
||||
{
|
||||
key: "start_date",
|
||||
header: "تاریخ شروع",
|
||||
render: (r) => toFaDate(r.start_date),
|
||||
className: "w-32",
|
||||
},
|
||||
{
|
||||
key: "end_date",
|
||||
header: "تاریخ پایان",
|
||||
render: (r) => toFaDate(r.end_date),
|
||||
className: "w-32",
|
||||
},
|
||||
];
|
||||
|
||||
return (
|
||||
<div>
|
||||
<PageHeader
|
||||
title="اعلانها"
|
||||
subtitle="مدیریت اعلانهای برنامه (تصویر، عنوان، توضیح، لینک و بازه زمانی)"
|
||||
action={
|
||||
<Button icon={<PlusIcon className="h-4 w-4" />} onClick={openCreate}>
|
||||
اعلان جدید
|
||||
</Button>
|
||||
}
|
||||
/>
|
||||
|
||||
<DataTable
|
||||
columns={columns}
|
||||
rows={data}
|
||||
loading={loading}
|
||||
error={error}
|
||||
onRetry={reload}
|
||||
emptyMessage="هنوز اعلانی ثبت نشده است."
|
||||
emptyAction={
|
||||
<Button icon={<PlusIcon className="h-4 w-4" />} onClick={openCreate}>
|
||||
اعلان جدید
|
||||
</Button>
|
||||
}
|
||||
actions={(row) => (
|
||||
<>
|
||||
<Button variant="ghost" onClick={() => openEdit(row)} aria-label="ویرایش">
|
||||
<EditIcon className="h-4 w-4" />
|
||||
</Button>
|
||||
<Button
|
||||
variant="ghost"
|
||||
onClick={() => setDeleting(row)}
|
||||
aria-label="حذف"
|
||||
className="text-danger"
|
||||
>
|
||||
<TrashIcon className="h-4 w-4" />
|
||||
</Button>
|
||||
</>
|
||||
)}
|
||||
/>
|
||||
|
||||
<Modal
|
||||
open={open}
|
||||
onClose={() => setOpen(false)}
|
||||
title={editing ? "ویرایش اعلان" : "اعلان جدید"}
|
||||
footer={
|
||||
<>
|
||||
<Button variant="secondary" onClick={() => setOpen(false)}>
|
||||
انصراف
|
||||
</Button>
|
||||
<Button form="announcement-form" type="submit" loading={saving}>
|
||||
ذخیره
|
||||
</Button>
|
||||
</>
|
||||
}
|
||||
>
|
||||
<form
|
||||
id="announcement-form"
|
||||
onSubmit={save}
|
||||
className="flex flex-col gap-4"
|
||||
>
|
||||
<Field label="عنوان" required>
|
||||
<Input
|
||||
value={title}
|
||||
onChange={(e) => setTitle(e.target.value)}
|
||||
placeholder="مثلاً تخفیف ویژه نوروز"
|
||||
required
|
||||
autoFocus
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field label="توضیحات">
|
||||
<Textarea
|
||||
value={description}
|
||||
onChange={(e) => setDescription(e.target.value)}
|
||||
placeholder="توضیح کوتاه درباره اعلان"
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field label="تصویر" hint="انتخاب از کتابخانه یا بارگذاری تصویر جدید">
|
||||
<ImagePicker
|
||||
imageId={imageId}
|
||||
onPickId={setImageId}
|
||||
file={imageFile}
|
||||
onPickFile={setImageFile}
|
||||
existingUrl={editing ? pickUrl(editing, IMAGE_KEYS) : null}
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field label="لینک">
|
||||
<Input
|
||||
value={link}
|
||||
onChange={(e) => setLink(e.target.value)}
|
||||
placeholder="https://"
|
||||
dir="ltr"
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field label="متن دکمه" hint="اختیاری">
|
||||
<Input
|
||||
value={buttonText}
|
||||
onChange={(e) => setButtonText(e.target.value)}
|
||||
placeholder="مثلاً مشاهده"
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
<Field label="تاریخ شروع">
|
||||
<Input
|
||||
type="date"
|
||||
value={startDate}
|
||||
onChange={(e) => setStartDate(e.target.value)}
|
||||
dir="ltr"
|
||||
/>
|
||||
</Field>
|
||||
<Field label="تاریخ پایان">
|
||||
<Input
|
||||
type="date"
|
||||
value={endDate}
|
||||
onChange={(e) => setEndDate(e.target.value)}
|
||||
dir="ltr"
|
||||
/>
|
||||
</Field>
|
||||
</div>
|
||||
</form>
|
||||
</Modal>
|
||||
|
||||
<ConfirmDialog
|
||||
open={!!deleting}
|
||||
message={`آیا از حذف «${deleting?.title}» مطمئن هستید؟`}
|
||||
loading={removing}
|
||||
onConfirm={confirmDelete}
|
||||
onClose={() => setDeleting(null)}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -129,7 +129,8 @@ export default function BreathingPage() {
|
||||
duration: numOrUndefined(form.duration),
|
||||
description: form.description,
|
||||
image_id: numOrUndefined(form.image_id),
|
||||
breathing_color_id: breathingColorId ?? undefined,
|
||||
// Send null (not undefined) so choosing "بدون رنگ" actually clears it.
|
||||
breathing_color_id: breathingColorId,
|
||||
};
|
||||
if (editing) {
|
||||
// Update is JSON on /breathing-templates/:id
|
||||
|
||||
@@ -26,6 +26,7 @@ interface Category {
|
||||
id: number;
|
||||
name: string;
|
||||
type: CategoryType;
|
||||
order?: number;
|
||||
description?: string | null;
|
||||
icon?: string | null;
|
||||
subcategories_count?: number;
|
||||
@@ -56,6 +57,7 @@ export default function CategoriesPage() {
|
||||
const [open, setOpen] = useState(false);
|
||||
const [name, setName] = useState("");
|
||||
const [type, setType] = useState<CategoryType>("media");
|
||||
const [order, setOrder] = useState("0");
|
||||
const [description, setDescription] = useState("");
|
||||
const [icon, setIcon] = useState<File | null>(null);
|
||||
const [saving, setSaving] = useState(false);
|
||||
@@ -67,6 +69,7 @@ export default function CategoriesPage() {
|
||||
setEditing(null);
|
||||
setName("");
|
||||
setType(typeFilter);
|
||||
setOrder("0");
|
||||
setDescription("");
|
||||
setIcon(null);
|
||||
setOpen(true);
|
||||
@@ -75,6 +78,7 @@ export default function CategoriesPage() {
|
||||
setEditing(row);
|
||||
setName(row.name ?? "");
|
||||
setType(row.type ?? "media");
|
||||
setOrder(String(row.order ?? 0));
|
||||
setDescription(row.description ?? "");
|
||||
setIcon(null);
|
||||
setOpen(true);
|
||||
@@ -84,12 +88,25 @@ export default function CategoriesPage() {
|
||||
e.preventDefault();
|
||||
setSaving(true);
|
||||
try {
|
||||
if (editing) {
|
||||
// Playlist categories live in the music_categories table, so their writes
|
||||
// go to /music-categories (image is the `image` field, not `icon`).
|
||||
const targetType = editing ? editing.type : type;
|
||||
if (targetType === "playlist") {
|
||||
const body = toFormData({ name, description, order, image: icon });
|
||||
if (editing) {
|
||||
await apiFetch(`/music-categories/${editing.id}`, { method: "POST", body });
|
||||
toast.success("دستهبندی ویرایش شد.");
|
||||
} else {
|
||||
await apiFetch("/music-categories", { method: "POST", body });
|
||||
toast.success("دستهبندی افزوده شد.");
|
||||
}
|
||||
} else if (editing) {
|
||||
// Icon is a file, so update goes through POST + Laravel method spoofing.
|
||||
const body = toFormData({
|
||||
_method: "PUT",
|
||||
name,
|
||||
type,
|
||||
order,
|
||||
description,
|
||||
icon,
|
||||
});
|
||||
@@ -98,7 +115,7 @@ export default function CategoriesPage() {
|
||||
} else {
|
||||
await apiFetch("/categories", {
|
||||
method: "POST",
|
||||
body: toFormData({ name, type, description, icon }),
|
||||
body: toFormData({ name, type, order, description, icon }),
|
||||
});
|
||||
toast.success("دستهبندی افزوده شد.");
|
||||
}
|
||||
@@ -115,7 +132,11 @@ export default function CategoriesPage() {
|
||||
if (!deleting) return;
|
||||
setRemoving(true);
|
||||
try {
|
||||
await apiFetch(`/categories/${deleting.id}`, { method: "DELETE" });
|
||||
const path =
|
||||
deleting.type === "playlist"
|
||||
? `/music-categories/${deleting.id}`
|
||||
: `/categories/${deleting.id}`;
|
||||
await apiFetch(path, { method: "DELETE" });
|
||||
toast.success("دستهبندی حذف شد.");
|
||||
setDeleting(null);
|
||||
reload();
|
||||
@@ -135,6 +156,12 @@ export default function CategoriesPage() {
|
||||
<MediaPreview kind="image" src={pickUrl(r, ICON_KEYS)} label={r.name} />
|
||||
),
|
||||
},
|
||||
{
|
||||
key: "order",
|
||||
header: "ترتیب",
|
||||
render: (r) => toFa(r.order ?? 0),
|
||||
className: "w-20",
|
||||
},
|
||||
{ key: "name", header: "نام دستهبندی" },
|
||||
{
|
||||
key: "type",
|
||||
@@ -249,6 +276,15 @@ export default function CategoriesPage() {
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field label="ترتیب" hint="عدد کوچکتر بالاتر نمایش داده میشود.">
|
||||
<Input
|
||||
type="number"
|
||||
value={order}
|
||||
onChange={(e) => setOrder(e.target.value)}
|
||||
dir="ltr"
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field label="توضیحات">
|
||||
<Textarea
|
||||
value={description}
|
||||
|
||||
@@ -6,6 +6,7 @@ import { useList } from "@/lib/useResource";
|
||||
import { useToast } from "@/components/toast";
|
||||
import { DataTable, type Column } from "@/components/DataTable";
|
||||
import {
|
||||
Badge,
|
||||
Button,
|
||||
ConfirmDialog,
|
||||
Field,
|
||||
@@ -13,6 +14,7 @@ import {
|
||||
Textarea,
|
||||
Modal,
|
||||
PageHeader,
|
||||
Switch,
|
||||
} from "@/components/ui";
|
||||
import { EditIcon, PlusIcon, TrashIcon } from "@/components/icons";
|
||||
import { ImagePicker } from "@/components/ImagePicker";
|
||||
@@ -28,6 +30,7 @@ interface Playlist {
|
||||
image?: unknown;
|
||||
detail_image_id?: number | null;
|
||||
detail_image?: unknown;
|
||||
is_premium?: boolean;
|
||||
}
|
||||
|
||||
interface MusicCategory {
|
||||
@@ -53,6 +56,7 @@ export default function MusicPlaylistsPage() {
|
||||
const [imageFile, setImageFile] = useState<File | null>(null);
|
||||
const [detailImageId, setDetailImageId] = useState<number | null>(null);
|
||||
const [detailImageFile, setDetailImageFile] = useState<File | null>(null);
|
||||
const [isPremium, setIsPremium] = useState(false);
|
||||
|
||||
const [deleting, setDeleting] = useState<Playlist | null>(null);
|
||||
const [removing, setRemoving] = useState(false);
|
||||
@@ -71,6 +75,7 @@ export default function MusicPlaylistsPage() {
|
||||
setImageFile(null);
|
||||
setDetailImageId(null);
|
||||
setDetailImageFile(null);
|
||||
setIsPremium(false);
|
||||
setOpen(true);
|
||||
}
|
||||
function openEdit(row: Playlist) {
|
||||
@@ -83,6 +88,7 @@ export default function MusicPlaylistsPage() {
|
||||
setImageFile(null);
|
||||
setDetailImageId(row.detail_image_id ?? null);
|
||||
setDetailImageFile(null);
|
||||
setIsPremium(row.is_premium ?? false);
|
||||
setOpen(true);
|
||||
}
|
||||
|
||||
@@ -101,6 +107,7 @@ export default function MusicPlaylistsPage() {
|
||||
image: imageFile,
|
||||
detail_image_id: detailImageId,
|
||||
detail_image: detailImageFile,
|
||||
is_premium: isPremium,
|
||||
}),
|
||||
});
|
||||
toast.success("پلیلیست ویرایش شد.");
|
||||
@@ -117,6 +124,7 @@ export default function MusicPlaylistsPage() {
|
||||
image: imageFile,
|
||||
detail_image_id: detailImageId,
|
||||
detail_image: detailImageFile,
|
||||
is_premium: isPremium,
|
||||
}),
|
||||
});
|
||||
toast.success("پلیلیست افزوده شد.");
|
||||
@@ -161,6 +169,17 @@ export default function MusicPlaylistsPage() {
|
||||
header: "توضیحات",
|
||||
render: (r) => r.description ?? "—",
|
||||
},
|
||||
{
|
||||
key: "is_premium",
|
||||
header: "ویژه",
|
||||
className: "w-20",
|
||||
render: (r) =>
|
||||
r.is_premium ? (
|
||||
<Badge tone="primary">ویژه</Badge>
|
||||
) : (
|
||||
<Badge tone="neutral">رایگان</Badge>
|
||||
),
|
||||
},
|
||||
];
|
||||
|
||||
return (
|
||||
@@ -257,6 +276,8 @@ export default function MusicPlaylistsPage() {
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Switch checked={isPremium} onChange={setIsPremium} label="ویژه (پرمیوم)" />
|
||||
|
||||
{!editing && (
|
||||
<>
|
||||
<Field label="دستهبندیها">
|
||||
|
||||
@@ -16,10 +16,10 @@ import {
|
||||
Textarea,
|
||||
} from "@/components/ui";
|
||||
import { EditIcon, PlusIcon, TrashIcon } from "@/components/icons";
|
||||
import { toFa } from "@/lib/utils";
|
||||
import { MediaPreview } from "@/components/MediaPreview";
|
||||
import { ImagePicker } from "@/components/ImagePicker";
|
||||
import { MediaPreview } from "@/components/MediaPreview";
|
||||
import { pickUrl, IMAGE_KEYS } from "@/lib/media";
|
||||
import { toFa } from "@/lib/utils";
|
||||
|
||||
interface SliderAction {
|
||||
path?: string;
|
||||
@@ -37,7 +37,7 @@ interface Slider {
|
||||
}
|
||||
|
||||
const ACTION_TYPES = [
|
||||
{ value: "screen", label: "صفحه (screen)" },
|
||||
{ value: "navigation", label: "صفحه (navigation)" },
|
||||
{ value: "link", label: "لینک (link)" },
|
||||
];
|
||||
|
||||
@@ -51,7 +51,7 @@ export default function SlidersPage() {
|
||||
const [description, setDescription] = useState("");
|
||||
const [url, setUrl] = useState("");
|
||||
const [actionPath, setActionPath] = useState("");
|
||||
const [actionType, setActionType] = useState("screen");
|
||||
const [actionType, setActionType] = useState("navigation");
|
||||
const [imageId, setImageId] = useState<number | null>(null);
|
||||
const [imageFile, setImageFile] = useState<File | null>(null);
|
||||
const [saving, setSaving] = useState(false);
|
||||
@@ -65,7 +65,7 @@ export default function SlidersPage() {
|
||||
setDescription("");
|
||||
setUrl("");
|
||||
setActionPath("");
|
||||
setActionType("screen");
|
||||
setActionType("navigation");
|
||||
setImageId(null);
|
||||
setImageFile(null);
|
||||
setOpen(true);
|
||||
@@ -76,7 +76,7 @@ export default function SlidersPage() {
|
||||
setDescription(row.description ?? "");
|
||||
setUrl(row.url ?? "");
|
||||
setActionPath(row.action?.path ?? "");
|
||||
setActionType(row.action?.type ?? "screen");
|
||||
setActionType(row.action?.type ?? "navigation");
|
||||
setImageId(row.image_id ?? null);
|
||||
setImageFile(null);
|
||||
setOpen(true);
|
||||
@@ -86,9 +86,8 @@ export default function SlidersPage() {
|
||||
e.preventDefault();
|
||||
setSaving(true);
|
||||
try {
|
||||
// Both create and edit go through multipart (the image is a file). The
|
||||
// nested action uses literal bracket keys; edit posts to /slider/:id,
|
||||
// which the backend also accepts as a multipart update.
|
||||
// Multipart for both (so an image file can be uploaded). Bracket keys
|
||||
// build the nested action object; update uses POST /slider/:id.
|
||||
const body = toFormData({
|
||||
title,
|
||||
description,
|
||||
@@ -102,6 +101,7 @@ export default function SlidersPage() {
|
||||
await apiFetch(`/slider/${editing.id}`, { method: "POST", body });
|
||||
toast.success("اسلایدر ویرایش شد.");
|
||||
} else {
|
||||
await apiFetch("/slider", { method: "POST", body });
|
||||
await apiFetch("/slider", { method: "POST", body });
|
||||
toast.success("اسلایدر افزوده شد.");
|
||||
}
|
||||
@@ -132,7 +132,7 @@ export default function SlidersPage() {
|
||||
const columns: Column<Slider>[] = [
|
||||
{ key: "id", header: "شناسه", render: (r) => toFa(r.id), className: "w-24" },
|
||||
{
|
||||
key: "thumbnail",
|
||||
key: "image",
|
||||
header: "تصویر",
|
||||
className: "w-20",
|
||||
render: (r) => (
|
||||
@@ -236,6 +236,16 @@ export default function SlidersPage() {
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field label="تصویر">
|
||||
<ImagePicker
|
||||
imageId={imageId}
|
||||
onPickId={setImageId}
|
||||
file={imageFile}
|
||||
onPickFile={setImageFile}
|
||||
existingUrl={editing ? pickUrl(editing, IMAGE_KEYS) : null}
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field label="توضیحات">
|
||||
<Textarea
|
||||
value={description}
|
||||
|
||||
@@ -0,0 +1,302 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { apiFetch, ApiError, toFormData } from "@/lib/api";
|
||||
import { useList } from "@/lib/useResource";
|
||||
import { useToast } from "@/components/toast";
|
||||
import { DataTable, type Column } from "@/components/DataTable";
|
||||
import {
|
||||
Button,
|
||||
ConfirmDialog,
|
||||
Field,
|
||||
Input,
|
||||
Modal,
|
||||
PageHeader,
|
||||
Textarea,
|
||||
} from "@/components/ui";
|
||||
import { EditIcon, PlusIcon, TrashIcon } from "@/components/icons";
|
||||
import { toFa } from "@/lib/utils";
|
||||
import { MediaPreview } from "@/components/MediaPreview";
|
||||
import { ImagePicker } from "@/components/ImagePicker";
|
||||
import { pickUrl, IMAGE_KEYS } from "@/lib/media";
|
||||
|
||||
interface AppVersion {
|
||||
id: number;
|
||||
title?: string;
|
||||
description?: string;
|
||||
version_name?: string;
|
||||
version_code?: number;
|
||||
link?: string;
|
||||
button_text?: string;
|
||||
image_id?: number | null;
|
||||
image?: unknown;
|
||||
}
|
||||
|
||||
export default function VersionsPage() {
|
||||
const { data, loading, error, reload } = useList<AppVersion>("/versions");
|
||||
const toast = useToast();
|
||||
|
||||
const [editing, setEditing] = useState<AppVersion | null>(null);
|
||||
const [open, setOpen] = useState(false);
|
||||
const [title, setTitle] = useState("");
|
||||
const [description, setDescription] = useState("");
|
||||
const [versionName, setVersionName] = useState("");
|
||||
const [versionCode, setVersionCode] = useState("");
|
||||
const [link, setLink] = useState("");
|
||||
const [buttonText, setButtonText] = useState("");
|
||||
const [imageId, setImageId] = useState<number | null>(null);
|
||||
const [imageFile, setImageFile] = useState<File | null>(null);
|
||||
const [saving, setSaving] = useState(false);
|
||||
|
||||
const [deleting, setDeleting] = useState<AppVersion | null>(null);
|
||||
const [removing, setRemoving] = useState(false);
|
||||
|
||||
function openCreate() {
|
||||
setEditing(null);
|
||||
setTitle("");
|
||||
setDescription("");
|
||||
setVersionName("");
|
||||
setVersionCode("");
|
||||
setLink("");
|
||||
setButtonText("");
|
||||
setImageId(null);
|
||||
setImageFile(null);
|
||||
setOpen(true);
|
||||
}
|
||||
function openEdit(row: AppVersion) {
|
||||
setEditing(row);
|
||||
setTitle(row.title ?? "");
|
||||
setDescription(row.description ?? "");
|
||||
setVersionName(row.version_name ?? "");
|
||||
setVersionCode(row.version_code != null ? String(row.version_code) : "");
|
||||
setLink(row.link ?? "");
|
||||
setButtonText(row.button_text ?? "");
|
||||
setImageId(row.image_id ?? null);
|
||||
setImageFile(null);
|
||||
setOpen(true);
|
||||
}
|
||||
|
||||
async function save(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
setSaving(true);
|
||||
try {
|
||||
// Multipart (the image is a file). Edit posts to /versions/:id, which the
|
||||
// backend also accepts as a multipart update.
|
||||
const body = toFormData({
|
||||
title,
|
||||
description,
|
||||
version_name: versionName,
|
||||
version_code: versionCode,
|
||||
link,
|
||||
button_text: buttonText,
|
||||
image_id: imageId,
|
||||
image: imageFile,
|
||||
});
|
||||
if (editing) {
|
||||
await apiFetch(`/versions/${editing.id}`, { method: "POST", body });
|
||||
toast.success("نسخه ویرایش شد.");
|
||||
} else {
|
||||
await apiFetch("/versions", { method: "POST", body });
|
||||
toast.success("نسخه افزوده شد.");
|
||||
}
|
||||
setOpen(false);
|
||||
reload();
|
||||
} catch (err) {
|
||||
toast.error(err instanceof ApiError ? err.message : "خطا در ذخیرهسازی");
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function confirmDelete() {
|
||||
if (!deleting) return;
|
||||
setRemoving(true);
|
||||
try {
|
||||
await apiFetch(`/versions/${deleting.id}`, { method: "DELETE" });
|
||||
toast.success("نسخه حذف شد.");
|
||||
setDeleting(null);
|
||||
reload();
|
||||
} catch (err) {
|
||||
toast.error(err instanceof ApiError ? err.message : "خطا در حذف");
|
||||
} finally {
|
||||
setRemoving(false);
|
||||
}
|
||||
}
|
||||
|
||||
const columns: Column<AppVersion>[] = [
|
||||
{ key: "id", header: "شناسه", render: (r) => toFa(r.id), className: "w-20" },
|
||||
{
|
||||
key: "thumbnail",
|
||||
header: "تصویر",
|
||||
className: "w-20",
|
||||
render: (r) => (
|
||||
<MediaPreview kind="image" src={pickUrl(r, IMAGE_KEYS)} label={r.title} />
|
||||
),
|
||||
},
|
||||
{ key: "title", header: "عنوان", render: (r) => r.title ?? "—" },
|
||||
{
|
||||
key: "version_name",
|
||||
header: "نام نسخه",
|
||||
render: (r) => (
|
||||
<span dir="ltr" className="block">
|
||||
{r.version_name ?? "—"}
|
||||
</span>
|
||||
),
|
||||
className: "w-28",
|
||||
},
|
||||
{
|
||||
key: "version_code",
|
||||
header: "کد نسخه",
|
||||
render: (r) => (r.version_code != null ? toFa(r.version_code) : "—"),
|
||||
className: "w-24",
|
||||
},
|
||||
{
|
||||
key: "link",
|
||||
header: "لینک",
|
||||
render: (r) =>
|
||||
r.link ? (
|
||||
<span dir="ltr" className="block max-w-[12rem] truncate">
|
||||
{r.link}
|
||||
</span>
|
||||
) : (
|
||||
"—"
|
||||
),
|
||||
},
|
||||
];
|
||||
|
||||
return (
|
||||
<div>
|
||||
<PageHeader
|
||||
title="نسخهها"
|
||||
subtitle="مدیریت نسخههای برنامه (بررسی بهروزرسانی)"
|
||||
action={
|
||||
<Button icon={<PlusIcon className="h-4 w-4" />} onClick={openCreate}>
|
||||
نسخه جدید
|
||||
</Button>
|
||||
}
|
||||
/>
|
||||
|
||||
<DataTable
|
||||
columns={columns}
|
||||
rows={data}
|
||||
loading={loading}
|
||||
error={error}
|
||||
onRetry={reload}
|
||||
emptyMessage="هنوز نسخهای ثبت نشده است."
|
||||
emptyAction={
|
||||
<Button icon={<PlusIcon className="h-4 w-4" />} onClick={openCreate}>
|
||||
نسخه جدید
|
||||
</Button>
|
||||
}
|
||||
actions={(row) => (
|
||||
<>
|
||||
<Button variant="ghost" onClick={() => openEdit(row)} aria-label="ویرایش">
|
||||
<EditIcon className="h-4 w-4" />
|
||||
</Button>
|
||||
<Button
|
||||
variant="ghost"
|
||||
onClick={() => setDeleting(row)}
|
||||
aria-label="حذف"
|
||||
className="text-danger"
|
||||
>
|
||||
<TrashIcon className="h-4 w-4" />
|
||||
</Button>
|
||||
</>
|
||||
)}
|
||||
/>
|
||||
|
||||
<Modal
|
||||
open={open}
|
||||
onClose={() => setOpen(false)}
|
||||
title={editing ? "ویرایش نسخه" : "نسخه جدید"}
|
||||
footer={
|
||||
<>
|
||||
<Button variant="secondary" onClick={() => setOpen(false)}>
|
||||
انصراف
|
||||
</Button>
|
||||
<Button form="version-form" type="submit" loading={saving}>
|
||||
ذخیره
|
||||
</Button>
|
||||
</>
|
||||
}
|
||||
>
|
||||
<form id="version-form" onSubmit={save} className="flex flex-col gap-4">
|
||||
<Field label="عنوان" required>
|
||||
<Input
|
||||
value={title}
|
||||
onChange={(e) => setTitle(e.target.value)}
|
||||
placeholder="مثلاً نسخه ۱.۲.۰"
|
||||
required
|
||||
autoFocus
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field label="توضیحات">
|
||||
<Textarea
|
||||
value={description}
|
||||
onChange={(e) => setDescription(e.target.value)}
|
||||
placeholder="توضیح تغییرات این نسخه"
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field label="تصویر" hint="انتخاب از کتابخانه یا بارگذاری تصویر جدید">
|
||||
<ImagePicker
|
||||
imageId={imageId}
|
||||
onPickId={setImageId}
|
||||
file={imageFile}
|
||||
onPickFile={setImageFile}
|
||||
existingUrl={editing ? pickUrl(editing, IMAGE_KEYS) : null}
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
<Field label="نام نسخه" required>
|
||||
<Input
|
||||
value={versionName}
|
||||
onChange={(e) => setVersionName(e.target.value)}
|
||||
placeholder="1.2.0"
|
||||
dir="ltr"
|
||||
required
|
||||
/>
|
||||
</Field>
|
||||
<Field label="کد نسخه" required>
|
||||
<Input
|
||||
type="number"
|
||||
value={versionCode}
|
||||
onChange={(e) => setVersionCode(e.target.value)}
|
||||
placeholder="42"
|
||||
dir="ltr"
|
||||
required
|
||||
/>
|
||||
</Field>
|
||||
</div>
|
||||
|
||||
<Field label="لینک">
|
||||
<Input
|
||||
value={link}
|
||||
onChange={(e) => setLink(e.target.value)}
|
||||
placeholder="https://"
|
||||
dir="ltr"
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field label="متن دکمه" hint="اختیاری">
|
||||
<Input
|
||||
value={buttonText}
|
||||
onChange={(e) => setButtonText(e.target.value)}
|
||||
placeholder="مثلاً بهروزرسانی"
|
||||
/>
|
||||
</Field>
|
||||
</form>
|
||||
</Modal>
|
||||
|
||||
<ConfirmDialog
|
||||
open={!!deleting}
|
||||
message={`آیا از حذف «${deleting?.title}» مطمئن هستید؟`}
|
||||
loading={removing}
|
||||
onConfirm={confirmDelete}
|
||||
onClose={() => setDeleting(null)}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
+10
@@ -80,6 +80,16 @@ export const NAV: NavSection[] = [
|
||||
icon: SliderIcon,
|
||||
items: [{ href: "/dashboard/sliders", label: "اسلایدرها" }],
|
||||
},
|
||||
{
|
||||
label: "اعلانها",
|
||||
icon: SliderIcon,
|
||||
items: [{ href: "/dashboard/announcements", label: "اعلانها" }],
|
||||
},
|
||||
{
|
||||
label: "نسخهها",
|
||||
icon: SliderIcon,
|
||||
items: [{ href: "/dashboard/versions", label: "نسخههای برنامه" }],
|
||||
},
|
||||
{
|
||||
label: "تصاویر",
|
||||
icon: ImageIcon,
|
||||
|
||||
Reference in New Issue
Block a user