Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
504b1e2c8e | ||
|
|
60a054710a | ||
|
|
1b2d2dec7d | ||
|
|
e374829358 |
@@ -17,6 +17,9 @@ import {
|
||||
PageHeader,
|
||||
} from "@/components/ui";
|
||||
import { EditIcon, PlusIcon, TrashIcon } from "@/components/icons";
|
||||
import { ImagePicker } from "@/components/ImagePicker";
|
||||
import { MediaPreview } from "@/components/MediaPreview";
|
||||
import { pickUrl, IMAGE_KEYS } from "@/lib/media";
|
||||
import { toFa } from "@/lib/utils";
|
||||
|
||||
interface MusicCategory {
|
||||
@@ -25,6 +28,8 @@ interface MusicCategory {
|
||||
description?: string;
|
||||
order?: number;
|
||||
is_active?: boolean;
|
||||
image_id?: number | null;
|
||||
image?: unknown;
|
||||
}
|
||||
|
||||
export default function MusicCategoriesPage() {
|
||||
@@ -39,7 +44,8 @@ export default function MusicCategoriesPage() {
|
||||
const [name, setName] = useState("");
|
||||
const [description, setDescription] = useState("");
|
||||
const [order, setOrder] = useState("");
|
||||
const [imageId, setImageId] = useState("");
|
||||
const [imageId, setImageId] = useState<number | null>(null);
|
||||
const [imageFile, setImageFile] = useState<File | null>(null);
|
||||
const [isActive, setIsActive] = useState(true);
|
||||
|
||||
const [deleting, setDeleting] = useState<MusicCategory | null>(null);
|
||||
@@ -50,7 +56,8 @@ export default function MusicCategoriesPage() {
|
||||
setName("");
|
||||
setDescription("");
|
||||
setOrder("");
|
||||
setImageId("");
|
||||
setImageId(null);
|
||||
setImageFile(null);
|
||||
setIsActive(true);
|
||||
setOpen(true);
|
||||
}
|
||||
@@ -59,7 +66,8 @@ export default function MusicCategoriesPage() {
|
||||
setName(row.name ?? "");
|
||||
setDescription(row.description ?? "");
|
||||
setOrder(row.order != null ? String(row.order) : "");
|
||||
setImageId("");
|
||||
setImageId(row.image_id ?? null);
|
||||
setImageFile(null);
|
||||
setIsActive(row.is_active ?? true);
|
||||
setOpen(true);
|
||||
}
|
||||
@@ -69,15 +77,17 @@ export default function MusicCategoriesPage() {
|
||||
setSaving(true);
|
||||
try {
|
||||
if (editing) {
|
||||
// Update is JSON on /music-categories/:id
|
||||
// Update is multipart (POST) so an image file can be uploaded.
|
||||
await apiFetch(`/music-categories/${editing.id}`, {
|
||||
method: "PUT",
|
||||
body: {
|
||||
method: "POST",
|
||||
body: toFormData({
|
||||
name,
|
||||
description,
|
||||
order: order === "" ? undefined : Number(order),
|
||||
is_active: isActive,
|
||||
},
|
||||
image_id: imageId,
|
||||
image: imageFile,
|
||||
}),
|
||||
});
|
||||
toast.success("دستهبندی ویرایش شد.");
|
||||
} else {
|
||||
@@ -88,8 +98,9 @@ export default function MusicCategoriesPage() {
|
||||
name,
|
||||
description,
|
||||
order,
|
||||
image_id: imageId,
|
||||
is_active: isActive,
|
||||
image_id: imageId,
|
||||
image: imageFile,
|
||||
}),
|
||||
});
|
||||
toast.success("دستهبندی افزوده شد.");
|
||||
@@ -120,6 +131,14 @@ export default function MusicCategoriesPage() {
|
||||
|
||||
const columns: Column<MusicCategory>[] = [
|
||||
{ key: "id", header: "شناسه", render: (r) => toFa(r.id), className: "w-24" },
|
||||
{
|
||||
key: "image",
|
||||
header: "تصویر",
|
||||
className: "w-20",
|
||||
render: (r) => (
|
||||
<MediaPreview kind="image" src={pickUrl(r, IMAGE_KEYS)} label={r.name} />
|
||||
),
|
||||
},
|
||||
{ key: "name", header: "نام" },
|
||||
{
|
||||
key: "description",
|
||||
@@ -223,16 +242,15 @@ export default function MusicCategoriesPage() {
|
||||
dir="ltr"
|
||||
/>
|
||||
</Field>
|
||||
{!editing && (
|
||||
<Field label="شناسه تصویر">
|
||||
<Input
|
||||
value={imageId}
|
||||
onChange={(e) => setImageId(e.target.value)}
|
||||
dir="ltr"
|
||||
placeholder="image_id"
|
||||
<Field label="تصویر">
|
||||
<ImagePicker
|
||||
imageId={imageId}
|
||||
onPickId={setImageId}
|
||||
file={imageFile}
|
||||
onPickFile={setImageFile}
|
||||
existingUrl={editing ? pickUrl(editing, IMAGE_KEYS) : null}
|
||||
/>
|
||||
</Field>
|
||||
)}
|
||||
<Switch checked={isActive} onChange={setIsActive} label="فعال" />
|
||||
</form>
|
||||
</Modal>
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
import { useState } from "react";
|
||||
import { apiFetch, ApiError, toFormData } from "@/lib/api";
|
||||
import { useList } from "@/lib/useResource";
|
||||
import { useList, usePaginated } from "@/lib/useResource";
|
||||
import { useToast } from "@/components/toast";
|
||||
import { DataTable, type Column } from "@/components/DataTable";
|
||||
import {
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
Select,
|
||||
Modal,
|
||||
PageHeader,
|
||||
Pagination,
|
||||
} from "@/components/ui";
|
||||
import { EditIcon, PlusIcon, TrashIcon } from "@/components/icons";
|
||||
import { formatMinutes, fileNameToTitle } from "@/lib/utils";
|
||||
@@ -20,6 +21,14 @@ import { MediaPreview } from "@/components/MediaPreview";
|
||||
import { ImagePicker } from "@/components/ImagePicker";
|
||||
import { pickUrl, SOUND_KEYS, IMAGE_KEYS } from "@/lib/media";
|
||||
|
||||
interface Playlist {
|
||||
id: number;
|
||||
name?: string;
|
||||
// Some endpoints nest the playlist's sub-categories; key casing varies.
|
||||
subcategories?: Playlist[];
|
||||
subCategories?: Playlist[];
|
||||
}
|
||||
|
||||
interface Track {
|
||||
id: number;
|
||||
title?: string;
|
||||
@@ -28,15 +37,17 @@ interface Track {
|
||||
type?: string;
|
||||
playlist_id?: number;
|
||||
image_id?: number | null;
|
||||
}
|
||||
|
||||
interface Playlist {
|
||||
id: number;
|
||||
name?: string;
|
||||
// A track belongs to many playlists; the /music row embeds them as an array.
|
||||
playlists?: Playlist[];
|
||||
// Sub-categories may also be embedded directly on the track (key casing varies).
|
||||
subcategories?: Playlist[];
|
||||
subCategories?: Playlist[];
|
||||
sub_categories?: Playlist[];
|
||||
}
|
||||
|
||||
export default function MusicTracksPage() {
|
||||
const { data, loading, error, reload } = useList<Track>("/music");
|
||||
const { data, meta, page, setPage, loading, error, reload } =
|
||||
usePaginated<Track>("/music", { perPage: 20 });
|
||||
const { data: playlists } = useList<Playlist>("/music-playlists");
|
||||
const toast = useToast();
|
||||
|
||||
@@ -56,6 +67,26 @@ export default function MusicTracksPage() {
|
||||
const [deleting, setDeleting] = useState<Track | null>(null);
|
||||
const [removing, setRemoving] = useState(false);
|
||||
|
||||
// A track belongs to many playlists — list their names.
|
||||
function playlistName(r: Track): string {
|
||||
const names = (r.playlists ?? []).map((p) => p.name).filter(Boolean);
|
||||
return names.length ? (names as string[]).join("، ") : "—";
|
||||
}
|
||||
|
||||
// Sub-categories may sit on the track directly or come through its playlists;
|
||||
// key casing varies between endpoints, so gather from every shape.
|
||||
function subCategoryNames(r: Track): string {
|
||||
const subs: Playlist[] = [
|
||||
...(r.subcategories ?? r.subCategories ?? r.sub_categories ?? []),
|
||||
...(r.playlists ?? []).flatMap(
|
||||
(p) => p.subcategories ?? p.subCategories ?? [],
|
||||
),
|
||||
];
|
||||
const names = subs.map((s) => s.name).filter(Boolean) as string[];
|
||||
const unique = Array.from(new Set(names));
|
||||
return unique.length ? unique.join("، ") : "—";
|
||||
}
|
||||
|
||||
function resetForm() {
|
||||
setTitle("");
|
||||
setArtist("");
|
||||
@@ -163,6 +194,16 @@ export default function MusicTracksPage() {
|
||||
},
|
||||
{ key: "title", header: "عنوان" },
|
||||
{ key: "artist", header: "هنرمند", render: (r) => r.artist ?? "—" },
|
||||
{
|
||||
key: "playlist",
|
||||
header: "پلیلیست",
|
||||
render: (r) => playlistName(r),
|
||||
},
|
||||
{
|
||||
key: "subcategory",
|
||||
header: "زیردسته",
|
||||
render: (r) => subCategoryNames(r),
|
||||
},
|
||||
{
|
||||
key: "duration",
|
||||
header: "مدت زمان",
|
||||
@@ -217,6 +258,15 @@ export default function MusicTracksPage() {
|
||||
)}
|
||||
/>
|
||||
|
||||
{meta && (
|
||||
<Pagination
|
||||
page={page}
|
||||
lastPage={meta.last_page}
|
||||
total={meta.total}
|
||||
onChange={setPage}
|
||||
/>
|
||||
)}
|
||||
|
||||
<Modal
|
||||
open={open}
|
||||
onClose={() => setOpen(false)}
|
||||
|
||||
@@ -20,9 +20,21 @@ import {
|
||||
import { EditIcon, PlusIcon, TrashIcon } from "@/components/icons";
|
||||
import { toFa } from "@/lib/utils";
|
||||
|
||||
interface SurveyTag {
|
||||
id: number;
|
||||
name: string;
|
||||
}
|
||||
|
||||
interface SurveyOption {
|
||||
id?: number;
|
||||
label?: string;
|
||||
tags?: SurveyTag[];
|
||||
}
|
||||
|
||||
// Form-side option: tags edited as a comma-separated string.
|
||||
interface OptionDraft {
|
||||
label: string;
|
||||
tags: string;
|
||||
}
|
||||
|
||||
interface SurveyQuestion {
|
||||
@@ -54,7 +66,9 @@ export default function SurveysPage() {
|
||||
const [type, setType] = useState("single");
|
||||
const [order, setOrder] = useState("0");
|
||||
const [isActive, setIsActive] = useState(true);
|
||||
const [options, setOptions] = useState<string[]>([""]);
|
||||
const [options, setOptions] = useState<OptionDraft[]>([
|
||||
{ label: "", tags: "" },
|
||||
]);
|
||||
|
||||
const [deleting, setDeleting] = useState<SurveyQuestion | null>(null);
|
||||
const [removing, setRemoving] = useState(false);
|
||||
@@ -66,7 +80,7 @@ export default function SurveysPage() {
|
||||
setType("single");
|
||||
setOrder("0");
|
||||
setIsActive(true);
|
||||
setOptions([""]);
|
||||
setOptions([{ label: "", tags: "" }]);
|
||||
setOpen(true);
|
||||
}
|
||||
|
||||
@@ -79,17 +93,22 @@ export default function SurveysPage() {
|
||||
setIsActive(row.is_active ?? true);
|
||||
setOptions(
|
||||
row.options && row.options.length
|
||||
? row.options.map((o) => o.label ?? "")
|
||||
: [""],
|
||||
? row.options.map((o) => ({
|
||||
label: o.label ?? "",
|
||||
tags: (o.tags ?? []).map((t) => t.name).join("، "),
|
||||
}))
|
||||
: [{ label: "", tags: "" }],
|
||||
);
|
||||
setOpen(true);
|
||||
}
|
||||
|
||||
function setOptionAt(index: number, value: string) {
|
||||
setOptions((prev) => prev.map((o, i) => (i === index ? value : o)));
|
||||
function setOptionAt(index: number, patch: Partial<OptionDraft>) {
|
||||
setOptions((prev) =>
|
||||
prev.map((o, i) => (i === index ? { ...o, ...patch } : o)),
|
||||
);
|
||||
}
|
||||
function addOption() {
|
||||
setOptions((prev) => [...prev, ""]);
|
||||
setOptions((prev) => [...prev, { label: "", tags: "" }]);
|
||||
}
|
||||
function removeOption(index: number) {
|
||||
setOptions((prev) =>
|
||||
@@ -108,9 +127,15 @@ export default function SurveysPage() {
|
||||
order: Number(order),
|
||||
is_active: isActive,
|
||||
options: options
|
||||
.map((label) => label.trim())
|
||||
.filter((label) => label !== "")
|
||||
.map((label) => ({ label })),
|
||||
.map((o) => ({ label: o.label.trim(), tagsRaw: o.tags }))
|
||||
.filter((o) => o.label !== "")
|
||||
.map((o) => ({
|
||||
label: o.label,
|
||||
tags: o.tagsRaw
|
||||
.split(/[,،]/)
|
||||
.map((t) => t.trim())
|
||||
.filter((t) => t !== ""),
|
||||
})),
|
||||
};
|
||||
if (editing) {
|
||||
await apiFetch(`/survey-questions/${editing.id}`, {
|
||||
@@ -283,12 +308,22 @@ export default function SurveysPage() {
|
||||
</Button>
|
||||
</div>
|
||||
{options.map((opt, i) => (
|
||||
<div key={i} className="flex items-center gap-2">
|
||||
<div
|
||||
key={i}
|
||||
className="flex items-start gap-2 rounded-lg border border-border p-2"
|
||||
>
|
||||
<div className="flex flex-1 flex-col gap-2">
|
||||
<Input
|
||||
value={opt}
|
||||
onChange={(e) => setOptionAt(i, e.target.value)}
|
||||
value={opt.label}
|
||||
onChange={(e) => setOptionAt(i, { label: e.target.value })}
|
||||
placeholder={`گزینه ${toFa(i + 1)}`}
|
||||
/>
|
||||
<Input
|
||||
value={opt.tags}
|
||||
onChange={(e) => setOptionAt(i, { tags: e.target.value })}
|
||||
placeholder="برچسبها (با کاما جدا کنید) — برای پیشنهاد محتوا"
|
||||
/>
|
||||
</div>
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
|
||||
+39
-1
@@ -8,7 +8,7 @@ import {
|
||||
type TextareaHTMLAttributes,
|
||||
useEffect,
|
||||
} from "react";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { cn, toFa } from "@/lib/utils";
|
||||
import { CloseIcon, SpinnerIcon } from "./icons";
|
||||
|
||||
/* ------------------------------- Button -------------------------------- */
|
||||
@@ -192,6 +192,44 @@ export function Badge({
|
||||
);
|
||||
}
|
||||
|
||||
export function Pagination({
|
||||
page,
|
||||
lastPage,
|
||||
total,
|
||||
onChange,
|
||||
}: {
|
||||
page: number;
|
||||
lastPage: number;
|
||||
total?: number;
|
||||
onChange: (p: number) => void;
|
||||
}) {
|
||||
if (lastPage <= 1) return null;
|
||||
return (
|
||||
<div className="mt-4 flex items-center justify-between gap-3 text-sm">
|
||||
<span className="text-muted">
|
||||
{total != null && `${toFa(total)} مورد · `}
|
||||
صفحه {toFa(page)} از {toFa(lastPage)}
|
||||
</span>
|
||||
<div className="flex items-center gap-2">
|
||||
<Button
|
||||
variant="secondary"
|
||||
onClick={() => onChange(page - 1)}
|
||||
disabled={page <= 1}
|
||||
>
|
||||
قبلی
|
||||
</Button>
|
||||
<Button
|
||||
variant="secondary"
|
||||
onClick={() => onChange(page + 1)}
|
||||
disabled={page >= lastPage}
|
||||
>
|
||||
بعدی
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function EmptyState({
|
||||
title,
|
||||
message,
|
||||
|
||||
@@ -51,6 +51,73 @@ export function useList<T = unknown>(
|
||||
return { data, loading, error, reload };
|
||||
}
|
||||
|
||||
export interface PageMeta {
|
||||
current_page: number;
|
||||
last_page: number;
|
||||
per_page: number;
|
||||
total: number;
|
||||
}
|
||||
|
||||
// Paginated GET-list hook for Laravel paginator responses
|
||||
// ({ data, current_page, last_page, per_page, total }).
|
||||
export function usePaginated<T = unknown>(
|
||||
path: string | null,
|
||||
options?: { perPage?: number; query?: Query },
|
||||
): {
|
||||
data: T[];
|
||||
meta: PageMeta | null;
|
||||
page: number;
|
||||
setPage: (p: number) => void;
|
||||
loading: boolean;
|
||||
error: string | null;
|
||||
reload: () => void;
|
||||
} {
|
||||
const perPage = options?.perPage ?? 20;
|
||||
const query = options?.query;
|
||||
const [page, setPage] = useState(1);
|
||||
const [data, setData] = useState<T[]>([]);
|
||||
const [meta, setMeta] = useState<PageMeta | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const queryKey = JSON.stringify(query ?? {});
|
||||
|
||||
const reload = useCallback(() => {
|
||||
if (!path) {
|
||||
setData([]);
|
||||
setMeta(null);
|
||||
setLoading(false);
|
||||
setError(null);
|
||||
return;
|
||||
}
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
apiFetch(path, { query: { ...query, page, per_page: perPage } })
|
||||
.then((res) => {
|
||||
const r = (res ?? {}) as Record<string, unknown>;
|
||||
const rows = Array.isArray(r.data) ? (r.data as T[]) : [];
|
||||
setData(rows);
|
||||
setMeta({
|
||||
current_page: Number(r.current_page ?? page),
|
||||
last_page: Number(r.last_page ?? 1),
|
||||
per_page: Number(r.per_page ?? perPage),
|
||||
total: Number(r.total ?? rows.length),
|
||||
});
|
||||
})
|
||||
.catch((e: unknown) => {
|
||||
setError(e instanceof ApiError ? e.message : "خطا در دریافت اطلاعات");
|
||||
setData([]);
|
||||
})
|
||||
.finally(() => setLoading(false));
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [path, page, perPage, queryKey]);
|
||||
|
||||
useEffect(() => {
|
||||
reload();
|
||||
}, [reload]);
|
||||
|
||||
return { data, meta, page, setPage, loading, error, reload };
|
||||
}
|
||||
|
||||
// Single-record GET hook.
|
||||
export function useItem<T = unknown>(path: string | null): {
|
||||
data: T | null;
|
||||
|
||||
Reference in New Issue
Block a user