From 27b696faab2d7e01e61cf13f40b8dd21370874aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Dere=C5=BEi=C4=87?= Date: Fri, 9 Feb 2024 09:40:43 +0100 Subject: [PATCH] home page converted back to server-side component --- app/page.tsx | 19 +++-- app/ui/HomePage.tsx | 158 ++++++++++++----------------------- app/ui/LocationCard.tsx | 4 +- app/ui/MonthCard.tsx | 2 +- app/ui/MonthLocationList.tsx | 4 +- 5 files changed, 70 insertions(+), 117 deletions(-) diff --git a/app/page.tsx b/app/page.tsx index 01601df..6505d33 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -1,11 +1,7 @@ import { FC, Suspense } from 'react'; import { Main } from './ui/Main'; -import dynamic from 'next/dynamic' - -const HomePage = dynamic( - () => import('./ui/HomePage'), - { ssr: false } -) +import HomePage from './ui/HomePage'; +import { MonthCardSkeleton } from './ui/MonthCardSkeleton'; export interface PageProps { searchParams?: { @@ -14,11 +10,20 @@ export interface PageProps { }; } +const HomePageSkeleton = () => +<> + + + + + const Page:FC = async ({ searchParams }) => { return (
- + }> + +
); } diff --git a/app/ui/HomePage.tsx b/app/ui/HomePage.tsx index 262da6c..2f3f59a 100644 --- a/app/ui/HomePage.tsx +++ b/app/ui/HomePage.tsx @@ -1,118 +1,30 @@ -"use client"; - +import { fetchAllLocations } from '@/app/lib/actions/locationActions'; +import { fetchAvailableYears } from '@/app/lib/actions/monthActions'; import { BillingLocation, YearMonth } from '@/app/lib/db-types'; -import { FC, useEffect, useState } from 'react'; +import { FC } from 'react'; import { MonthLocationList } from '@/app/ui/MonthLocationList'; -import { WithId } from 'mongodb'; -import { MonthCardSkeleton } from './MonthCardSkeleton'; -import { useSearchParams } from 'next/navigation'; export interface HomePageProps { + searchParams?: { + year?: string; + month?: string; + }; } -type MonthsLocations = { - [key:string]:{ - yearMonth: YearMonth, - locations: BillingLocation[], - monthlyExpense: number - } -} +export const HomePage:FC = async ({ searchParams }) => { -const fetchAllLocations = async (year: number) => { - const response = await fetch(`/api/locations/in-year/?year=${year}`); - const { locations } : { locations: WithId[] } = await response.json(); - return locations; -} + let availableYears: number[]; -const fetchAvailableYears = async () => { - const response = await fetch(`/api/locations/available-years/`); - const { availableYears }: { availableYears: number[]} = await response.json(); - return availableYears; -} + // const asyncTimout = (ms:number) => new Promise(resolve => setTimeout(resolve, ms)); + // await asyncTimout(5000); -export const HomePage:FC = () => { - - const searchParams = useSearchParams(); - const year = searchParams.get('year'); - const currentYear = year ? parseInt(year, 10) : new Date().getFullYear(); - - const [ homePageStatus, setHomePageStatus ] = useState<{ - status: "loading" | "loaded" | "error", - availableYears: number[], - months?: MonthsLocations, - error?: string - }>({ - status: "loading", - availableYears: [], - }); - - const {availableYears, months, status, error} = homePageStatus; - - useEffect(() => { - - const fetchData = async () => { - - try { - 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 MonthsLocations); - - setHomePageStatus({ - availableYears: await fetchAvailableYears(), - months, - status: "loaded", - }); - - } catch (error: any) { - setHomePageStatus({ - status: "error", - availableYears: [], - error: error.message - }); - } - } - - fetchData(); - }, [currentYear]); - - if(status === "loading") { + try { + availableYears = await fetchAvailableYears(); + } catch (error:any) { return ( - <> - - - - - ); - } - - if(status === "error") { - return(

{error}

); +
+

{error.message}

+
); } // if the database is in it's initial state, show the add location button for the current month @@ -120,6 +32,42 @@ export const HomePage:FC = () => { return (); } + 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 ( ); diff --git a/app/ui/LocationCard.tsx b/app/ui/LocationCard.tsx index 7e9c613..ae1d556 100644 --- a/app/ui/LocationCard.tsx +++ b/app/ui/LocationCard.tsx @@ -18,12 +18,12 @@ export const LocationCard:FC = ({location: { _id, name, yearM const monthlyExpense = bills.reduce((acc, bill) => bill.paid ? acc + (bill.payedAmount ?? 0) : acc, 0); return( -
+
-

{formatYearMonth(yearMonth)} {name}

+

{formatYearMonth(yearMonth)} {name}

{ bills.map(bill => ) diff --git a/app/ui/MonthCard.tsx b/app/ui/MonthCard.tsx index 3861fb2..e9fea4e 100644 --- a/app/ui/MonthCard.tsx +++ b/app/ui/MonthCard.tsx @@ -30,7 +30,7 @@ export const MonthCard:FC = ({ yearMonth, children, monthlyExpen }, [expanded]); return( -
+
{`${formatYearMonth(yearMonth)}`} diff --git a/app/ui/MonthLocationList.tsx b/app/ui/MonthLocationList.tsx index 01b3584..e3bafa2 100644 --- a/app/ui/MonthLocationList.tsx +++ b/app/ui/MonthLocationList.tsx @@ -63,10 +63,10 @@ export const MonthLocationList:React.FC = ({ const handleMonthToggle = (yearMonth:YearMonth) => { // if the month is already expanded, collapse it if(expandedMonth === yearMonth.month) { - router.push(`/?year=${yearMonth.year}`); + // router.push(`/?year=${yearMonth.year}`); setExpandedMonth(-1); // no month is expanded } else { - router.push(`/?year=${yearMonth.year}&month=${yearMonth.month}`); + // router.push(`/?year=${yearMonth.year}&month=${yearMonth.month}`); setExpandedMonth(yearMonth.month); } }