Files
rowy/src/components/fields/SubTable/utils.ts

36 lines
1021 B
TypeScript
Raw Normal View History

import { useLocation } from "react-router-dom";
2022-05-24 20:34:28 +10:00
import { ROUTES } from "@src/constants/routes";
import { ColumnConfig, TableRow, TableRowRef } from "@src/types/table";
2023-04-20 15:45:14 +02:00
import get from "lodash-es/get";
2022-05-24 20:34:28 +10:00
export const useSubTableData = (
column: ColumnConfig,
row: TableRow,
2022-11-15 16:53:20 +11:00
_rowy_ref: TableRowRef
2022-05-24 20:34:28 +10:00
) => {
const label = (column.config?.parentLabel ?? []).reduce((acc, curr) => {
2023-04-20 15:45:14 +02:00
if (acc !== "") return `${acc} - ${get(row, curr)}`;
else return get(row, curr);
2022-05-24 20:34:28 +10:00
}, "");
const documentCount: string = row[column.fieldName]?.count ?? "";
2022-05-24 20:34:28 +10:00
const location = useLocation();
const rootTablePath = decodeURIComponent(
location.pathname.split("/" + ROUTES.subTable)[0]
2022-05-24 20:34:28 +10:00
);
2022-10-12 10:45:18 +11:00
// Get params from URL: /table/:tableId/subTable/:docPath/:subTableKey
let subTablePath = [
rootTablePath,
ROUTES.subTable,
2022-11-15 16:53:20 +11:00
encodeURIComponent(_rowy_ref.path),
column.key,
].join("/");
2022-05-24 20:34:28 +10:00
2022-06-09 15:55:47 +10:00
subTablePath += "?parentLabel=" + encodeURIComponent(label ?? "");
2022-05-24 20:34:28 +10:00
return { documentCount, label, subTablePath };
};