feat: initial
This commit is contained in:
@@ -0,0 +1,265 @@
|
||||
"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 {
|
||||
Badge,
|
||||
Button,
|
||||
ConfirmDialog,
|
||||
Field,
|
||||
Input,
|
||||
Switch,
|
||||
Modal,
|
||||
PageHeader,
|
||||
} from "@/components/ui";
|
||||
import { EditIcon, PlusIcon, TrashIcon } from "@/components/icons";
|
||||
import { MediaPreview } from "@/components/MediaPreview";
|
||||
import { pickUrl, SOUND_KEYS } from "@/lib/media";
|
||||
import { toFa } from "@/lib/utils";
|
||||
|
||||
interface BackgroundSound {
|
||||
id: number;
|
||||
name: string;
|
||||
order: number | null;
|
||||
is_active: boolean;
|
||||
}
|
||||
|
||||
export default function BackgroundSoundsPage() {
|
||||
const { data, loading, error, reload } =
|
||||
useList<BackgroundSound>("/background-sounds");
|
||||
const toast = useToast();
|
||||
|
||||
const [editing, setEditing] = useState<BackgroundSound | null>(null);
|
||||
const [open, setOpen] = useState(false);
|
||||
const [name, setName] = useState("");
|
||||
const [order, setOrder] = useState("");
|
||||
const [isActive, setIsActive] = useState(true);
|
||||
const [sound, setSound] = useState<File | null>(null);
|
||||
const [image, setImage] = useState<File | null>(null);
|
||||
const [saving, setSaving] = useState(false);
|
||||
|
||||
const [deleting, setDeleting] = useState<BackgroundSound | null>(null);
|
||||
const [removing, setRemoving] = useState(false);
|
||||
|
||||
function openCreate() {
|
||||
setEditing(null);
|
||||
setName("");
|
||||
setOrder("");
|
||||
setIsActive(true);
|
||||
setSound(null);
|
||||
setImage(null);
|
||||
setOpen(true);
|
||||
}
|
||||
function openEdit(row: BackgroundSound) {
|
||||
setEditing(row);
|
||||
setName(row.name ?? "");
|
||||
setOrder(row.order?.toString() ?? "");
|
||||
setIsActive(!!row.is_active);
|
||||
setSound(null);
|
||||
setImage(null);
|
||||
setOpen(true);
|
||||
}
|
||||
|
||||
async function save(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
setSaving(true);
|
||||
try {
|
||||
const body = toFormData({
|
||||
name,
|
||||
order,
|
||||
is_active: isActive,
|
||||
sound,
|
||||
image,
|
||||
});
|
||||
if (editing) {
|
||||
// File-bearing resource: update via POST /background-sounds/:id (multipart)
|
||||
await apiFetch(`/background-sounds/${editing.id}`, {
|
||||
method: "POST",
|
||||
body,
|
||||
});
|
||||
toast.success("صدای پسزمینه ویرایش شد.");
|
||||
} else {
|
||||
await apiFetch("/background-sounds", {
|
||||
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(`/background-sounds/${deleting.id}`, { method: "DELETE" });
|
||||
toast.success("صدای پسزمینه حذف شد.");
|
||||
setDeleting(null);
|
||||
reload();
|
||||
} catch (err) {
|
||||
toast.error(err instanceof ApiError ? err.message : "خطا در حذف");
|
||||
} finally {
|
||||
setRemoving(false);
|
||||
}
|
||||
}
|
||||
|
||||
const columns: Column<BackgroundSound>[] = [
|
||||
{ key: "id", header: "شناسه", render: (r) => toFa(r.id), className: "w-24" },
|
||||
{ key: "name", header: "نام" },
|
||||
{
|
||||
key: "preview",
|
||||
header: "پخش",
|
||||
render: (r) => (
|
||||
<MediaPreview kind="audio" src={pickUrl(r, SOUND_KEYS)} label={r.name} />
|
||||
),
|
||||
},
|
||||
{ key: "order", header: "ترتیب", render: (r) => toFa(r.order ?? "—") },
|
||||
{
|
||||
key: "is_active",
|
||||
header: "وضعیت",
|
||||
render: (r) =>
|
||||
r.is_active ? (
|
||||
<Badge tone="success">فعال</Badge>
|
||||
) : (
|
||||
<Badge tone="neutral">غیرفعال</Badge>
|
||||
),
|
||||
},
|
||||
];
|
||||
|
||||
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="background-form" type="submit" loading={saving}>
|
||||
ذخیره
|
||||
</Button>
|
||||
</>
|
||||
}
|
||||
>
|
||||
<form
|
||||
id="background-form"
|
||||
onSubmit={save}
|
||||
className="flex flex-col gap-4"
|
||||
>
|
||||
<Field label="نام" required>
|
||||
<Input
|
||||
value={name}
|
||||
onChange={(e) => setName(e.target.value)}
|
||||
placeholder="مثلاً صدای باران"
|
||||
required
|
||||
autoFocus
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field label="ترتیب">
|
||||
<Input
|
||||
type="number"
|
||||
dir="ltr"
|
||||
value={order}
|
||||
onChange={(e) => setOrder(e.target.value)}
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field label="وضعیت">
|
||||
<Switch
|
||||
checked={isActive}
|
||||
onChange={setIsActive}
|
||||
label={isActive ? "فعال" : "غیرفعال"}
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field
|
||||
label="فایل صوتی"
|
||||
hint={editing ? "در صورت عدم انتخاب، فایل فعلی حفظ میشود." : undefined}
|
||||
required={!editing}
|
||||
>
|
||||
<Input
|
||||
type="file"
|
||||
accept="audio/*"
|
||||
onChange={(e) => setSound(e.target.files?.[0] ?? null)}
|
||||
required={!editing}
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field
|
||||
label="تصویر"
|
||||
hint={editing ? "در صورت عدم انتخاب، تصویر فعلی حفظ میشود." : undefined}
|
||||
>
|
||||
<Input
|
||||
type="file"
|
||||
accept="image/*"
|
||||
onChange={(e) => setImage(e.target.files?.[0] ?? null)}
|
||||
/>
|
||||
</Field>
|
||||
</form>
|
||||
</Modal>
|
||||
|
||||
<ConfirmDialog
|
||||
open={!!deleting}
|
||||
message={`آیا از حذف «${deleting?.name}» مطمئن هستید؟`}
|
||||
loading={removing}
|
||||
onConfirm={confirmDelete}
|
||||
onClose={() => setDeleting(null)}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,260 @@
|
||||
"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 {
|
||||
Badge,
|
||||
Button,
|
||||
ConfirmDialog,
|
||||
Field,
|
||||
Input,
|
||||
Switch,
|
||||
Modal,
|
||||
PageHeader,
|
||||
} from "@/components/ui";
|
||||
import { EditIcon, PlusIcon, TrashIcon } from "@/components/icons";
|
||||
import { MediaPreview } from "@/components/MediaPreview";
|
||||
import { pickUrl, SOUND_KEYS } from "@/lib/media";
|
||||
import { toFa } from "@/lib/utils";
|
||||
|
||||
interface BellSound {
|
||||
id: number;
|
||||
name: string;
|
||||
order: number | null;
|
||||
is_active: boolean;
|
||||
}
|
||||
|
||||
export default function BellSoundsPage() {
|
||||
const { data, loading, error, reload } = useList<BellSound>("/bell-sounds");
|
||||
const toast = useToast();
|
||||
|
||||
const [editing, setEditing] = useState<BellSound | null>(null);
|
||||
const [open, setOpen] = useState(false);
|
||||
const [name, setName] = useState("");
|
||||
const [order, setOrder] = useState("");
|
||||
const [isActive, setIsActive] = useState(true);
|
||||
const [sound, setSound] = useState<File | null>(null);
|
||||
const [image, setImage] = useState<File | null>(null);
|
||||
const [saving, setSaving] = useState(false);
|
||||
|
||||
const [deleting, setDeleting] = useState<BellSound | null>(null);
|
||||
const [removing, setRemoving] = useState(false);
|
||||
|
||||
function openCreate() {
|
||||
setEditing(null);
|
||||
setName("");
|
||||
setOrder("");
|
||||
setIsActive(true);
|
||||
setSound(null);
|
||||
setImage(null);
|
||||
setOpen(true);
|
||||
}
|
||||
function openEdit(row: BellSound) {
|
||||
setEditing(row);
|
||||
setName(row.name ?? "");
|
||||
setOrder(row.order?.toString() ?? "");
|
||||
setIsActive(!!row.is_active);
|
||||
setSound(null);
|
||||
setImage(null);
|
||||
setOpen(true);
|
||||
}
|
||||
|
||||
async function save(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
setSaving(true);
|
||||
try {
|
||||
const body = toFormData({
|
||||
name,
|
||||
order,
|
||||
is_active: isActive,
|
||||
sound,
|
||||
image,
|
||||
});
|
||||
if (editing) {
|
||||
// File-bearing resource: update via POST /bell-sounds/:id (multipart)
|
||||
await apiFetch(`/bell-sounds/${editing.id}`, {
|
||||
method: "POST",
|
||||
body,
|
||||
});
|
||||
toast.success("صدای زنگ ویرایش شد.");
|
||||
} else {
|
||||
await apiFetch("/bell-sounds", {
|
||||
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(`/bell-sounds/${deleting.id}`, { method: "DELETE" });
|
||||
toast.success("صدای زنگ حذف شد.");
|
||||
setDeleting(null);
|
||||
reload();
|
||||
} catch (err) {
|
||||
toast.error(err instanceof ApiError ? err.message : "خطا در حذف");
|
||||
} finally {
|
||||
setRemoving(false);
|
||||
}
|
||||
}
|
||||
|
||||
const columns: Column<BellSound>[] = [
|
||||
{ key: "id", header: "شناسه", render: (r) => toFa(r.id), className: "w-24" },
|
||||
{ key: "name", header: "نام" },
|
||||
{
|
||||
key: "preview",
|
||||
header: "پخش",
|
||||
render: (r) => (
|
||||
<MediaPreview kind="audio" src={pickUrl(r, SOUND_KEYS)} label={r.name} />
|
||||
),
|
||||
},
|
||||
{ key: "order", header: "ترتیب", render: (r) => toFa(r.order ?? "—") },
|
||||
{
|
||||
key: "is_active",
|
||||
header: "وضعیت",
|
||||
render: (r) =>
|
||||
r.is_active ? (
|
||||
<Badge tone="success">فعال</Badge>
|
||||
) : (
|
||||
<Badge tone="neutral">غیرفعال</Badge>
|
||||
),
|
||||
},
|
||||
];
|
||||
|
||||
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="bell-form" type="submit" loading={saving}>
|
||||
ذخیره
|
||||
</Button>
|
||||
</>
|
||||
}
|
||||
>
|
||||
<form id="bell-form" onSubmit={save} className="flex flex-col gap-4">
|
||||
<Field label="نام" required>
|
||||
<Input
|
||||
value={name}
|
||||
onChange={(e) => setName(e.target.value)}
|
||||
placeholder="مثلاً زنگ تبتی"
|
||||
required
|
||||
autoFocus
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field label="ترتیب">
|
||||
<Input
|
||||
type="number"
|
||||
dir="ltr"
|
||||
value={order}
|
||||
onChange={(e) => setOrder(e.target.value)}
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field label="وضعیت">
|
||||
<Switch
|
||||
checked={isActive}
|
||||
onChange={setIsActive}
|
||||
label={isActive ? "فعال" : "غیرفعال"}
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field
|
||||
label="فایل صوتی"
|
||||
hint={editing ? "در صورت عدم انتخاب، فایل فعلی حفظ میشود." : undefined}
|
||||
required={!editing}
|
||||
>
|
||||
<Input
|
||||
type="file"
|
||||
accept="audio/*"
|
||||
onChange={(e) => setSound(e.target.files?.[0] ?? null)}
|
||||
required={!editing}
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field
|
||||
label="تصویر"
|
||||
hint={editing ? "در صورت عدم انتخاب، تصویر فعلی حفظ میشود." : undefined}
|
||||
>
|
||||
<Input
|
||||
type="file"
|
||||
accept="image/*"
|
||||
onChange={(e) => setImage(e.target.files?.[0] ?? null)}
|
||||
/>
|
||||
</Field>
|
||||
</form>
|
||||
</Modal>
|
||||
|
||||
<ConfirmDialog
|
||||
open={!!deleting}
|
||||
message={`آیا از حذف «${deleting?.name}» مطمئن هستید؟`}
|
||||
loading={removing}
|
||||
onConfirm={confirmDelete}
|
||||
onClose={() => setDeleting(null)}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
"use client";
|
||||
|
||||
// Read-only view of GET /timer/options — the option sets the app uses to build
|
||||
// the meditation timer (durations, intervals, default bells, etc.). The exact
|
||||
// shape is backend-defined, so we render it generically: primitive values as a
|
||||
// definition list, arrays of objects as small tables.
|
||||
|
||||
import { useItem } from "@/lib/useResource";
|
||||
import { Button, Card, PageHeader, Spinner, Badge, EmptyState } from "@/components/ui";
|
||||
import { toFa } from "@/lib/utils";
|
||||
|
||||
function Primitive({ value }: { value: unknown }) {
|
||||
if (typeof value === "boolean") {
|
||||
return <Badge tone={value ? "success" : "neutral"}>{value ? "بله" : "خیر"}</Badge>;
|
||||
}
|
||||
if (value === null || value === undefined) return <span className="text-muted">—</span>;
|
||||
return <span>{toFa(String(value))}</span>;
|
||||
}
|
||||
|
||||
function ArrayBlock({ items }: { items: unknown[] }) {
|
||||
if (!items.length) return <EmptyState message="موردی وجود ندارد." />;
|
||||
|
||||
// Array of objects -> table of their union of keys.
|
||||
if (items.every((it) => it && typeof it === "object" && !Array.isArray(it))) {
|
||||
const keys = Array.from(
|
||||
new Set(items.flatMap((it) => Object.keys(it as object))),
|
||||
);
|
||||
return (
|
||||
<div className="overflow-x-auto rounded-xl border border-border">
|
||||
<table className="w-full text-right text-sm">
|
||||
<thead>
|
||||
<tr className="border-b border-border bg-surface-muted text-muted">
|
||||
{keys.map((k) => (
|
||||
<th key={k} className="px-3 py-2 font-medium">
|
||||
{k}
|
||||
</th>
|
||||
))}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{items.map((it, i) => (
|
||||
<tr key={i} className="border-b border-border last:border-0">
|
||||
{keys.map((k) => (
|
||||
<td key={k} className="px-3 py-2">
|
||||
<Primitive value={(it as Record<string, unknown>)[k]} />
|
||||
</td>
|
||||
))}
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// Array of primitives -> chips.
|
||||
return (
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{items.map((it, i) => (
|
||||
<Badge key={i}>{toFa(String(it))}</Badge>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default function TimerOptionsPage() {
|
||||
const { data, loading, error, reload } = useItem<Record<string, unknown>>(
|
||||
"/timer/options",
|
||||
);
|
||||
|
||||
return (
|
||||
<div>
|
||||
<PageHeader
|
||||
title="گزینههای تایمر"
|
||||
subtitle="مقادیر پیشفرض و فهرستهایی که اپلیکیشن برای ساخت تایمر مدیتیشن استفاده میکند."
|
||||
/>
|
||||
|
||||
{loading ? (
|
||||
<div className="flex justify-center py-16">
|
||||
<Spinner className="h-8 w-8" />
|
||||
</div>
|
||||
) : error ? (
|
||||
<EmptyState
|
||||
icon="⚠️"
|
||||
title="خطا در دریافت اطلاعات"
|
||||
message={error}
|
||||
action={
|
||||
<Button variant="secondary" onClick={reload}>
|
||||
تلاش دوباره
|
||||
</Button>
|
||||
}
|
||||
/>
|
||||
) : !data || typeof data !== "object" || !Object.keys(data).length ? (
|
||||
<EmptyState
|
||||
title="گزینهای موجود نیست"
|
||||
message="هیچ گزینهای از سرور دریافت نشد."
|
||||
/>
|
||||
) : (
|
||||
<div className="grid grid-cols-1 gap-4 lg:grid-cols-2">
|
||||
{Object.entries(data).map(([key, value]) => (
|
||||
<Card key={key}>
|
||||
<h2 className="mb-3 font-bold text-foreground">{key}</h2>
|
||||
{Array.isArray(value) ? (
|
||||
<ArrayBlock items={value} />
|
||||
) : value && typeof value === "object" ? (
|
||||
<dl className="flex flex-col gap-2 text-sm">
|
||||
{Object.entries(value as Record<string, unknown>).map(
|
||||
([k, v]) => (
|
||||
<div key={k} className="flex justify-between gap-3 border-b border-border pb-1.5 last:border-0">
|
||||
<dt className="text-muted">{k}</dt>
|
||||
<dd>
|
||||
<Primitive value={v} />
|
||||
</dd>
|
||||
</div>
|
||||
),
|
||||
)}
|
||||
</dl>
|
||||
) : (
|
||||
<Primitive value={value} />
|
||||
)}
|
||||
</Card>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,354 @@
|
||||
"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 {
|
||||
Button,
|
||||
ConfirmDialog,
|
||||
Field,
|
||||
Input,
|
||||
Select,
|
||||
Modal,
|
||||
PageHeader,
|
||||
} from "@/components/ui";
|
||||
import { EditIcon, PlusIcon, TrashIcon } from "@/components/icons";
|
||||
import { toFa, formatDuration } from "@/lib/utils";
|
||||
|
||||
interface TimerPreset {
|
||||
id: number;
|
||||
name: string;
|
||||
duration_seconds: number;
|
||||
start_bell_id: number | null;
|
||||
end_bell_id: number | null;
|
||||
interval_bell_id: number | null;
|
||||
interval_seconds: number | null;
|
||||
interval_repeat: number | null;
|
||||
background_sound_id: number | null;
|
||||
background_image_id: number | null;
|
||||
volume: number | null;
|
||||
}
|
||||
|
||||
interface BellSound {
|
||||
id: number;
|
||||
name: string;
|
||||
}
|
||||
|
||||
interface BackgroundSound {
|
||||
id: number;
|
||||
name: string;
|
||||
}
|
||||
|
||||
const emptyForm = {
|
||||
name: "",
|
||||
duration_seconds: "",
|
||||
start_bell_id: "",
|
||||
end_bell_id: "",
|
||||
interval_bell_id: "",
|
||||
interval_seconds: "",
|
||||
interval_repeat: "",
|
||||
background_sound_id: "",
|
||||
background_image_id: "",
|
||||
volume: "",
|
||||
};
|
||||
|
||||
export default function TimerPresetsPage() {
|
||||
const { data, loading, error, reload } = useList<TimerPreset>("/timer-presets");
|
||||
const { data: bells } = useList<BellSound>("/bell-sounds");
|
||||
const { data: backgroundSounds } =
|
||||
useList<BackgroundSound>("/background-sounds");
|
||||
const toast = useToast();
|
||||
|
||||
const [editing, setEditing] = useState<TimerPreset | null>(null);
|
||||
const [open, setOpen] = useState(false);
|
||||
const [form, setForm] = useState({ ...emptyForm });
|
||||
const [saving, setSaving] = useState(false);
|
||||
|
||||
const [deleting, setDeleting] = useState<TimerPreset | null>(null);
|
||||
const [removing, setRemoving] = useState(false);
|
||||
|
||||
function set<K extends keyof typeof form>(key: K, value: string) {
|
||||
setForm((f) => ({ ...f, [key]: value }));
|
||||
}
|
||||
|
||||
function openCreate() {
|
||||
setEditing(null);
|
||||
setForm({ ...emptyForm });
|
||||
setOpen(true);
|
||||
}
|
||||
function openEdit(row: TimerPreset) {
|
||||
setEditing(row);
|
||||
setForm({
|
||||
name: row.name ?? "",
|
||||
duration_seconds: row.duration_seconds?.toString() ?? "",
|
||||
start_bell_id: row.start_bell_id?.toString() ?? "",
|
||||
end_bell_id: row.end_bell_id?.toString() ?? "",
|
||||
interval_bell_id: row.interval_bell_id?.toString() ?? "",
|
||||
interval_seconds: row.interval_seconds?.toString() ?? "",
|
||||
interval_repeat: row.interval_repeat?.toString() ?? "",
|
||||
background_sound_id: row.background_sound_id?.toString() ?? "",
|
||||
background_image_id: row.background_image_id?.toString() ?? "",
|
||||
volume: row.volume?.toString() ?? "",
|
||||
});
|
||||
setOpen(true);
|
||||
}
|
||||
|
||||
function toNum(value: string): number | null {
|
||||
if (value === "" || value === null || value === undefined) return null;
|
||||
const n = Number(value);
|
||||
return Number.isNaN(n) ? null : n;
|
||||
}
|
||||
|
||||
async function save(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
setSaving(true);
|
||||
try {
|
||||
const body = {
|
||||
name: form.name,
|
||||
duration_seconds: toNum(form.duration_seconds),
|
||||
start_bell_id: toNum(form.start_bell_id),
|
||||
end_bell_id: toNum(form.end_bell_id),
|
||||
interval_bell_id: toNum(form.interval_bell_id),
|
||||
interval_seconds: toNum(form.interval_seconds),
|
||||
interval_repeat: toNum(form.interval_repeat),
|
||||
background_sound_id: toNum(form.background_sound_id),
|
||||
background_image_id: toNum(form.background_image_id),
|
||||
volume: toNum(form.volume),
|
||||
};
|
||||
if (editing) {
|
||||
await apiFetch(`/timer-presets/${editing.id}`, {
|
||||
method: "PUT",
|
||||
body,
|
||||
});
|
||||
toast.success("پیشتنظیم ویرایش شد.");
|
||||
} else {
|
||||
await apiFetch("/timer-presets", {
|
||||
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(`/timer-presets/${deleting.id}`, { method: "DELETE" });
|
||||
toast.success("پیشتنظیم حذف شد.");
|
||||
setDeleting(null);
|
||||
reload();
|
||||
} catch (err) {
|
||||
toast.error(err instanceof ApiError ? err.message : "خطا در حذف");
|
||||
} finally {
|
||||
setRemoving(false);
|
||||
}
|
||||
}
|
||||
|
||||
const columns: Column<TimerPreset>[] = [
|
||||
{ key: "id", header: "شناسه", render: (r) => toFa(r.id), className: "w-24" },
|
||||
{ key: "name", header: "نام" },
|
||||
{
|
||||
key: "duration_seconds",
|
||||
header: "مدت",
|
||||
render: (r) => formatDuration(r.duration_seconds),
|
||||
},
|
||||
{ key: "volume", header: "صدا", render: (r) => toFa(r.volume ?? "—") },
|
||||
];
|
||||
|
||||
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="preset-form" type="submit" loading={saving}>
|
||||
ذخیره
|
||||
</Button>
|
||||
</>
|
||||
}
|
||||
>
|
||||
<form id="preset-form" onSubmit={save} className="flex flex-col gap-4">
|
||||
<Field label="نام" required>
|
||||
<Input
|
||||
value={form.name}
|
||||
onChange={(e) => set("name", e.target.value)}
|
||||
placeholder="مثلاً مدیتیشن صبحگاهی"
|
||||
required
|
||||
autoFocus
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field label="مدت (ثانیه)" required>
|
||||
<Input
|
||||
type="number"
|
||||
dir="ltr"
|
||||
value={form.duration_seconds}
|
||||
onChange={(e) => set("duration_seconds", e.target.value)}
|
||||
placeholder="مثلاً ۶۰۰"
|
||||
required
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field label="زنگ شروع">
|
||||
<Select
|
||||
value={form.start_bell_id}
|
||||
onChange={(e) => set("start_bell_id", e.target.value)}
|
||||
>
|
||||
<option value="">— انتخاب کنید —</option>
|
||||
{bells.map((b) => (
|
||||
<option key={b.id} value={b.id}>
|
||||
{b.name}
|
||||
</option>
|
||||
))}
|
||||
</Select>
|
||||
</Field>
|
||||
|
||||
<Field label="زنگ پایان">
|
||||
<Select
|
||||
value={form.end_bell_id}
|
||||
onChange={(e) => set("end_bell_id", e.target.value)}
|
||||
>
|
||||
<option value="">— انتخاب کنید —</option>
|
||||
{bells.map((b) => (
|
||||
<option key={b.id} value={b.id}>
|
||||
{b.name}
|
||||
</option>
|
||||
))}
|
||||
</Select>
|
||||
</Field>
|
||||
|
||||
<Field label="زنگ میاندورهای">
|
||||
<Select
|
||||
value={form.interval_bell_id}
|
||||
onChange={(e) => set("interval_bell_id", e.target.value)}
|
||||
>
|
||||
<option value="">— انتخاب کنید —</option>
|
||||
{bells.map((b) => (
|
||||
<option key={b.id} value={b.id}>
|
||||
{b.name}
|
||||
</option>
|
||||
))}
|
||||
</Select>
|
||||
</Field>
|
||||
|
||||
<Field label="فاصله زمانی (ثانیه)">
|
||||
<Input
|
||||
type="number"
|
||||
dir="ltr"
|
||||
value={form.interval_seconds}
|
||||
onChange={(e) => set("interval_seconds", e.target.value)}
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field label="تعداد تکرار فاصله">
|
||||
<Input
|
||||
type="number"
|
||||
dir="ltr"
|
||||
value={form.interval_repeat}
|
||||
onChange={(e) => set("interval_repeat", e.target.value)}
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field label="صدای پسزمینه">
|
||||
<Select
|
||||
value={form.background_sound_id}
|
||||
onChange={(e) => set("background_sound_id", e.target.value)}
|
||||
>
|
||||
<option value="">— انتخاب کنید —</option>
|
||||
{backgroundSounds.map((s) => (
|
||||
<option key={s.id} value={s.id}>
|
||||
{s.name}
|
||||
</option>
|
||||
))}
|
||||
</Select>
|
||||
</Field>
|
||||
|
||||
<Field label="شناسه تصویر پسزمینه">
|
||||
<Input
|
||||
type="number"
|
||||
dir="ltr"
|
||||
value={form.background_image_id}
|
||||
onChange={(e) => set("background_image_id", e.target.value)}
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field label="میزان صدا">
|
||||
<Input
|
||||
type="number"
|
||||
dir="ltr"
|
||||
value={form.volume}
|
||||
onChange={(e) => set("volume", e.target.value)}
|
||||
placeholder="مثلاً ۸۰"
|
||||
/>
|
||||
</Field>
|
||||
</form>
|
||||
</Modal>
|
||||
|
||||
<ConfirmDialog
|
||||
open={!!deleting}
|
||||
message={`آیا از حذف «${deleting?.name}» مطمئن هستید؟`}
|
||||
loading={removing}
|
||||
onConfirm={confirmDelete}
|
||||
onClose={() => setDeleting(null)}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user