Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
04c9ff5fac | ||
|
|
60a054710a | ||
|
|
1b2d2dec7d | ||
|
|
e374829358 |
@@ -34,6 +34,8 @@ interface Media {
|
|||||||
caption?: string;
|
caption?: string;
|
||||||
// The API returns a `categories` array (many-to-many).
|
// The API returns a `categories` array (many-to-many).
|
||||||
categories?: Category[];
|
categories?: Category[];
|
||||||
|
// The API returns `sub_categories` (snake_case); keep the other casings as fallbacks.
|
||||||
|
sub_categories?: Category[];
|
||||||
subcategories?: Category[];
|
subcategories?: Category[];
|
||||||
subCategories?: Category[];
|
subCategories?: Category[];
|
||||||
type?: string;
|
type?: string;
|
||||||
@@ -142,7 +144,9 @@ export default function MediaPage() {
|
|||||||
setCaption(row.caption ?? "");
|
setCaption(row.caption ?? "");
|
||||||
setCategoryIds((row.categories ?? []).map((c) => c.id));
|
setCategoryIds((row.categories ?? []).map((c) => c.id));
|
||||||
setSubcategoryIds(
|
setSubcategoryIds(
|
||||||
(row.subcategories ?? row.subCategories ?? []).map((c) => c.id),
|
(row.sub_categories ?? row.subcategories ?? row.subCategories ?? []).map(
|
||||||
|
(c) => c.id,
|
||||||
|
),
|
||||||
);
|
);
|
||||||
setType(row.type ?? "audio");
|
setType(row.type ?? "audio");
|
||||||
setDuration(row.duration != null ? String(row.duration) : "");
|
setDuration(row.duration != null ? String(row.duration) : "");
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { apiFetch, ApiError, toFormData } from "@/lib/api";
|
import { apiFetch, ApiError, toFormData } from "@/lib/api";
|
||||||
import { useList } from "@/lib/useResource";
|
import { useList, usePaginated } from "@/lib/useResource";
|
||||||
import { useToast } from "@/components/toast";
|
import { useToast } from "@/components/toast";
|
||||||
import { DataTable, type Column } from "@/components/DataTable";
|
import { DataTable, type Column } from "@/components/DataTable";
|
||||||
import {
|
import {
|
||||||
@@ -13,6 +13,7 @@ import {
|
|||||||
Select,
|
Select,
|
||||||
Modal,
|
Modal,
|
||||||
PageHeader,
|
PageHeader,
|
||||||
|
Pagination,
|
||||||
} from "@/components/ui";
|
} from "@/components/ui";
|
||||||
import { EditIcon, PlusIcon, TrashIcon } from "@/components/icons";
|
import { EditIcon, PlusIcon, TrashIcon } from "@/components/icons";
|
||||||
import { formatMinutes, fileNameToTitle } from "@/lib/utils";
|
import { formatMinutes, fileNameToTitle } from "@/lib/utils";
|
||||||
@@ -20,6 +21,14 @@ import { MediaPreview } from "@/components/MediaPreview";
|
|||||||
import { ImagePicker } from "@/components/ImagePicker";
|
import { ImagePicker } from "@/components/ImagePicker";
|
||||||
import { pickUrl, SOUND_KEYS, IMAGE_KEYS } from "@/lib/media";
|
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 {
|
interface Track {
|
||||||
id: number;
|
id: number;
|
||||||
title?: string;
|
title?: string;
|
||||||
@@ -28,15 +37,17 @@ interface Track {
|
|||||||
type?: string;
|
type?: string;
|
||||||
playlist_id?: number;
|
playlist_id?: number;
|
||||||
image_id?: number | null;
|
image_id?: number | null;
|
||||||
}
|
// A track belongs to many playlists; the /music row embeds them as an array.
|
||||||
|
playlists?: Playlist[];
|
||||||
interface Playlist {
|
// Sub-categories may also be embedded directly on the track (key casing varies).
|
||||||
id: number;
|
subcategories?: Playlist[];
|
||||||
name?: string;
|
subCategories?: Playlist[];
|
||||||
|
sub_categories?: Playlist[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function MusicTracksPage() {
|
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 { data: playlists } = useList<Playlist>("/music-playlists");
|
||||||
const toast = useToast();
|
const toast = useToast();
|
||||||
|
|
||||||
@@ -56,6 +67,26 @@ export default function MusicTracksPage() {
|
|||||||
const [deleting, setDeleting] = useState<Track | null>(null);
|
const [deleting, setDeleting] = useState<Track | null>(null);
|
||||||
const [removing, setRemoving] = useState(false);
|
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() {
|
function resetForm() {
|
||||||
setTitle("");
|
setTitle("");
|
||||||
setArtist("");
|
setArtist("");
|
||||||
@@ -163,6 +194,16 @@ export default function MusicTracksPage() {
|
|||||||
},
|
},
|
||||||
{ key: "title", header: "عنوان" },
|
{ key: "title", header: "عنوان" },
|
||||||
{ key: "artist", header: "هنرمند", render: (r) => r.artist ?? "—" },
|
{ key: "artist", header: "هنرمند", render: (r) => r.artist ?? "—" },
|
||||||
|
{
|
||||||
|
key: "playlist",
|
||||||
|
header: "پلیلیست",
|
||||||
|
render: (r) => playlistName(r),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "subcategory",
|
||||||
|
header: "زیردسته",
|
||||||
|
render: (r) => subCategoryNames(r),
|
||||||
|
},
|
||||||
{
|
{
|
||||||
key: "duration",
|
key: "duration",
|
||||||
header: "مدت زمان",
|
header: "مدت زمان",
|
||||||
@@ -217,6 +258,15 @@ export default function MusicTracksPage() {
|
|||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
{meta && (
|
||||||
|
<Pagination
|
||||||
|
page={page}
|
||||||
|
lastPage={meta.last_page}
|
||||||
|
total={meta.total}
|
||||||
|
onChange={setPage}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
<Modal
|
<Modal
|
||||||
open={open}
|
open={open}
|
||||||
onClose={() => setOpen(false)}
|
onClose={() => setOpen(false)}
|
||||||
|
|||||||
@@ -20,9 +20,21 @@ import {
|
|||||||
import { EditIcon, PlusIcon, TrashIcon } from "@/components/icons";
|
import { EditIcon, PlusIcon, TrashIcon } from "@/components/icons";
|
||||||
import { toFa } from "@/lib/utils";
|
import { toFa } from "@/lib/utils";
|
||||||
|
|
||||||
|
interface SurveyTag {
|
||||||
|
id: number;
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
|
|
||||||
interface SurveyOption {
|
interface SurveyOption {
|
||||||
id?: number;
|
id?: number;
|
||||||
label?: string;
|
label?: string;
|
||||||
|
tags?: SurveyTag[];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Form-side option: tags edited as a comma-separated string.
|
||||||
|
interface OptionDraft {
|
||||||
|
label: string;
|
||||||
|
tags: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface SurveyQuestion {
|
interface SurveyQuestion {
|
||||||
@@ -54,7 +66,9 @@ export default function SurveysPage() {
|
|||||||
const [type, setType] = useState("single");
|
const [type, setType] = useState("single");
|
||||||
const [order, setOrder] = useState("0");
|
const [order, setOrder] = useState("0");
|
||||||
const [isActive, setIsActive] = useState(true);
|
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 [deleting, setDeleting] = useState<SurveyQuestion | null>(null);
|
||||||
const [removing, setRemoving] = useState(false);
|
const [removing, setRemoving] = useState(false);
|
||||||
@@ -66,7 +80,7 @@ export default function SurveysPage() {
|
|||||||
setType("single");
|
setType("single");
|
||||||
setOrder("0");
|
setOrder("0");
|
||||||
setIsActive(true);
|
setIsActive(true);
|
||||||
setOptions([""]);
|
setOptions([{ label: "", tags: "" }]);
|
||||||
setOpen(true);
|
setOpen(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,17 +93,22 @@ export default function SurveysPage() {
|
|||||||
setIsActive(row.is_active ?? true);
|
setIsActive(row.is_active ?? true);
|
||||||
setOptions(
|
setOptions(
|
||||||
row.options && row.options.length
|
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);
|
setOpen(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
function setOptionAt(index: number, value: string) {
|
function setOptionAt(index: number, patch: Partial<OptionDraft>) {
|
||||||
setOptions((prev) => prev.map((o, i) => (i === index ? value : o)));
|
setOptions((prev) =>
|
||||||
|
prev.map((o, i) => (i === index ? { ...o, ...patch } : o)),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
function addOption() {
|
function addOption() {
|
||||||
setOptions((prev) => [...prev, ""]);
|
setOptions((prev) => [...prev, { label: "", tags: "" }]);
|
||||||
}
|
}
|
||||||
function removeOption(index: number) {
|
function removeOption(index: number) {
|
||||||
setOptions((prev) =>
|
setOptions((prev) =>
|
||||||
@@ -108,9 +127,15 @@ export default function SurveysPage() {
|
|||||||
order: Number(order),
|
order: Number(order),
|
||||||
is_active: isActive,
|
is_active: isActive,
|
||||||
options: options
|
options: options
|
||||||
.map((label) => label.trim())
|
.map((o) => ({ label: o.label.trim(), tagsRaw: o.tags }))
|
||||||
.filter((label) => label !== "")
|
.filter((o) => o.label !== "")
|
||||||
.map((label) => ({ label })),
|
.map((o) => ({
|
||||||
|
label: o.label,
|
||||||
|
tags: o.tagsRaw
|
||||||
|
.split(/[,،]/)
|
||||||
|
.map((t) => t.trim())
|
||||||
|
.filter((t) => t !== ""),
|
||||||
|
})),
|
||||||
};
|
};
|
||||||
if (editing) {
|
if (editing) {
|
||||||
await apiFetch(`/survey-questions/${editing.id}`, {
|
await apiFetch(`/survey-questions/${editing.id}`, {
|
||||||
@@ -283,12 +308,22 @@ export default function SurveysPage() {
|
|||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
{options.map((opt, i) => (
|
{options.map((opt, i) => (
|
||||||
<div key={i} className="flex items-center gap-2">
|
<div
|
||||||
<Input
|
key={i}
|
||||||
value={opt}
|
className="flex items-start gap-2 rounded-lg border border-border p-2"
|
||||||
onChange={(e) => setOptionAt(i, e.target.value)}
|
>
|
||||||
placeholder={`گزینه ${toFa(i + 1)}`}
|
<div className="flex flex-1 flex-col gap-2">
|
||||||
/>
|
<Input
|
||||||
|
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
|
<Button
|
||||||
type="button"
|
type="button"
|
||||||
variant="ghost"
|
variant="ghost"
|
||||||
|
|||||||
+39
-1
@@ -8,7 +8,7 @@ import {
|
|||||||
type TextareaHTMLAttributes,
|
type TextareaHTMLAttributes,
|
||||||
useEffect,
|
useEffect,
|
||||||
} from "react";
|
} from "react";
|
||||||
import { cn } from "@/lib/utils";
|
import { cn, toFa } from "@/lib/utils";
|
||||||
import { CloseIcon, SpinnerIcon } from "./icons";
|
import { CloseIcon, SpinnerIcon } from "./icons";
|
||||||
|
|
||||||
/* ------------------------------- Button -------------------------------- */
|
/* ------------------------------- 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({
|
export function EmptyState({
|
||||||
title,
|
title,
|
||||||
message,
|
message,
|
||||||
|
|||||||
@@ -51,6 +51,73 @@ export function useList<T = unknown>(
|
|||||||
return { data, loading, error, reload };
|
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.
|
// Single-record GET hook.
|
||||||
export function useItem<T = unknown>(path: string | null): {
|
export function useItem<T = unknown>(path: string | null): {
|
||||||
data: T | null;
|
data: T | null;
|
||||||
|
|||||||
Reference in New Issue
Block a user