Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
60a054710a |
@@ -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