(refactor + bugfix) Improve data structure and handle empty database edge cases
Refactored months data structure from object to array for better performance and cleaner iteration. Fixed crash when availableYears array is empty by adding proper guards and fallback to current year. Changes: - MonthLocationList: Changed months prop from object to array type - HomePage: Refactored reduce logic to build array instead of object - HomePage: Added empty database handling in year selection logic - HomePage: Added early returns for invalid year params in empty DB Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
import { fetchAllLocations } from '@/app/lib/actions/locationActions';
|
||||
import { fetchAvailableYears } from '@/app/lib/actions/monthActions';
|
||||
import { getUserSettings } from '@/app/lib/actions/userSettingsActions';
|
||||
import { BillingLocation, YearMonth } from '@/app/lib/db-types';
|
||||
import { FC } from 'react';
|
||||
import { MonthLocationList } from '@/app/ui/MonthLocationList';
|
||||
import { MonthArray, MonthLocationList } from '@/app/ui/MonthLocationList';
|
||||
import { ParamsYearInvalidMessage } from './ParamsYearInvalidMessage';
|
||||
|
||||
export interface HomePageProps {
|
||||
@@ -46,51 +45,59 @@ export const HomePage:FC<HomePageProps> = async ({ searchParams }) => {
|
||||
}
|
||||
|
||||
selectedYear = paramsYear;
|
||||
|
||||
} else {
|
||||
const currYear = new Date().getFullYear();
|
||||
|
||||
// IF current year is available in DB THEN use it
|
||||
// ELSE use the latest year found in the DB
|
||||
selectedYear = availableYears.includes(currYear) ? currYear : availableYears[0];
|
||||
if(availableYears.length === 0) {
|
||||
// Database is in it's initial state
|
||||
// so just set selected year to current year
|
||||
selectedYear = currYear;
|
||||
} else {
|
||||
// IF current year is available in DB THEN use it
|
||||
// ELSE use the latest year found in the DB
|
||||
selectedYear = availableYears.includes(currYear) ? currYear : availableYears[0];
|
||||
}
|
||||
}
|
||||
|
||||
const locations = await fetchAllLocations(selectedYear);
|
||||
const userSettings = await getUserSettings();
|
||||
|
||||
// group locations by month
|
||||
const months = locations.reduce((acc, location) => {
|
||||
// Create months object by grouping locations by yearMonth
|
||||
const { months } = locations.reduce((acc, location) => {
|
||||
const {year, month} = location.yearMonth;
|
||||
const key = `${year}-${month}`;
|
||||
|
||||
const locationsInMonth = acc[key];
|
||||
const monthIx = acc.index[key];
|
||||
|
||||
if(locationsInMonth) {
|
||||
return({
|
||||
...acc,
|
||||
[key]: {
|
||||
yearMonth: location.yearMonth,
|
||||
locations: [...locationsInMonth.locations, location],
|
||||
unpaidTotal: locationsInMonth.unpaidTotal + location.bills.reduce((acc, bill) => !bill.paid ? acc + (bill.payedAmount ?? 0) : acc, 0),
|
||||
payedTotal: locationsInMonth.payedTotal + location.bills.reduce((acc, bill) => bill.paid ? acc + (bill.payedAmount ?? 0) : acc, 0)
|
||||
}
|
||||
})
|
||||
if(monthIx) {
|
||||
|
||||
const existingMonth = acc.months[monthIx];
|
||||
|
||||
existingMonth.locations.push(location);
|
||||
existingMonth.unpaidTotal += location.bills.reduce((acc, bill) => !bill.paid ? acc + (bill.payedAmount ?? 0) : acc, 0);
|
||||
existingMonth.payedTotal += location.bills.reduce((acc, bill) => bill.paid ? acc + (bill.payedAmount ?? 0) : acc, 0);
|
||||
|
||||
return acc;
|
||||
}
|
||||
|
||||
return({
|
||||
...acc,
|
||||
[key]: {
|
||||
yearMonth: location.yearMonth,
|
||||
locations: [location],
|
||||
unpaidTotal: location.bills.reduce((acc, bill) => !bill.paid ? acc + (bill.payedAmount ?? 0) : acc, 0),
|
||||
payedTotal: location.bills.reduce((acc, bill) => bill.paid ? acc + (bill.payedAmount ?? 0) : acc, 0)
|
||||
}
|
||||
acc.months.push({
|
||||
yearMonth: location.yearMonth,
|
||||
locations: [location],
|
||||
unpaidTotal: location.bills.reduce((acc, bill) => !bill.paid ? acc + (bill.payedAmount ?? 0) : acc, 0),
|
||||
payedTotal: location.bills.reduce((acc, bill) => bill.paid ? acc + (bill.payedAmount ?? 0) : acc, 0)
|
||||
});
|
||||
}, {} as {[key:string]:{
|
||||
yearMonth: YearMonth,
|
||||
locations: BillingLocation[],
|
||||
unpaidTotal: number,
|
||||
payedTotal: number
|
||||
} });
|
||||
|
||||
acc.index[key] = acc.months.length - 1;
|
||||
|
||||
return acc;
|
||||
}, {
|
||||
index: {},
|
||||
months: []
|
||||
} as {
|
||||
index: Record<string, number>,
|
||||
months: MonthArray
|
||||
});
|
||||
|
||||
return (
|
||||
<MonthLocationList availableYears={availableYears} months={months} userSettings={userSettings} />
|
||||
|
||||
Reference in New Issue
Block a user