|
| 1 | +import { useState } from "react"; |
| 2 | +import { downloadCsv, CsvColumn } from "../../utils/csvUtils"; |
| 3 | +import { Grant } from "../../../../middle-layer/types/Grant"; |
| 4 | +import { useProcessGrantData } from "../../main-page/grants/filter-bar/processGrantData"; |
| 5 | +import { observer } from "mobx-react-lite"; |
| 6 | +import "../grants/styles/GrantButton.css"; |
| 7 | +import { getAppStore } from "../../external/bcanSatchel/store"; |
| 8 | + |
| 9 | +// Define the columns for the CSV export, including any necessary formatting. |
| 10 | +const columns: CsvColumn<Grant>[] = [ |
| 11 | + { key: "grantId", title: "Grant ID" }, |
| 12 | + { key: "organization", title: "Organization" }, |
| 13 | + { |
| 14 | + key: "does_bcan_qualify", |
| 15 | + title: "BCAN Qualifies", |
| 16 | + formatValue: (value: boolean) => (value ? "Yes" : "No"), |
| 17 | + }, |
| 18 | + { key: "status", title: "Status" }, |
| 19 | + { |
| 20 | + key: "amount", |
| 21 | + title: "Amount ($)", |
| 22 | + formatValue: (value: number) => value.toLocaleString(), |
| 23 | + }, |
| 24 | + { |
| 25 | + key: "grant_start_date", |
| 26 | + title: "Grant Start Date", |
| 27 | + formatValue: (value: string) => new Date(value).toLocaleDateString(), |
| 28 | + }, |
| 29 | + { |
| 30 | + key: "application_deadline", |
| 31 | + title: "Application Deadline", |
| 32 | + formatValue: (value: string) => new Date(value).toLocaleDateString(), |
| 33 | + }, |
| 34 | + { |
| 35 | + key: "report_deadlines", |
| 36 | + title: "Report Deadlines", |
| 37 | + formatValue: (value?: string[]) => |
| 38 | + value?.length ? value.join(", ") : "None", |
| 39 | + }, |
| 40 | + { |
| 41 | + key: "description", |
| 42 | + title: "Description", |
| 43 | + formatValue: (value?: string) => value ?? "", |
| 44 | + }, |
| 45 | + { key: "timeline", title: "Timeline (Years)" }, |
| 46 | + { |
| 47 | + key: "estimated_completion_time", |
| 48 | + title: "Estimated Completion Time (Hours)", |
| 49 | + }, |
| 50 | + { |
| 51 | + key: "grantmaker_poc", |
| 52 | + title: "Grantmaker POC", |
| 53 | + formatValue: (value?: { POC_name: string; POC_email: string }) => |
| 54 | + value |
| 55 | + ? `${value.POC_name}${value.POC_email ? ` (${value.POC_email})` : ""}` |
| 56 | + : "N/A", |
| 57 | + }, |
| 58 | + { |
| 59 | + key: "bcan_poc", |
| 60 | + title: "BCAN POC", |
| 61 | + formatValue: (value: { POC_name: string; POC_email: string }) => |
| 62 | + `${value.POC_name}${value.POC_email ? ` (${value.POC_email})` : ""}`, |
| 63 | + }, |
| 64 | + { |
| 65 | + key: "attachments", |
| 66 | + title: "Attachments", |
| 67 | + formatValue: (attachments: string[]) => |
| 68 | + attachments?.length ? attachments.join(" | ") : "None", |
| 69 | + }, |
| 70 | + { |
| 71 | + key: "isRestricted", |
| 72 | + title: "Restricted?", |
| 73 | + formatValue: (value: boolean) => (value ? "Yes" : "No"), |
| 74 | + }, |
| 75 | +]; |
| 76 | + |
| 77 | +const CsvExportButton: React.FC = observer(() => { |
| 78 | + const { yearFilter } = getAppStore(); |
| 79 | + const [isProcessing, setIsProcessing] = useState(false); |
| 80 | + const { grants } = useProcessGrantData(); |
| 81 | + const onClickDownload = async () => { |
| 82 | + setIsProcessing(true); |
| 83 | + |
| 84 | + const data = grants as Grant[]; |
| 85 | + |
| 86 | + // Optional: handle empty or invalid data gracefully |
| 87 | + if (!data || data.length === 0) { |
| 88 | + alert("No data available to export."); |
| 89 | + setIsProcessing(false); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + // Simulate delay for UX |
| 94 | + await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 95 | + |
| 96 | + downloadCsv( |
| 97 | + data, |
| 98 | + columns, |
| 99 | + `BCAN Data ${(yearFilter ?? []).join("_")} as of ${ |
| 100 | + new Date().toISOString().split("T")[0] |
| 101 | + }` |
| 102 | + ); |
| 103 | + setIsProcessing(false); |
| 104 | + }; |
| 105 | + |
| 106 | + return ( |
| 107 | + <button |
| 108 | + className="grant-button add-grant-button bg-medium-orange" |
| 109 | + type="button" |
| 110 | + onClick={onClickDownload} |
| 111 | + disabled={isProcessing} |
| 112 | + title="Export the grants data including any applied filters." |
| 113 | + > |
| 114 | + {isProcessing ? "Please wait..." : "Export CSV"} |
| 115 | + </button> |
| 116 | + ); |
| 117 | +}); |
| 118 | + |
| 119 | +export default CsvExportButton; |
0 commit comments