sozsoft-platform/ui/src/views/report/reportRouteParams.ts

58 lines
1.4 KiB
TypeScript
Raw Normal View History

export type ReportRouteParams = {
report?: string
id?: string
listFormCode?: string
}
const isDynamicGridReport = (report?: string) =>
2026-06-17 19:55:23 +00:00
report === 'DynamicGrid' ||
report === 'DynamicGridReport' ||
report === 'DynamicTree' ||
report === 'DynamicTreeReport'
export const normalizeReportRouteParams = ({
report,
id,
listFormCode,
}: ReportRouteParams) => {
if (isDynamicGridReport(report) && id && !listFormCode) {
return {
report,
id: undefined,
listFormCode: id,
}
}
return {
report,
id,
listFormCode,
}
}
export const createReportQueryString = (searchParams: URLSearchParams) =>
Array.from(searchParams.entries())
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
.join('&')
export const createReportUrl = (
params: ReportRouteParams,
search: string,
action = 'view',
) => {
const { report, id, listFormCode } = normalizeReportRouteParams(params)
if (!report) return ''
const searchParams = new URLSearchParams(search)
searchParams.delete('id')
searchParams.delete('listFormCode')
const pathSegments = [report, action]
if (id) pathSegments.push(id)
if (listFormCode) pathSegments.push(listFormCode)
const path = pathSegments.map((segment) => encodeURIComponent(segment)).join('/')
const queryString = createReportQueryString(searchParams)
return queryString ? `${path}?${queryString}` : path
}