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, useEffect } from 'react';
import { MonthLocationList } from '@/app/ui/MonthLocationList';
import { redirect } from 'next/navigation';
export interface HomePageProps {
searchParams?: {
year?: string;
month?: string;
};
}
const ParamsYearInvalidMessage:FC<{ firstAvailableYear?: number }> = ({ firstAvailableYear }) => {
// Redirect to the first available year after showing the message
useEffect(() => {
if(firstAvailableYear) {
redirect(`/?year=${firstAvailableYear}`);
} else {
redirect(`/`);
}
});
return(
The year specified in the URL is invalid ... redirecting
);
};
export const HomePage:FC = async ({ searchParams }) => {
/** years found in the DB sorted descending */
let availableYears: number[];
try {
availableYears = await fetchAvailableYears();
} catch (error:any) {
return (
{error.message}
);
}
const paramsYear = searchParams?.year ? Number(searchParams.year) : null;
let selectedYear:number;
// IF year is set via params, check if it's valid
if(paramsYear) {
// IF the database is in it's initial state
// THEN show message param being invalid AND redirect to root page
if(availableYears.length === 0) {
return ();
}
// IF the year specified in the params is not found in the DB
// THEN show message param being invalid AND redirect to page showing first available year
if(!availableYears.includes(paramsYear)) {
return ();
}
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];
}
const locations = await fetchAllLocations(selectedYear);
const userSettings = await getUserSettings();
// group locations by month
const months = locations.reduce((acc, location) => {
const {year, month} = location.yearMonth;
const key = `${year}-${month}`;
const locationsInMonth = acc[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)
}
})
}
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)
}
});
}, {} as {[key:string]:{
yearMonth: YearMonth,
locations: BillingLocation[],
unpaidTotal: number,
payedTotal: number
} });
return (
);
}
export default HomePage;