added suspense for home page

This commit is contained in:
2024-02-02 10:12:10 +01:00
parent e66958f6a3
commit 3cb93c53c4
4 changed files with 131 additions and 76 deletions

77
app/ui/HomePage.tsx Normal file
View File

@@ -0,0 +1,77 @@
import { fetchAllLocations } from '@/app/lib/actions/locationActions';
import { fetchAvailableYears } from '@/app/lib/actions/monthActions';
import { BillingLocation, YearMonth } from '@/app/lib/db-types';
import { FC } from 'react';
import { MonthLocationList } from '@/app/ui/MonthLocationList';
export interface HomePageProps {
searchParams?: {
year?: string;
month?: string;
};
}
export const HomePage:FC<HomePageProps> = async ({ searchParams }) => {
let availableYears: number[];
const asyncTimout = (ms:number) => new Promise(resolve => setTimeout(resolve, ms));
await asyncTimout(5000);
try {
availableYears = await fetchAvailableYears();
} catch (error:any) {
return (
<main className="flex min-h-screen flex-col p-6 bg-base-300">
<p className="text-center text-2xl text-red-500">{error.message}</p>
</main>);
}
// if the database is in it's initial state, show the add location button for the current month
if(availableYears.length === 0) {
return (<MonthLocationList />);
}
const currentYear = Number(searchParams?.year) || availableYears[0];
const locations = await fetchAllLocations(currentYear);
// 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],
monthlyExpense: locationsInMonth.monthlyExpense + location.bills.reduce((acc, bill) => bill.paid ? acc + (bill.payedAmount ?? 0) : acc, 0)
}
})
}
return({
...acc,
[key]: {
yearMonth: location.yearMonth,
locations: [location],
monthlyExpense: location.bills.reduce((acc, bill) => bill.paid ? acc + (bill.payedAmount ?? 0) : acc, 0)
}
});
}, {} as {[key:string]:{
yearMonth: YearMonth,
locations: BillingLocation[],
monthlyExpense: number
} });
return (
<MonthLocationList availableYears={availableYears} months={months} />
);
}
export default HomePage;

View File

@@ -0,0 +1,32 @@
import { Cog8ToothIcon, PlusCircleIcon } from "@heroicons/react/24/outline";
import React from "react"
const LocationCardSkeleton: React.FC = () =>
<div className={`skeleton card card-compact card-bordered max-w-[30em] bg-base-100 shadow-s my-1`}>
<div className="card-body">
<h2 className="card-title mr-[2em]"></h2>
</div>
</div>;
export interface MonthCardSkeletonProps {
checked?: boolean;
}
export const MonthCardSkeleton: React.FC<MonthCardSkeletonProps> = ({checked=false}) =>
<div className={`skeleton collapse collapse-plus bg-base-200 my-1`}>
<input type="checkbox" name="my-accordion-3" checked={checked} disabled />
<div className="collapse-title text-xl font-medium w-[15em]">
</div>
<div className="collapse-content">
<LocationCardSkeleton />
<LocationCardSkeleton />
</div>
</div>;
export const HomePageSkeleton: React.FC = () =>
<>
<MonthCardSkeleton checked={true} />
<MonthCardSkeleton />
<MonthCardSkeleton />
</>;

View File

@@ -18,8 +18,8 @@ const getNextYearMonth = (yearMonth:YearMonth) => {
}
export interface MonthLocationListProps {
availableYears: number[];
months: {
availableYears?: number[];
months?: {
[key: string]: {
yearMonth: YearMonth;
locations: BillingLocation[];
@@ -36,6 +36,20 @@ export const MonthLocationList:React.FC<MonthLocationListProps > = ({
const router = useRouter();
const params = useParams();
if(!availableYears || !months) {
const currentYearMonth:YearMonth = {
year: new Date().getFullYear(),
month: new Date().getMonth() + 1
};
return(
<>
<MonthCard yearMonth={currentYearMonth} key={`month-${currentYearMonth}`} monthlyExpense={0} onToggle={() => {}}>
<AddLocationButton yearMonth={currentYearMonth} />
</MonthCard>
</>)
};
const { month: initialMonth } = params;
const [expandedMonth, setExpandedMonth] = React.useState<number>(