diff --git a/.gitignore b/.gitignore index 4835519..5d33bf9 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ package-lock* # misc .DS_Store +.idea diff --git a/README.md b/README.md index c8405d1..c79c7bb 100644 --- a/README.md +++ b/README.md @@ -117,6 +117,8 @@ const config: ImportConfig = { disableImportNew?: boolean; // Disable "import overwrite" button disableImportOverwrite?: boolean; + // Provide metadata to dataProvider + meta?: any[]; // A function to translate the CSV rows on import preCommitCallback?: (action: "create" | "overwrite", values: any[]) => Promise; // A function to handle row errors after import @@ -150,11 +152,11 @@ const importOptions = { Your dataprovider will need to implement the `.createMany()` method in order to reduce requests to your backend. If it doesn't exist, it will fallback to calling `.create()` on all items, as shown below (same goes for `.update()`): -| Name | Special Method | Fallback Method | -| ----------------- | ------------------ | --------------- | -| Creating from CSV | .createMany() | .create() | -| Updating from CSV | .updateManyArray() | .update() | -| Checking which exist | .getMany() | .getSingle() | +| Name | Special Method | Fallback Method | +|----------------------|--------------------|-----------------| +| Creating from CSV | .createMany() | .create() | +| Updating from CSV | .updateManyArray() | .update() | +| Checking which exist | .getMany() | .getSingle() | *Note: You can disable this feature setting `disableCreateMany: true` or `disableUpdateMany: true` in the configuration.* ### Interfaces diff --git a/src/config.interface.ts b/src/config.interface.ts index bc1770f..b47d8bd 100644 --- a/src/config.interface.ts +++ b/src/config.interface.ts @@ -13,6 +13,8 @@ export interface ImportConfig { disableImportNew?: boolean; // Disable "import overwrite" button disableImportOverwrite?: boolean; + // Provide metadata to dataProvider + meta?: any[]; // A function to translate the CSV rows on import preCommitCallback?: PrecommitCallback; // A function to handle row errors after import diff --git a/src/main-csv-button.tsx b/src/main-csv-button.tsx index dd83ab8..d06f224 100644 --- a/src/main-csv-button.tsx +++ b/src/main-csv-button.tsx @@ -22,6 +22,7 @@ export const MainCsvImport = (props: any) => { const { parseConfig, + meta, preCommitCallback, postCommitCallback, validateRow, @@ -108,7 +109,7 @@ export const MainCsvImport = (props: any) => { const collidingIdsAsNumbers = collidingIds.map((id) => parseFloat(id+'')); const allCollidingIdsAreNumbers = collidingIdsAsNumbers.every(id => isFinite(id)); if (allCollidingIdsAreNumbers) { - collidingIdsAsNumbers.map(id => collidingIdsNumbersSet.add(id)) + collidingIdsAsNumbers.map(id => collidingIdsNumbersSet.add(id)) } function idNotInNumbersOrStrings(item: any) { const matchesIdString = collidingIdsStringsSet.has(item.id+'') @@ -124,7 +125,7 @@ export const MainCsvImport = (props: any) => { processCSV() .then(async ([csvItems, hasCollidingIds]) => { - await createRows(csvItems); + await createRows(csvItems, meta); mounted && !hasCollidingIds && handleClose(); }) .catch((error) => { @@ -148,25 +149,27 @@ export const MainCsvImport = (props: any) => { setFile(null); } - async function createRows(vals: any[]) { + async function createRows(vals: any[], meta?: any[]) { return create( logging, disableCreateMany, dataProvider, resourceName, vals, + meta, preCommitCallback, postCommitCallback ); } - async function updateRows(vals: any[]) { + async function updateRows(vals: any[], meta?: any[]) { return update( logging, disableUpdateMany, dataProvider, resourceName, vals, + meta, preCommitCallback, postCommitCallback ); @@ -200,7 +203,7 @@ export const MainCsvImport = (props: any) => { const valuesColliding = values.filter((item) => collidingIdsSet.has(item.id) ); - await updateRows(valuesColliding); + await updateRows(valuesColliding, meta); handleClose(); } catch (error) { setIsLoading(false); @@ -235,7 +238,7 @@ export const MainCsvImport = (props: any) => { const handleAskDecideReplace = async () => { logger.log("handleAskDecideReplace"); - await updateRows([currentValue]); + await updateRows([currentValue], meta); const val = nextConflicting(); if (!val) { return handleClose(); @@ -246,7 +249,7 @@ export const MainCsvImport = (props: any) => { logger.log("handleAskDecideAddAsNew"); const localCopy = Object.assign({},currentValue) delete localCopy.id; - await createRows([localCopy]); + await createRows([localCopy], meta); const val = nextConflicting(); if (!val) { return handleClose(); diff --git a/src/uploader.ts b/src/uploader.ts index 99af07e..db9efe2 100644 --- a/src/uploader.ts +++ b/src/uploader.ts @@ -10,6 +10,7 @@ export async function create( dataProvider: DataProvider, resource: string, values: any[], + meta?: any[], preCommitCallback?: PrecommitCallback, postCommitCallback?: ErrorCallback ) { @@ -21,7 +22,8 @@ export async function create( !!disableCreateMany, dataProvider, resource, - parsedValues + parsedValues, + meta ); if (postCommitCallback) { postCommitCallback(reportItems); @@ -39,6 +41,7 @@ export async function update( dataProvider: DataProvider, resource: string, values: any[], + meta?: any[], preCommitCallback?: PrecommitCallback, postCommitCallback?: ErrorCallback ) { @@ -50,7 +53,8 @@ export async function update( !!disableUpdateMany, dataProvider, resource, - parsedValues + parsedValues, + meta ); if (postCommitCallback) { postCommitCallback(reportItems); @@ -64,6 +68,7 @@ export async function update( interface ReportItem { value: any; + meta?: any[]; success: boolean; err?: any; response?: any; @@ -74,18 +79,19 @@ export async function createInDataProvider( disableCreateMany: boolean, dataProvider: DataProvider, resource: string, - values: any[] + values: any[], + meta?: any[] ): Promise { logger.setEnabled(logging); logger.log("createInDataProvider", { dataProvider, resource, values }); const reportItems: ReportItem[] = []; if (disableCreateMany) { - const items = await createInDataProviderFallback(dataProvider, resource, values); + const items = await createInDataProviderFallback(dataProvider, resource, values, meta); reportItems.push(...items); return items; } try { - const response = await dataProvider.createMany(resource, { data: values }); + const response = await dataProvider.createMany(resource, { data: values, meta: meta }); reportItems.push({ value: null, success: true, response: response }) @@ -120,13 +126,14 @@ export async function createInDataProvider( async function createInDataProviderFallback( dataProvider: DataProvider, resource: string, - values: any[] + values: any[], + meta?: any[] ): Promise { const reportItems: ReportItem[] = []; await Promise.all( values.map((value) => dataProvider - .create(resource, { data: value }) + .create(resource, { data: value, meta: meta }) .then((res) => reportItems.push({ value: value, success: true, response: res }) ) @@ -141,7 +148,8 @@ async function updateInDataProvider( disableUpdateMany: boolean, dataProvider: DataProvider, resource: string, - values: any[] + values: any[], + meta?: any[] ) { const ids = values.map((v) => v.id); logger.setEnabled(logging); @@ -149,16 +157,17 @@ async function updateInDataProvider( dataProvider, resource, values, + meta, logging, ids, }); if (disableUpdateMany) { - const items = await updateInDataProviderFallback(dataProvider, resource, values); + const items = await updateInDataProviderFallback(dataProvider, resource, values, meta); return items; } const reportItems: ReportItem[] = []; try { - const response = await dataProvider.updateManyArray(resource, { ids: ids, data: values }); + const response = await dataProvider.updateManyArray(resource, { ids: ids, data: values, meta: meta }); reportItems.push({ value: null, success: true, response: response }) @@ -193,13 +202,14 @@ async function updateInDataProvider( async function updateInDataProviderFallback( dataProvider: DataProvider, resource: string, - values: any[] + values: any[], + meta?: any[] ): Promise { const reportItems: ReportItem[] = []; await Promise.all( values.map((value) => dataProvider - .update(resource, { id: value.id, data: value, previousData: null as any }) + .update(resource, { id: value.id, data: value, previousData: null as any, meta: meta }) .then((res) => reportItems.push({ value: value, success: true, response: res }) ) diff --git a/tsconfig.json b/tsconfig.json index 8c2f2f2..4694219 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,10 +1,13 @@ { "compilerOptions": { "target": "ES5", - "module": "commonjs", - "lib": ["es2017", "dom"], + "module": "esnext", + "lib": [ + "es2017", + "dom" + ], "allowJs": true, - "jsx": "react", + "jsx": "react-jsx", "strict": true, "declaration": true, "noImplicitAny": false, @@ -14,8 +17,20 @@ "skipLibCheck": true, "sourceMap": true, "outDir": "lib", - "rootDir": "src" + "rootDir": "src", + "forceConsistentCasingInFileNames": true, + "noFallthroughCasesInSwitch": true, + "resolveJsonModule": true, + "isolatedModules": true, + "noEmit": true }, - "exclude": ["**/*.spec.ts", "**/*.spec.tsx", "**/*.spec.js", "./demo"], - "include": ["src"] + "exclude": [ + "**/*.spec.ts", + "**/*.spec.tsx", + "**/*.spec.js", + "./demo" + ], + "include": [ + "src" + ] }