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

Filters fix #24

Closed
wants to merge 7 commits into from
Closed
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
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ dist
.DS_store

# build files
/build
/lib
/types
.adminjs
example-app/.adminjs
155 changes: 0 additions & 155 deletions example-app/.gitignore

This file was deleted.

69 changes: 69 additions & 0 deletions lib/components/ExportComponent.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getExportedFileName = exports.mimeTypes = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importStar(require("react"));
const adminjs_1 = require("adminjs");
const design_system_1 = require("@adminjs/design-system");
const file_saver_1 = require("file-saver");
const exporter_type_1 = require("../exporter.type");
const format_1 = tslib_1.__importDefault(require("date-fns/format"));
exports.mimeTypes = {
json: 'application/json',
csv: 'text/csv',
xml: 'text/xml',
};
const getExportedFileName = (extension) => `export-${(0, format_1.default)(Date.now(), 'yyyy-MM-dd_HH-mm')}.${extension}`;
exports.getExportedFileName = getExportedFileName;
const ExportComponent = ({ resource }) => {
var _a;
const filter = {};
const prevPage = JSON.parse((_a = localStorage.getItem("prevPage")) !== null && _a !== void 0 ? _a : "{}");
const prevFilter = new URLSearchParams(prevPage.search);
let query = prevFilter !== null && prevFilter !== void 0 ? prevFilter : new URLSearchParams(location.search);
for (const entry of query.entries()) {
const [key, value] = entry;
if (key.match('filters.')) {
filter[key.replace('filters.', '')] = value;
}
}
const [isFetching, setFetching] = (0, react_1.useState)();
const sendNotice = (0, adminjs_1.useNotice)();
const exportData = async (type) => {
setFetching(true);
try {
const { data: { exportedData }, } = await new adminjs_1.ApiClient().resourceAction({
method: 'post',
resourceId: resource.id,
actionName: 'export',
params: {
type,
filter,
},
});
const blob = new Blob([exportedData], { type: exports.mimeTypes[type] });
(0, file_saver_1.saveAs)(blob, (0, exports.getExportedFileName)(type));
sendNotice({ message: 'Exported successfully', type: 'success' });
}
catch (e) {
sendNotice({ message: e.message, type: 'error' });
}
setFetching(false);
};
if (isFetching) {
return <design_system_1.Loader />;
}
return (<design_system_1.Box>
<design_system_1.Box display="flex" justifyContent="center">
<design_system_1.Text variant="lg">Choose export format:</design_system_1.Text>
</design_system_1.Box>
<design_system_1.Box display="flex" justifyContent="center">
{exporter_type_1.Exporters.map(parserType => (<design_system_1.Box key={parserType} m={2}>
<design_system_1.Button onClick={() => exportData(parserType)} disabled={isFetching}>
{parserType.toUpperCase()}
</design_system_1.Button>
</design_system_1.Box>))}
</design_system_1.Box>
</design_system_1.Box>);
};
exports.default = ExportComponent;
49 changes: 49 additions & 0 deletions lib/components/ImportComponent.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importStar(require("react"));
const adminjs_1 = require("adminjs");
const design_system_1 = require("@adminjs/design-system");
const ImportComponent = ({ resource }) => {
const [file, setFile] = (0, react_1.useState)(null);
const sendNotice = (0, adminjs_1.useNotice)();
const [isFetching, setFetching] = (0, react_1.useState)();
const onUpload = (uploadedFile) => {
var _a;
setFile((_a = uploadedFile === null || uploadedFile === void 0 ? void 0 : uploadedFile[0]) !== null && _a !== void 0 ? _a : null);
};
const onSubmit = async () => {
if (!file) {
return;
}
setFetching(true);
try {
const importData = new FormData();
importData.append('file', file, file === null || file === void 0 ? void 0 : file.name);
await new adminjs_1.ApiClient().resourceAction({
method: 'post',
resourceId: resource.id,
actionName: 'import',
data: importData,
});
sendNotice({ message: 'Imported successfully', type: 'success' });
}
catch (e) {
sendNotice({ message: e.message, type: 'error' });
}
setFetching(false);
};
if (isFetching) {
return <design_system_1.Loader />;
}
return (<design_system_1.Box margin="auto" maxWidth={600} display="flex" justifyContent="center" flexDirection="column">
<design_system_1.DropZone files={[]} onChange={onUpload} multiple={false}/>
{file && (<design_system_1.DropZoneItem file={file} filename={file.name} onRemove={() => setFile(null)}/>)}
<design_system_1.Box display="flex" justifyContent="center" m={10}>
<design_system_1.Button onClick={onSubmit} disabled={!file || isFetching}>
Upload
</design_system_1.Button>
</design_system_1.Box>
</design_system_1.Box>);
};
exports.default = ImportComponent;
11 changes: 11 additions & 0 deletions lib/components/bundleComponents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.bundleComponents = void 0;
const tslib_1 = require("tslib");
const adminjs_1 = tslib_1.__importDefault(require("adminjs"));
const bundleComponents = () => {
const EXPORT_COMPONENT = adminjs_1.default.bundle('../../src/components/ExportComponent');
const IMPORT_COMPONENT = adminjs_1.default.bundle('../../src/components/ImportComponent');
return { EXPORT_COMPONENT, IMPORT_COMPONENT };
};
exports.bundleComponents = bundleComponents;
15 changes: 15 additions & 0 deletions lib/export.handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.exportHandler = void 0;
const parsers_1 = require("./parsers");
const utils_1 = require("./utils");
const exportHandler = async (request, response, context) => {
var _a, _b;
const parser = parsers_1.Parsers[(_b = (_a = request.query) === null || _a === void 0 ? void 0 : _a.type) !== null && _b !== void 0 ? _b : 'json'].export;
const records = await (0, utils_1.getRecords)(context, request);
const parsedData = parser(records);
return {
exportedData: parsedData,
};
};
exports.exportHandler = exportHandler;
4 changes: 4 additions & 0 deletions lib/exporter.type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Exporters = void 0;
exports.Exporters = ['csv', 'json', 'xml'];
16 changes: 16 additions & 0 deletions lib/import.handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.importHandler = void 0;
const tslib_1 = require("tslib");
const fs_1 = tslib_1.__importDefault(require("fs"));
const util_1 = tslib_1.__importDefault(require("util"));
const utils_1 = require("./utils");
const readFile = util_1.default.promisify(fs_1.default.readFile);
const importHandler = async (request, response, context) => {
const file = (0, utils_1.getFileFromRequest)(request);
const importer = (0, utils_1.getImporterByFileName)(file.name);
const fileContent = await readFile(file.path);
await importer(fileContent.toString(), context.resource);
return {};
};
exports.importHandler = importHandler;
27 changes: 27 additions & 0 deletions lib/importExportFeature.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const adminjs_1 = require("adminjs");
const bundleComponents_1 = require("./components/bundleComponents");
const utils_1 = require("./utils");
const export_handler_1 = require("./export.handler");
const import_handler_1 = require("./import.handler");
const { EXPORT_COMPONENT, IMPORT_COMPONENT } = (0, bundleComponents_1.bundleComponents)();
const importExportFeature = () => {
return (0, adminjs_1.buildFeature)({
actions: {
export: {
handler: (0, utils_1.postActionHandler)(export_handler_1.exportHandler),
component: EXPORT_COMPONENT,
actionType: 'resource',
showFilter: true,
},
import: {
handler: (0, utils_1.postActionHandler)(import_handler_1.importHandler),
component: IMPORT_COMPONENT,
actionType: 'resource',
showFilter: true,
},
},
});
};
exports.default = importExportFeature;
11 changes: 11 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"use strict";
/**
* @module @adminjs/import-export
* @subcategory Features
* @section modules
*/
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const importExportFeature_1 = tslib_1.__importDefault(require("./importExportFeature"));
tslib_1.__exportStar(require("./components/bundleComponents"), exports);
exports.default = importExportFeature_1.default;
8 changes: 8 additions & 0 deletions lib/modules/csv/csv.exporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.csvExporter = void 0;
const json2csv_1 = require("json2csv");
const csvExporter = (records) => {
return (0, json2csv_1.parse)(records.map(r => r.params));
};
exports.csvExporter = csvExporter;
11 changes: 11 additions & 0 deletions lib/modules/csv/csv.importer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.csvImporter = void 0;
const tslib_1 = require("tslib");
const csvtojson_1 = tslib_1.__importDefault(require("csvtojson"));
const utils_1 = require("../../utils");
const csvImporter = async (csvString, resource) => {
const records = await (0, csvtojson_1.default)().fromString(csvString);
return (0, utils_1.saveRecords)(records, resource);
};
exports.csvImporter = csvImporter;
Loading