Skip to content

Commit

Permalink
Modified getUniqueCategories to not show 0 expenses categories for a …
Browse files Browse the repository at this point in the history
…selected month
  • Loading branch information
SahilDahekar committed Feb 9, 2025
1 parent 5fc837e commit 894f705
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion components/FinancialSummary/BarChartComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default function BarChartComponent() {
});

// Extracting unique categories from the data
const categories: string[] = getUniqueCategories({ selectedYear });
const categories: string[] = getUniqueCategories({ selectedYear, selectedMonth });
const years: string[] = ['2023', '2024']; // Add more years as needed

// Effect hook to update windowWidth state on resize
Expand Down
22 changes: 18 additions & 4 deletions utils/getUniqueCategories.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
import { loadYearData } from './loadYearData';

/**
* Retrieves unique expense categories from the Expenses data.
* Retrieves unique expense categories for a specific month and year.
* @param {string} selectedYear - The year for which to retrieve the data.
* @param {string} selectedMonth - The month for which to retrieve the data.
* @returns {string[]} An array of unique expense categories.
*/
export const getUniqueCategories = ({selectedYear} : {selectedYear: string}): string[] => {
export const getUniqueCategories = ({selectedYear, selectedMonth} : {selectedYear: string; selectedMonth: string}): string[] => {
const { expensesData } = loadYearData(selectedYear);
const allCategories: string[] = [];
for (const month in expensesData) {
expensesData[month as keyof typeof expensesData].forEach((entry: { Category: string }) => {

// If "All Months" is selected, return categories from all months
if (selectedMonth === 'All Months') {
for (const month in expensesData) {
expensesData[month as keyof typeof expensesData].forEach((entry: { Category: string }) => {
if (!allCategories.includes(entry.Category)) {
allCategories.push(entry.Category);
}
});
}
} else {
// Return categories only for the selected month
const monthData = expensesData[selectedMonth as keyof typeof expensesData] || [];
monthData.forEach((entry: { Category: string }) => {
if (!allCategories.includes(entry.Category)) {
allCategories.push(entry.Category);
}
});
}

return allCategories;
};

0 comments on commit 894f705

Please sign in to comment.