feat: add theme update
This commit is contained in:
@@ -17,6 +17,9 @@ import {
|
||||
} 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 SliderAction {
|
||||
path?: string;
|
||||
@@ -29,6 +32,8 @@ interface Slider {
|
||||
description?: string;
|
||||
url?: string;
|
||||
action?: SliderAction;
|
||||
image_id?: number | null;
|
||||
image?: unknown;
|
||||
}
|
||||
|
||||
const ACTION_TYPES = [
|
||||
@@ -47,6 +52,8 @@ export default function SlidersPage() {
|
||||
const [url, setUrl] = useState("");
|
||||
const [actionPath, setActionPath] = useState("");
|
||||
const [actionType, setActionType] = useState("screen");
|
||||
const [imageId, setImageId] = useState<number | null>(null);
|
||||
const [imageFile, setImageFile] = useState<File | null>(null);
|
||||
const [saving, setSaving] = useState(false);
|
||||
|
||||
const [deleting, setDeleting] = useState<Slider | null>(null);
|
||||
@@ -59,6 +66,8 @@ export default function SlidersPage() {
|
||||
setUrl("");
|
||||
setActionPath("");
|
||||
setActionType("screen");
|
||||
setImageId(null);
|
||||
setImageFile(null);
|
||||
setOpen(true);
|
||||
}
|
||||
function openEdit(row: Slider) {
|
||||
@@ -68,6 +77,8 @@ export default function SlidersPage() {
|
||||
setUrl(row.url ?? "");
|
||||
setActionPath(row.action?.path ?? "");
|
||||
setActionType(row.action?.type ?? "screen");
|
||||
setImageId(row.image_id ?? null);
|
||||
setImageFile(null);
|
||||
setOpen(true);
|
||||
}
|
||||
|
||||
@@ -75,30 +86,23 @@ 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.
|
||||
const body = toFormData({
|
||||
title,
|
||||
description,
|
||||
url,
|
||||
"action[path]": actionPath,
|
||||
"action[type]": actionType,
|
||||
image_id: imageId,
|
||||
image: imageFile,
|
||||
});
|
||||
if (editing) {
|
||||
// Update is JSON on /slider/:id with a nested action object.
|
||||
await apiFetch(`/slider/${editing.id}`, {
|
||||
method: "PUT",
|
||||
body: {
|
||||
title,
|
||||
description,
|
||||
url,
|
||||
action: { path: actionPath, type: actionType },
|
||||
},
|
||||
});
|
||||
await apiFetch(`/slider/${editing.id}`, { method: "POST", body });
|
||||
toast.success("اسلایدر ویرایش شد.");
|
||||
} else {
|
||||
// Create is multipart with literal bracket keys for the nested action.
|
||||
await apiFetch("/slider", {
|
||||
method: "POST",
|
||||
body: toFormData({
|
||||
title,
|
||||
description,
|
||||
"action[path]": actionPath,
|
||||
"action[type]": actionType,
|
||||
url,
|
||||
}),
|
||||
});
|
||||
await apiFetch("/slider", { method: "POST", body });
|
||||
toast.success("اسلایدر افزوده شد.");
|
||||
}
|
||||
setOpen(false);
|
||||
@@ -127,6 +131,14 @@ export default function SlidersPage() {
|
||||
|
||||
const columns: Column<Slider>[] = [
|
||||
{ key: "id", header: "شناسه", render: (r) => toFa(r.id), className: "w-24" },
|
||||
{
|
||||
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: "description",
|
||||
@@ -232,6 +244,16 @@ export default function SlidersPage() {
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field label="تصویر" hint="انتخاب از کتابخانه یا بارگذاری تصویر جدید">
|
||||
<ImagePicker
|
||||
imageId={imageId}
|
||||
onPickId={setImageId}
|
||||
file={imageFile}
|
||||
onPickFile={setImageFile}
|
||||
existingUrl={editing ? pickUrl(editing, IMAGE_KEYS) : null}
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field label="آدرس (URL)">
|
||||
<Input
|
||||
value={url}
|
||||
|
||||
@@ -0,0 +1,259 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { apiFetch, ApiError } from "@/lib/api";
|
||||
import { useList } from "@/lib/useResource";
|
||||
import { useToast } from "@/components/toast";
|
||||
import { DataTable, type Column } from "@/components/DataTable";
|
||||
import {
|
||||
Badge,
|
||||
Button,
|
||||
Field,
|
||||
Input,
|
||||
Switch,
|
||||
Modal,
|
||||
PageHeader,
|
||||
} from "@/components/ui";
|
||||
import { EditIcon } from "@/components/icons";
|
||||
import { toFa } from "@/lib/utils";
|
||||
|
||||
interface Theme {
|
||||
id: number;
|
||||
key: string;
|
||||
name: string;
|
||||
colors: Record<string, string>;
|
||||
order?: number;
|
||||
is_active?: boolean;
|
||||
}
|
||||
|
||||
// "#FF156395" (ARGB) or "#156395" → a CSS color the browser understands.
|
||||
function cssColor(c?: string): string {
|
||||
if (!c) return "transparent";
|
||||
const hex = c.replace("#", "");
|
||||
if (hex.length === 8) return `#${hex.slice(2)}${hex.slice(0, 2)}`; // ARGB → RGBA
|
||||
return `#${hex}`;
|
||||
}
|
||||
|
||||
// The RGB part for a native <input type="color"> (which can't represent alpha).
|
||||
function toNativeRgb(argb?: string): string {
|
||||
const hex = (argb ?? "").replace("#", "");
|
||||
if (hex.length === 8) return `#${hex.slice(2)}`;
|
||||
if (hex.length === 6) return `#${hex}`;
|
||||
return "#000000";
|
||||
}
|
||||
|
||||
// Editable color cell: a native picker for the RGB part (preserving any alpha)
|
||||
// plus a free-text field for full ARGB control.
|
||||
function ColorInput({
|
||||
value,
|
||||
onChange,
|
||||
}: {
|
||||
value: string;
|
||||
onChange: (v: string) => void;
|
||||
}) {
|
||||
const hex = (value ?? "").replace("#", "");
|
||||
const alpha = hex.length === 8 ? hex.slice(0, 2) : "";
|
||||
return (
|
||||
<div className="flex items-center gap-2">
|
||||
<input
|
||||
type="color"
|
||||
aria-label="انتخاب رنگ"
|
||||
value={toNativeRgb(value)}
|
||||
onChange={(e) =>
|
||||
onChange(`#${alpha}${e.target.value.replace("#", "")}`.toUpperCase())
|
||||
}
|
||||
className="h-9 w-9 shrink-0 cursor-pointer rounded border border-border bg-transparent p-0.5"
|
||||
/>
|
||||
<Input
|
||||
value={value}
|
||||
onChange={(e) => onChange(e.target.value)}
|
||||
dir="ltr"
|
||||
className="font-mono"
|
||||
placeholder="#FFFFFFFF"
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default function ThemesPage() {
|
||||
// include_inactive so the editor also lists themes that are turned off.
|
||||
const { data, loading, error, reload } = useList<Theme>("/themes", {
|
||||
include_inactive: 1,
|
||||
});
|
||||
const toast = useToast();
|
||||
|
||||
const [editing, setEditing] = useState<Theme | null>(null);
|
||||
const [open, setOpen] = useState(false);
|
||||
const [saving, setSaving] = useState(false);
|
||||
|
||||
const [name, setName] = useState("");
|
||||
const [order, setOrder] = useState("");
|
||||
const [isActive, setIsActive] = useState(true);
|
||||
const [colors, setColors] = useState<Record<string, string>>({});
|
||||
|
||||
function openEdit(row: Theme) {
|
||||
setEditing(row);
|
||||
setName(row.name ?? "");
|
||||
setOrder(row.order != null ? String(row.order) : "");
|
||||
setIsActive(row.is_active ?? true);
|
||||
// Clone so edits don't mutate the loaded row before saving.
|
||||
setColors({ ...(row.colors ?? {}) });
|
||||
setOpen(true);
|
||||
}
|
||||
|
||||
function setColor(key: string, value: string) {
|
||||
setColors((prev) => ({ ...prev, [key]: value }));
|
||||
}
|
||||
|
||||
async function save(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
if (!editing) return;
|
||||
setSaving(true);
|
||||
try {
|
||||
await apiFetch(`/themes/${editing.id}`, {
|
||||
method: "PUT",
|
||||
body: {
|
||||
name,
|
||||
order: order === "" ? undefined : Number(order),
|
||||
is_active: isActive,
|
||||
colors,
|
||||
},
|
||||
});
|
||||
toast.success("تم ویرایش شد.");
|
||||
setOpen(false);
|
||||
reload();
|
||||
} catch (err) {
|
||||
toast.error(err instanceof ApiError ? err.message : "خطا در ذخیرهسازی");
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
}
|
||||
|
||||
const swatchKeys = [
|
||||
"themeUp",
|
||||
"themeDown",
|
||||
"lightColorGradient",
|
||||
"darkColorGradient",
|
||||
];
|
||||
|
||||
const columns: Column<Theme>[] = [
|
||||
{ key: "id", header: "شناسه", render: (r) => toFa(r.id), className: "w-20" },
|
||||
{
|
||||
key: "preview",
|
||||
header: "پیشنمایش",
|
||||
render: (r) => (
|
||||
<span className="inline-flex overflow-hidden rounded-full border border-border">
|
||||
{swatchKeys.map((k) => (
|
||||
<span
|
||||
key={k}
|
||||
className="h-5 w-5"
|
||||
style={{ backgroundColor: cssColor(r.colors?.[k]) }}
|
||||
/>
|
||||
))}
|
||||
</span>
|
||||
),
|
||||
className: "w-32",
|
||||
},
|
||||
{ key: "name", header: "نام" },
|
||||
{
|
||||
key: "key",
|
||||
header: "کلید",
|
||||
render: (r) => <span dir="ltr" className="font-mono">{r.key}</span>,
|
||||
className: "w-28",
|
||||
},
|
||||
{
|
||||
key: "order",
|
||||
header: "ترتیب",
|
||||
render: (r) => toFa(r.order ?? 0),
|
||||
className: "w-20",
|
||||
},
|
||||
{
|
||||
key: "is_active",
|
||||
header: "وضعیت",
|
||||
render: (r) =>
|
||||
r.is_active ? (
|
||||
<Badge tone="success">فعال</Badge>
|
||||
) : (
|
||||
<Badge>غیرفعال</Badge>
|
||||
),
|
||||
className: "w-24",
|
||||
},
|
||||
];
|
||||
|
||||
return (
|
||||
<div>
|
||||
<PageHeader
|
||||
title="تمها"
|
||||
subtitle="ویرایش رنگهای تمهای صحنه"
|
||||
/>
|
||||
|
||||
<DataTable
|
||||
columns={columns}
|
||||
rows={data}
|
||||
loading={loading}
|
||||
error={error}
|
||||
onRetry={reload}
|
||||
emptyMessage="هنوز تمی ثبت نشده است."
|
||||
actions={(row) => (
|
||||
<Button variant="ghost" onClick={() => openEdit(row)} aria-label="ویرایش">
|
||||
<EditIcon className="h-4 w-4" />
|
||||
</Button>
|
||||
)}
|
||||
/>
|
||||
|
||||
<Modal
|
||||
open={open}
|
||||
onClose={() => setOpen(false)}
|
||||
title={editing ? `ویرایش تم «${editing.name}»` : "ویرایش تم"}
|
||||
footer={
|
||||
<>
|
||||
<Button variant="secondary" onClick={() => setOpen(false)}>
|
||||
انصراف
|
||||
</Button>
|
||||
<Button form="theme-form" type="submit" loading={saving}>
|
||||
ذخیره
|
||||
</Button>
|
||||
</>
|
||||
}
|
||||
>
|
||||
<form id="theme-form" onSubmit={save} className="flex flex-col gap-4">
|
||||
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
|
||||
<Field label="نام" required>
|
||||
<Input
|
||||
value={name}
|
||||
onChange={(e) => setName(e.target.value)}
|
||||
required
|
||||
/>
|
||||
</Field>
|
||||
<Field label="ترتیب">
|
||||
<Input
|
||||
type="number"
|
||||
value={order}
|
||||
onChange={(e) => setOrder(e.target.value)}
|
||||
dir="ltr"
|
||||
/>
|
||||
</Field>
|
||||
</div>
|
||||
|
||||
<Field label="فعال">
|
||||
<Switch checked={isActive} onChange={setIsActive} />
|
||||
</Field>
|
||||
|
||||
<div className="border-t border-border pt-4">
|
||||
<p className="mb-3 text-sm font-medium text-foreground">رنگها</p>
|
||||
<div className="grid grid-cols-1 gap-3 sm:grid-cols-2">
|
||||
{Object.keys(colors).map((key) => (
|
||||
<Field key={key} label={key}>
|
||||
<ColorInput
|
||||
value={colors[key] ?? ""}
|
||||
onChange={(v) => setColor(key, v)}
|
||||
/>
|
||||
</Field>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</Modal>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user