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 46 forks source link

Inject record data into transformRows callback #68

Open freddyseubertnio opened 3 years ago

freddyseubertnio commented 3 years ago

Hi there!

I have a Show component for a 'Parent' resource and I want to import a CSV which contains entries of a 'Child' resource. Now I need to reference the currently displayed Parent ID in a field for every imported Child. I understand that I can add static information in the transformRows callback but how can I possibly inject the currently displayed Parent record into the function?

Thank you in advance for any solution or hint in the right direction!

const configuration: ImportConfig = {
  // ...
  transformRows: (csvRows) => {
    let rows = [];
    csvRows.map((row) => {
      row.parent_id = parent.id; // this is where I need the injected data
      rows.push(row);
    });
    return rows;
  },
  // ...
}

export const ParentShow = (props) => (
    <Show {...props}>
        <TabbedShowLayout>
            <Tab label="Parent Details">
                <TextField source="parentdetail1" />
                <TextField source="parentdetail2" />
            </Tab>
            <Tab label="Children" path="children">
                <ImportButton {...props} {...configuration} />
                <ReferenceManyField reference="children" target="parent_id">
                    <Datagrid>
                        <TextField source="childdetail" />
                    </Datagrid>
                </ReferenceManyField>
            </Tab>
        </TabbedShowLayout>
    </Show>
);