Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ package-lock*

# misc
.DS_Store
.idea
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<any[]>;
// A function to handle row errors after import
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/config.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 10 additions & 7 deletions src/main-csv-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const MainCsvImport = (props: any) => {

const {
parseConfig,
meta,
preCommitCallback,
postCommitCallback,
validateRow,
Expand Down Expand Up @@ -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+'')
Expand All @@ -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) => {
Expand All @@ -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
);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down
34 changes: 22 additions & 12 deletions src/uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export async function create(
dataProvider: DataProvider,
resource: string,
values: any[],
meta?: any[],
preCommitCallback?: PrecommitCallback,
postCommitCallback?: ErrorCallback
) {
Expand All @@ -21,7 +22,8 @@ export async function create(
!!disableCreateMany,
dataProvider,
resource,
parsedValues
parsedValues,
meta
);
if (postCommitCallback) {
postCommitCallback(reportItems);
Expand All @@ -39,6 +41,7 @@ export async function update(
dataProvider: DataProvider,
resource: string,
values: any[],
meta?: any[],
preCommitCallback?: PrecommitCallback,
postCommitCallback?: ErrorCallback
) {
Expand All @@ -50,7 +53,8 @@ export async function update(
!!disableUpdateMany,
dataProvider,
resource,
parsedValues
parsedValues,
meta
);
if (postCommitCallback) {
postCommitCallback(reportItems);
Expand All @@ -64,6 +68,7 @@ export async function update(

interface ReportItem {
value: any;
meta?: any[];
success: boolean;
err?: any;
response?: any;
Expand All @@ -74,18 +79,19 @@ export async function createInDataProvider(
disableCreateMany: boolean,
dataProvider: DataProvider,
resource: string,
values: any[]
values: any[],
meta?: any[]
): Promise<ReportItem[]> {
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
})
Expand Down Expand Up @@ -120,13 +126,14 @@ export async function createInDataProvider(
async function createInDataProviderFallback(
dataProvider: DataProvider,
resource: string,
values: any[]
values: any[],
meta?: any[]
): Promise<ReportItem[]> {
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 })
)
Expand All @@ -141,24 +148,26 @@ 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);
logger.log("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
})
Expand Down Expand Up @@ -193,13 +202,14 @@ async function updateInDataProvider(
async function updateInDataProviderFallback(
dataProvider: DataProvider,
resource: string,
values: any[]
values: any[],
meta?: any[]
): Promise<ReportItem[]> {
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 })
)
Expand Down
27 changes: 21 additions & 6 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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"
]
}