Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

customize export columns #16

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions src/export.handler.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
import { Parsers } from './parsers';
import { getRecords } from './utils';
import { ActionHandler, ActionResponse } from 'adminjs';
import { formatRecords } from './formater.handler';
import { Column } from './formater.type';

export const exportHandler: ActionHandler<ActionResponse> = async (
request,
response,
context
) => {
const parser = Parsers[request.query?.type ?? 'json'].export;
export const exportHandlerFactory = (columns?: Column[]) => {
const exportHandler: ActionHandler<ActionResponse> = async (
request,
response,
context
) => {
const parser = Parsers[request.query?.type ?? 'json'].export;

const records = await getRecords(context);
const parsedData = parser(records);
const records = await getRecords(context);
const parsedData = parser(
columns ? formatRecords(records, columns) : records
);

return {
exportedData: parsedData,
return {
exportedData: parsedData,
};
};

return exportHandler;
};
45 changes: 45 additions & 0 deletions src/formater.handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { BaseRecord } from 'adminjs';
import { Column } from './formater.type';

const formatKey = (record: BaseRecord, column: Column) => {
if (!column.key) return null;
return column.callback
? column.callback(record.params[column.key])
: record.params[column.key];
};

const formatConcatKeys = (record: BaseRecord, column: Column) => {
if (!column.concat?.keys) return null;
return column.concat.keys
.map(key => record.params[key])
.join(column.concat.separator);
};

const formatConcatKey = (record: BaseRecord, column: Column) => {
if (!column.concat?.key) return null;
let index = 0;
const array: string[] = [];
while (!!record.params[`${column.concat.key}.${index}`]) {
array.push(record.params[`${column.concat.key}.${index}`]);
index++;
}
return array.join(column.concat.separator);
};

export const formatRecords = (records: BaseRecord[], columns: Column[]) => {
const formattedRecords = records.map(record => {
const formattedParams = {};
columns.forEach(column => {
if (column.key) formattedParams[column.name] = formatKey(record, column);
if (column.value) formattedParams[column.name] = column.value;
if (column.concat?.keys)
formattedParams[column.name] = formatConcatKeys(record, column);
if (column.concat?.key)
formattedParams[column.name] = formatConcatKey(record, column);
});

return { params: formattedParams };
});

return formattedRecords;
};
11 changes: 11 additions & 0 deletions src/formater.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export interface Column {
name: string;
key?: string;
callback?: (value: string) => string;
value?: string;
concat?: {
key?: string;
keys?: string[];
separator: string;
};
}
37 changes: 23 additions & 14 deletions src/importExportFeature.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
import * as _ from 'lodash';
import { buildFeature, FeatureType } from 'adminjs';
import { bundleComponents } from './components/bundleComponents';
import { postActionHandler } from './utils';
import { exportHandler } from './export.handler';
import { exportHandlerFactory } from './export.handler';
import { importHandler } from './import.handler';
import { Options } from './options.type';
import { defaultOptions } from './options.default';

const { EXPORT_COMPONENT, IMPORT_COMPONENT } = bundleComponents();

const importExportFeature = (): FeatureType => {
const importExportFeature = (options: Options): FeatureType => {
const appOptions = _.merge({}, defaultOptions, options);
const actions = {};
if (appOptions.export?.isVisible)
actions['export'] = {
handler: postActionHandler(
exportHandlerFactory(appOptions.export.columns)
),
component: EXPORT_COMPONENT,
actionType: 'resource',
};
if (appOptions.import?.isVisible)
actions['import'] = {
handler: postActionHandler(importHandler),
component: IMPORT_COMPONENT,
actionType: 'resource',
};

return buildFeature({
actions: {
export: {
handler: postActionHandler(exportHandler),
component: EXPORT_COMPONENT,
actionType: 'resource',
},
import: {
handler: postActionHandler(importHandler),
component: IMPORT_COMPONENT,
actionType: 'resource',
},
},
actions,
});
};

Expand Down
10 changes: 10 additions & 0 deletions src/options.default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Options } from './options.type';

export const defaultOptions: Options = {
export: {
isVisible: true,
},
import: {
isVisible: true,
},
};
11 changes: 11 additions & 0 deletions src/options.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Column } from './formater.type';

export interface Options {
export?: {
isVisible?: boolean;
columns?: Column[];
};
import?: {
isVisible?: boolean;
};
}