Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
504b1e2c8e | ||
|
|
60a054710a |
@@ -17,6 +17,9 @@ import {
|
|||||||
PageHeader,
|
PageHeader,
|
||||||
} from "@/components/ui";
|
} from "@/components/ui";
|
||||||
import { EditIcon, PlusIcon, TrashIcon } from "@/components/icons";
|
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";
|
import { toFa } from "@/lib/utils";
|
||||||
|
|
||||||
interface MusicCategory {
|
interface MusicCategory {
|
||||||
@@ -25,6 +28,8 @@ interface MusicCategory {
|
|||||||
description?: string;
|
description?: string;
|
||||||
order?: number;
|
order?: number;
|
||||||
is_active?: boolean;
|
is_active?: boolean;
|
||||||
|
image_id?: number | null;
|
||||||
|
image?: unknown;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function MusicCategoriesPage() {
|
export default function MusicCategoriesPage() {
|
||||||
@@ -39,7 +44,8 @@ export default function MusicCategoriesPage() {
|
|||||||
const [name, setName] = useState("");
|
const [name, setName] = useState("");
|
||||||
const [description, setDescription] = useState("");
|
const [description, setDescription] = useState("");
|
||||||
const [order, setOrder] = 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 [isActive, setIsActive] = useState(true);
|
||||||
|
|
||||||
const [deleting, setDeleting] = useState<MusicCategory | null>(null);
|
const [deleting, setDeleting] = useState<MusicCategory | null>(null);
|
||||||
@@ -50,7 +56,8 @@ export default function MusicCategoriesPage() {
|
|||||||
setName("");
|
setName("");
|
||||||
setDescription("");
|
setDescription("");
|
||||||
setOrder("");
|
setOrder("");
|
||||||
setImageId("");
|
setImageId(null);
|
||||||
|
setImageFile(null);
|
||||||
setIsActive(true);
|
setIsActive(true);
|
||||||
setOpen(true);
|
setOpen(true);
|
||||||
}
|
}
|
||||||
@@ -59,7 +66,8 @@ export default function MusicCategoriesPage() {
|
|||||||
setName(row.name ?? "");
|
setName(row.name ?? "");
|
||||||
setDescription(row.description ?? "");
|
setDescription(row.description ?? "");
|
||||||
setOrder(row.order != null ? String(row.order) : "");
|
setOrder(row.order != null ? String(row.order) : "");
|
||||||
setImageId("");
|
setImageId(row.image_id ?? null);
|
||||||
|
setImageFile(null);
|
||||||
setIsActive(row.is_active ?? true);
|
setIsActive(row.is_active ?? true);
|
||||||
setOpen(true);
|
setOpen(true);
|
||||||
}
|
}
|
||||||
@@ -69,15 +77,17 @@ export default function MusicCategoriesPage() {
|
|||||||
setSaving(true);
|
setSaving(true);
|
||||||
try {
|
try {
|
||||||
if (editing) {
|
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}`, {
|
await apiFetch(`/music-categories/${editing.id}`, {
|
||||||
method: "PUT",
|
method: "POST",
|
||||||
body: {
|
body: toFormData({
|
||||||
name,
|
name,
|
||||||
description,
|
description,
|
||||||
order: order === "" ? undefined : Number(order),
|
order: order === "" ? undefined : Number(order),
|
||||||
is_active: isActive,
|
is_active: isActive,
|
||||||
},
|
image_id: imageId,
|
||||||
|
image: imageFile,
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
toast.success("دستهبندی ویرایش شد.");
|
toast.success("دستهبندی ویرایش شد.");
|
||||||
} else {
|
} else {
|
||||||
@@ -88,8 +98,9 @@ export default function MusicCategoriesPage() {
|
|||||||
name,
|
name,
|
||||||
description,
|
description,
|
||||||
order,
|
order,
|
||||||
image_id: imageId,
|
|
||||||
is_active: isActive,
|
is_active: isActive,
|
||||||
|
image_id: imageId,
|
||||||
|
image: imageFile,
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
toast.success("دستهبندی افزوده شد.");
|
toast.success("دستهبندی افزوده شد.");
|
||||||
@@ -120,6 +131,14 @@ export default function MusicCategoriesPage() {
|
|||||||
|
|
||||||
const columns: Column<MusicCategory>[] = [
|
const columns: Column<MusicCategory>[] = [
|
||||||
{ key: "id", header: "شناسه", render: (r) => toFa(r.id), className: "w-24" },
|
{ 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: "name", header: "نام" },
|
||||||
{
|
{
|
||||||
key: "description",
|
key: "description",
|
||||||
@@ -223,16 +242,15 @@ export default function MusicCategoriesPage() {
|
|||||||
dir="ltr"
|
dir="ltr"
|
||||||
/>
|
/>
|
||||||
</Field>
|
</Field>
|
||||||
{!editing && (
|
<Field label="تصویر">
|
||||||
<Field label="شناسه تصویر">
|
<ImagePicker
|
||||||
<Input
|
imageId={imageId}
|
||||||
value={imageId}
|
onPickId={setImageId}
|
||||||
onChange={(e) => setImageId(e.target.value)}
|
file={imageFile}
|
||||||
dir="ltr"
|
onPickFile={setImageFile}
|
||||||
placeholder="image_id"
|
existingUrl={editing ? pickUrl(editing, IMAGE_KEYS) : null}
|
||||||
/>
|
/>
|
||||||
</Field>
|
</Field>
|
||||||
)}
|
|
||||||
<Switch checked={isActive} onChange={setIsActive} label="فعال" />
|
<Switch checked={isActive} onChange={setIsActive} label="فعال" />
|
||||||
</form>
|
</form>
|
||||||
</Modal>
|
</Modal>
|
||||||
|
|||||||
@@ -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";
|
||||||
@@ -45,7 +46,8 @@ interface Track {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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();
|
||||||
|
|
||||||
@@ -256,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)}
|
||||||
|
|||||||
+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