benwinding / react-admin-import-csv

A csv file import button for react-admin
https://benwinding.github.io/react-admin-import-csv
MIT License
135 stars 47 forks source link

Import functionality with row by row is updating wrong record #67

Closed vinodphadtare closed 3 years ago

vinodphadtare commented 3 years ago

when I tried importing csv file posts.csv

and selected "Let me decide for each row" Current Record

Now you can see in console current record is with ID "4" But when I click "Replace the row ID =4" button, Code replaces record with ID=3 replaced-wrong-record

This happens with all the records when tried to replace or add new.

vinodphadtare commented 3 years ago

I was able to resolve this issue by changing two function in "src/main-csv-button.tsx" file

In handleAskDecideReplace, we should update to replace current record and then get nextConflicting record, instead of getting next conflicting record and updating it. Original Code (line 229)

const handleAskDecideReplace = async () => {
    logger.log("handleAskDecideReplace");
    const val = nextConflicting();
    if (!val) {
      return handleClose();
    }
    await updateRows([val]);
  };

Updated Code

const handleAskDecideReplace = async () => {
    logger.log("handleAskDecideReplace");
    await updateRows([currentValue]);
    const val = nextConflicting();
    if (!val) {
      return handleClose();
    }
  };

similarly for handleAskDecideAddAsNew we need to create a localCopy of current record and add it, instead of next conflicting record. original code(line 238)

const handleAskDecideAddAsNew = async () => {
    logger.log("handleAskDecideAddAsNew");
    const val = nextConflicting();
    if (!val) {
      return handleClose();
    }
    delete val.id;
    await createRows([val]);
  };

updated code

const handleAskDecideAddAsNew = async () => {
    logger.log("handleAskDecideAddAsNew");
    const localCopy = Object.assign({},currentValue)
    delete localCopy.id;
    await createRows([localCopy]);
    const val = nextConflicting();
    if (!val) {
      return handleClose();
    }
  };
benwinding commented 3 years ago

Hi @vinodphadtare, Thanks for reporting the issue, I've updated with your changes and it will be available in the 1.0.19 Also, feel free to submit pull requests for bugs like this in the future! Cheers, Ben