added suspense for home page
This commit is contained in:
80
app/page.tsx
80
app/page.tsx
@@ -1,12 +1,7 @@
|
|||||||
import { AddLocationButton } from './ui/AddLocationButton';
|
import { FC, Suspense } from 'react';
|
||||||
import { PageFooter } from './ui/PageFooter';
|
|
||||||
import { fetchAllLocations } from './lib/actions/locationActions';
|
|
||||||
import { fetchAvailableYears } from './lib/actions/monthActions';
|
|
||||||
import { BillingLocation, YearMonth } from './lib/db-types';
|
|
||||||
import { FC } from 'react';
|
|
||||||
import { Main } from './ui/Main';
|
import { Main } from './ui/Main';
|
||||||
import { MonthCard } from './ui/MonthCard';
|
import HomePage from './ui/HomePage';
|
||||||
import { MonthLocationList } from './ui/MonthLocationList';
|
import { HomePageSkeleton } from './ui/MonthCardSceleton';
|
||||||
|
|
||||||
export interface PageProps {
|
export interface PageProps {
|
||||||
searchParams?: {
|
searchParams?: {
|
||||||
@@ -17,74 +12,11 @@ export interface PageProps {
|
|||||||
|
|
||||||
const Page:FC<PageProps> = async ({ searchParams }) => {
|
const Page:FC<PageProps> = async ({ searchParams }) => {
|
||||||
|
|
||||||
let availableYears: number[];
|
|
||||||
|
|
||||||
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) {
|
|
||||||
|
|
||||||
const currentYearMonth:YearMonth = {
|
|
||||||
year: new Date().getFullYear(),
|
|
||||||
month: new Date().getMonth() + 1
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<main className="flex min-h-screen flex-col p-6 bg-base-300">
|
|
||||||
<MonthCard yearMonth={currentYearMonth} key={`month-${currentYearMonth}`} monthlyExpense={0}>
|
|
||||||
<AddLocationButton yearMonth={currentYearMonth} />
|
|
||||||
</MonthCard>
|
|
||||||
<PageFooter />
|
|
||||||
</main>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
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 (
|
return (
|
||||||
<Main>
|
<Main>
|
||||||
<MonthLocationList availableYears={availableYears} months={months} />
|
<Suspense fallback={<HomePageSkeleton />}>
|
||||||
|
<HomePage searchParams={searchParams} />
|
||||||
|
</Suspense>
|
||||||
</Main>
|
</Main>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
77
app/ui/HomePage.tsx
Normal file
77
app/ui/HomePage.tsx
Normal 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;
|
||||||
32
app/ui/MonthCardSceleton.tsx
Normal file
32
app/ui/MonthCardSceleton.tsx
Normal 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 />
|
||||||
|
</>;
|
||||||
@@ -18,8 +18,8 @@ const getNextYearMonth = (yearMonth:YearMonth) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface MonthLocationListProps {
|
export interface MonthLocationListProps {
|
||||||
availableYears: number[];
|
availableYears?: number[];
|
||||||
months: {
|
months?: {
|
||||||
[key: string]: {
|
[key: string]: {
|
||||||
yearMonth: YearMonth;
|
yearMonth: YearMonth;
|
||||||
locations: BillingLocation[];
|
locations: BillingLocation[];
|
||||||
@@ -36,6 +36,20 @@ export const MonthLocationList:React.FC<MonthLocationListProps > = ({
|
|||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const params = useParams();
|
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 { month: initialMonth } = params;
|
||||||
|
|
||||||
const [expandedMonth, setExpandedMonth] = React.useState<number>(
|
const [expandedMonth, setExpandedMonth] = React.useState<number>(
|
||||||
|
|||||||
Reference in New Issue
Block a user