admin/components/paginated-list/paginatedListApi.ts
alisaza e1eaf5eff5 feat: initial ghabilee-admin backoffice app
Extract admin dashboard from ghabilee-frontend2 into a dedicated Next.js
app for backoffice.ghabilee.ir (no SEO indexing / Clarity).
2026-09-05 13:12:59 +03:30

50 lines
1.4 KiB
TypeScript

import qs from 'qs'
import { type PaginatedListRow, withRowKeys } from '@/components/paginated-list/paginatedListUtils'
import axiosInstance from '@/config/axios'
import { extractExportFileData, getItemsKeyFromUrl, parseRemittanceList } from '@/helpers'
const paramsSerializer = (params: Record<string, unknown>) => qs.stringify(params, { arrayFormat: 'brackets' })
export async function fetchPaginatedList({
itemsKey,
page,
params,
rowsPerPage,
signal,
url,
}: {
itemsKey?: string
page: number
params: Record<string, unknown>
rowsPerPage: number
signal?: AbortSignal
url: string
}) {
const response = await axiosInstance.get(url, { params, paramsSerializer, signal })
const { items, pagination } = parseRemittanceList(
response.data as Record<string, unknown>,
itemsKey ?? getItemsKeyFromUrl(url),
page,
rowsPerPage
)
return {
items: withRowKeys(items as PaginatedListRow[]),
pagination: {
page: Number(pagination.page),
pageSize: Number(pagination.pageSize),
totalItems: Number(pagination.totalItemsCount),
},
}
}
export async function fetchPaginatedListExport(url: string, params: Record<string, unknown>) {
const response = await axiosInstance.get(url, { params, paramsSerializer })
const fileData = extractExportFileData(response.data)
if (!fileData) throw new Error('Export file not found in response')
return fileData
}