Skip to content

Commit

Permalink
Fix: added types & removed sample data
Browse files Browse the repository at this point in the history
  • Loading branch information
jeessh committed Oct 3, 2024
1 parent 7cd77eb commit 472c909
Showing 1 changed file with 6 additions and 64 deletions.
70 changes: 6 additions & 64 deletions frontend/src/components/common/ExportToCSV.tsx
Original file line number Diff line number Diff line change
@@ -1,77 +1,20 @@
import React from "react";
import { Button } from "@chakra-ui/react";
import DownloadIcon from "@mui/icons-material/Download";
import FileDownloadOutlinedIcon from "@mui/icons-material/FileDownloadOutlined";
import { TableData } from "./CommonTable";

type ExportProps = {
exportData: any;
exportData: TableData[];
};

// set exportData to be the list of objects needed to be exported
const ExportToCSV = ({ exportData }: ExportProps): React.ReactElement => {
// <ExportToCSV exportData={null}/>
const sampleData = [
{
numOfIds: 1,
productId: null,
askOrgId: "Yes",
orderId: 11608501,
orgSelectionType: "FLOWER",
batchCode: "B-E7BE5602-2F9B-E3",
IDType: "OPEN",
batchId: 413,
creationDate: "2022-06-29",
isOnline: "Yes",
productName: null,
batchProductArray: [
{
ID: 663255,
TYPE: "PRODUCT",
NAME: "SOME NAME",
},
],
numOfUsedIDs: 0,
redemptionMethod: "ID",
askSSN: "No",
askEmployeeId: "Yes",
batchStatus: "Active",
productType: null,
expirationDate: "2023-06-29",
},
{
numOfIds: 1,
productId: null,
askOrgId: "No",
orderId: 11608502,
orgSelectionType: "LEAF",
batchCode: "B-480A8929-57D5-97",
IDType: "OPEN",
batchId: 414,
creationDate: "2022-06-29",
isOnline: "Yes",
productName: null,
batchProductArray: [
{
ID: 663255,
TYPE: "PRODUCT",
NAME: "Other Name",
},
],
numOfUsedIDs: 0,
redemptionMethod: "ID",
askSSN: "No",
askEmployeeId: "No",
batchStatus: "Active",
productType: null,
expirationDate: "2023-06-29",
},
];

const jsonToCSV = (jsonData: any) => {
const jsonToCSV = (jsonData: Array<TableData>) => {
let csv = "";
const headers = Object.keys(jsonData[0]);
csv += `${headers.join(",")}\n`;
// Add the data
jsonData.forEach((row: any) => {
jsonData.forEach((row: TableData) => {
const data = headers
.map((header) => JSON.stringify(row[header]))
.join(",");
Expand All @@ -81,7 +24,7 @@ const ExportToCSV = ({ exportData }: ExportProps): React.ReactElement => {
};

const downloadJsonAsCSV = () => {
const csvData = jsonToCSV(exportData || sampleData); // Add .items.data
const csvData = jsonToCSV(exportData);
// Create a CSV file and allow the user to download it
const blob = new Blob([csvData], { type: "text/csv" });
const url = window.URL.createObjectURL(blob);
Expand All @@ -96,7 +39,6 @@ const ExportToCSV = ({ exportData }: ExportProps): React.ReactElement => {
<>
<Button
alignItems="center"
// ={<DownloadIcon />} startIcon
variant="secondary"
onClick={downloadJsonAsCSV}
gap={1}
Expand Down

0 comments on commit 472c909

Please sign in to comment.