|
106 | 106 | });
|
107 | 107 | }
|
108 | 108 |
|
109 |
| - function downloadCSV() { |
110 |
| - const filteredExpenses = tempExpenses.filter((expense) => { |
111 |
| - const date = new Date(expense.date); |
| 109 | + function downloadCSV(): void { |
| 110 | + console.log(selectedMonth, selectedYear); |
| 111 | +
|
| 112 | + // Print the expenses for the selected month and year |
| 113 | + console.log("Expenses for", selectedYear, selectedMonth); |
| 114 | +
|
| 115 | + // Define a mapping for German month abbreviations to full English equivalents |
| 116 | + const germanToEnglishMonths: { [key: string]: string } = { |
| 117 | + "Jan.": "Jan", |
| 118 | + "Feb.": "Feb", |
| 119 | + März: "Mar", |
| 120 | + "Apr.": "Apr", |
| 121 | + Mai: "May", |
| 122 | + Juni: "Jun", |
| 123 | + Juli: "Jul", |
| 124 | + "Aug.": "Aug", |
| 125 | + "Sept.": "Sep", |
| 126 | + "Okt.": "Oct", |
| 127 | + "Nov.": "Nov", |
| 128 | + "Dez.": "Dec" |
| 129 | + }; |
| 130 | +
|
| 131 | + // Replace German month names with English equivalents before parsing |
| 132 | + const normalizedDates = tempExpenses.map((expense: { date: string }) => { |
| 133 | + let dateStr = expense.date; |
| 134 | + Object.keys(germanToEnglishMonths).forEach((germanMonth) => { |
| 135 | + dateStr = dateStr.replace(germanMonth, germanToEnglishMonths[germanMonth]); |
| 136 | + }); |
| 137 | + return new Date(dateStr); |
| 138 | + }); |
| 139 | +
|
| 140 | + // Filter expenses for the selected month and year |
| 141 | + const filteredExpenses = tempExpenses.filter((expense: { date: string }, index: number) => { |
| 142 | + const date = normalizedDates[index]; |
112 | 143 | return (
|
113 | 144 | date.getMonth() + 1 === parseInt(selectedMonth) &&
|
114 | 145 | date.getFullYear() === parseInt(selectedYear)
|
115 | 146 | );
|
116 | 147 | });
|
| 148 | +
|
| 149 | + // Convert the filtered expenses to CSV |
117 | 150 | const csv = Papa.unparse(filteredExpenses);
|
118 | 151 | const blob = new Blob([csv], { type: "text/csv;charset=utf-8;" });
|
119 | 152 | saveAs(blob, `expenses_${selectedYear}-${selectedMonth}.csv`);
|
|
0 commit comments