benwinding / react-admin-import-csv

A csv file import button for react-admin

Home Page:https://benwinding.github.io/react-admin-import-csv

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Import functionality with row by row is updating wrong record

vinodphadtare opened this issue · comments

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.

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();
    }
  };

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