Skip to content

Commit

Permalink
Add flatten util for objects and used it for CSV exported data (han…
Browse files Browse the repository at this point in the history
…dle nested objects)
  • Loading branch information
vikavorkin committed Nov 14, 2023
1 parent b93716b commit f8f7053
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
6 changes: 4 additions & 2 deletions client/src/pages/filaments/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
} from "../../components/otherModels";
import { useLiveify } from "../../components/liveify";
import { removeUndefined } from "../../utils/filtering";
import {IVendor} from "../vendors/model";
import { flatten } from "../../utils/objects";

dayjs.extend(utc);

Expand Down Expand Up @@ -76,7 +76,9 @@ export const FilamentList: React.FC<IResourceComponentsProps> = () => {
// Load initial state
const initialState = useInitialTableState(namespace);

const { triggerExport, isLoading } = useExport<IVendor>();
const { triggerExport, isLoading } = useExport<IFilament>({
mapData: item => flatten(item)
});

// Fetch data from the API
// To provide the live updates, we use a custom solution (useLiveify) instead of the built-in refine "liveMode" feature.
Expand Down
7 changes: 6 additions & 1 deletion client/src/pages/spools/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
} from "../../components/otherModels";
import { useLiveify } from "../../components/liveify";
import { removeUndefined } from "../../utils/filtering";
import { flatten } from "../../utils/objects";

dayjs.extend(utc);

Expand Down Expand Up @@ -98,7 +99,11 @@ export const SpoolList: React.FC<IResourceComponentsProps> = () => {
// State for the switch to show archived spools
const [showArchived, setShowArchived] = useSavedState("spoolList-showArchived", false);

const { triggerExport, isLoading } = useExport<ISpoolCollapsed>();
const { triggerExport, isLoading } = useExport<ISpool>(
{
mapData: item => flatten(item)
}
);

// Fetch data from the API
// To provide the live updates, we use a custom solution (useLiveify) instead of the built-in refine "liveMode" feature.
Expand Down
5 changes: 4 additions & 1 deletion client/src/pages/vendors/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { EditOutlined, EyeOutlined, FilterOutlined, PlusSquareOutlined } from "@
import { DateColumn, RichColumn, SortedColumn, ActionsColumn } from "../../components/column";
import { useLiveify } from "../../components/liveify";
import { removeUndefined } from "../../utils/filtering";
import { flatten } from "../../utils/objects";

dayjs.extend(utc);

Expand All @@ -24,7 +25,9 @@ export const VendorList: React.FC<IResourceComponentsProps> = () => {
// Load initial state
const initialState = useInitialTableState(namespace);

const { triggerExport, isLoading } = useExport<IVendor>();
const { triggerExport, isLoading } = useExport<IVendor>({
mapData: item => flatten(item)
});

// Fetch data from the API
const { tableProps, sorters, setSorters, filters, setFilters, current, pageSize, setCurrent } = useTable<IVendor>({
Expand Down
13 changes: 13 additions & 0 deletions client/src/utils/objects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const flatten = (data: object, prefix: string = '', delimiter: string = '.') => {
const result: { [key: string]: string | number | null } = {};

Object.entries(data).forEach(([key, value]) => {
if (typeof value === 'object') {
Object.assign(result, flatten(value, `${prefix}${key}${delimiter}`, delimiter));
} else {
result[`${prefix}${key}`] = value;
}
});

return result;
};

0 comments on commit f8f7053

Please sign in to comment.