daisycrego / jbg-admin

Admin app for Jill Biggs Group follow-up boss event management
1 stars 0 forks source link

Export Events to CSV #25

Closed daisycrego closed 3 years ago

daisycrego commented 3 years ago

Something like this might help: https://stackoverflow.com/questions/48760815/export-to-csv-button-in-react-table

daisycrego commented 3 years ago

What happens

What should happen

daisycrego commented 3 years ago

Fixed the issue of missing fields. The api I have serving all of the events data specifically sends some fields, not all of the fields, so I had to go back in and specify all the fields I wanted to select from the Event model:

// event.controller.js
const list = async (req, res) => {
  try {
    let events = await Event.find().select(
      "id updated created source property status processed processedAt eventId created isNewLead isPossibleZillowExemption isZillowEvent message person personId propertyId type"
    );
    res.json(events);
  } catch (err) {
    return res.status(400).json({
      error: errorHandler.getErrorMessage(err),
    });
  }
};
daisycrego commented 3 years ago

Now, we still have the issue that the person and property objects aren't represented correctly, even though I do try to flatten them:

const EnhancedTableToolbar = (props) => {
  const classes = useToolbarStyles();

  const fields = [
    "processed",
    "processedAt",
    "status",
    "_id",
    "eventId",
    "created",
    "isNewLead",
    "isPossibleZillowExemption",
    "isZillowEvent",
    "message",
    "person",
    "personId",
    "property",
    "propertyId",
    "source",
    "type",
  ];
  const opts = { fields };
  const parser = new Parser(opts);
  console.log(`rows:`);
  console.log(props.rows);
  const flattenedRows = props.rows.flatMap((row) => flatten(row));
  console.log(`flattenedRows`);
  console.log(flattenedRows);
  const csv = parser.parse(flattenedRows);

I need to try flattening them differently.

daisycrego commented 3 years ago

I fixed that problem, so now the export seems to be working most of the time. It just seems like sometimes, even though I can see that the rows are loaded, the csv returned is empty. But re-downloading another time seems to resolve that issue, so it's not huge.