Compare commits
4 Commits
feature/em
...
0e3e41e064
| Author | SHA1 | Date | |
|---|---|---|---|
| 0e3e41e064 | |||
| 488c771a09 | |||
| 1076797c89 | |||
|
|
a54771e479 |
@@ -4,6 +4,7 @@ import { getUserSettings } from '@/app/lib/actions/userSettingsActions';
|
|||||||
import { BillingLocation, YearMonth } from '@/app/lib/db-types';
|
import { BillingLocation, YearMonth } from '@/app/lib/db-types';
|
||||||
import { FC } from 'react';
|
import { FC } from 'react';
|
||||||
import { MonthLocationList } from '@/app/ui/MonthLocationList';
|
import { MonthLocationList } from '@/app/ui/MonthLocationList';
|
||||||
|
import { ParamsYearInvalidMessage } from './ParamsYearInvalidMessage';
|
||||||
|
|
||||||
export interface HomePageProps {
|
export interface HomePageProps {
|
||||||
searchParams?: {
|
searchParams?: {
|
||||||
@@ -14,11 +15,9 @@ export interface HomePageProps {
|
|||||||
|
|
||||||
export const HomePage:FC<HomePageProps> = async ({ searchParams }) => {
|
export const HomePage:FC<HomePageProps> = async ({ searchParams }) => {
|
||||||
|
|
||||||
|
/** years found in the DB sorted descending */
|
||||||
let availableYears: number[];
|
let availableYears: number[];
|
||||||
|
|
||||||
// const asyncTimout = (ms:number) => new Promise(resolve => setTimeout(resolve, ms));
|
|
||||||
// await asyncTimout(5000);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
availableYears = await fetchAvailableYears();
|
availableYears = await fetchAvailableYears();
|
||||||
} catch (error:any) {
|
} catch (error:any) {
|
||||||
@@ -28,14 +27,34 @@ export const HomePage:FC<HomePageProps> = async ({ searchParams }) => {
|
|||||||
</main>);
|
</main>);
|
||||||
}
|
}
|
||||||
|
|
||||||
// if the database is in it's initial state, show the add location button for the current month
|
const paramsYear = searchParams?.year ? Number(searchParams.year) : null;
|
||||||
if(availableYears.length === 0) {
|
let selectedYear:number;
|
||||||
return (<MonthLocationList />);
|
|
||||||
|
// 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 (<ParamsYearInvalidMessage />);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 (<ParamsYearInvalidMessage firstAvailableYear={availableYears[0]} />);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 currentYear = Number(searchParams?.year) || new Date().getFullYear();
|
const locations = await fetchAllLocations(selectedYear);
|
||||||
|
|
||||||
const locations = await fetchAllLocations(currentYear);
|
|
||||||
const userSettings = await getUserSettings();
|
const userSettings = await getUserSettings();
|
||||||
|
|
||||||
// group locations by month
|
// group locations by month
|
||||||
|
|||||||
21
app/ui/ParamsYearInvalidMessage.tsx
Normal file
21
app/ui/ParamsYearInvalidMessage.tsx
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
"use client";
|
||||||
|
import { FC, useEffect } from 'react';
|
||||||
|
import { redirect } from 'next/navigation';
|
||||||
|
|
||||||
|
export 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(
|
||||||
|
<main className="flex min-h-screen flex-col p-6 bg-base-300">
|
||||||
|
<p className="text-center text-2xl text-red-500">The year specified in the URL is invalid ... redirecting</p>
|
||||||
|
</main>
|
||||||
|
);
|
||||||
|
};
|
||||||
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,11 +1,11 @@
|
|||||||
{
|
{
|
||||||
"name": "evidencija-rezija",
|
"name": "evidencija-rezija",
|
||||||
"version": "2.20.0",
|
"version": "2.20.3",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"version": "2.20.0",
|
"version": "2.20.3",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@emotion/react": "^11.14.0",
|
"@emotion/react": "^11.14.0",
|
||||||
"@emotion/styled": "^11.14.1",
|
"@emotion/styled": "^11.14.1",
|
||||||
|
|||||||
@@ -58,5 +58,5 @@
|
|||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=18.17.0"
|
"node": ">=18.17.0"
|
||||||
},
|
},
|
||||||
"version": "2.20.0"
|
"version": "2.20.3"
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user