diff --git a/app/lib/actions/locationActions.ts b/app/lib/actions/locationActions.ts index 3db4f29..e094c9d 100644 --- a/app/lib/actions/locationActions.ts +++ b/app/lib/actions/locationActions.ts @@ -7,7 +7,8 @@ import { ObjectId } from 'mongodb'; import { withUser } from '@/app/lib/auth'; import { AuthenticatedUser } from '../types/next-auth'; import { gotoHome } from './navigationActions'; -import { Noto_Sans_Tamil_Supplement } from 'next/font/google'; +import { unstable_noStore as noStore } from 'next/cache'; +import { asyncTimeout } from '../asyncTimeout'; export type State = { errors?: { @@ -34,6 +35,8 @@ const UpdateLocation = FormSchema.omit({ _id: true }); */ export const updateOrAddLocation = withUser(async (user:AuthenticatedUser, locationId: string | undefined, yearMonth: YearMonth | undefined, prevState:State, formData: FormData) => { + noStore(); + const validatedFields = UpdateLocation.safeParse({ locationName: formData.get('locationName'), locationNotes: formData.get('locationNotes'), @@ -81,6 +84,8 @@ export const updateOrAddLocation = withUser(async (user:AuthenticatedUser, locat }); } + // await asyncTimeout(1000); + if(yearMonth) await gotoHome(yearMonth); return { @@ -92,6 +97,8 @@ export const updateOrAddLocation = withUser(async (user:AuthenticatedUser, locat export const fetchAllLocations = withUser(async (user:AuthenticatedUser, year:number) => { + noStore(); + const dbClient = await getDbClient(); const { id: userId } = user; @@ -109,11 +116,15 @@ export const fetchAllLocations = withUser(async (user:AuthenticatedUser, year:nu }) .toArray(); + // await asyncTimeout(1000); + return(locations); }) export const fetchLocationById = withUser(async (user:AuthenticatedUser, locationID:string) => { + noStore(); + const dbClient = await getDbClient(); const { id: userId } = user; @@ -126,11 +137,15 @@ export const fetchLocationById = withUser(async (user:AuthenticatedUser, locatio return(null); } + // await asyncTimeout(1000); + return(billLocation); }) export const deleteLocationById = withUser(async (user:AuthenticatedUser, locationID:string, yearMonth:YearMonth) => { + noStore(); + const dbClient = await getDbClient(); const { id: userId } = user; @@ -138,5 +153,7 @@ export const deleteLocationById = withUser(async (user:AuthenticatedUser, locati // find a location with the given locationID const post = await dbClient.collection("lokacije").deleteOne({ _id: locationID, userId }); + // await asyncTimeout(1000); + await gotoHome(yearMonth) }) \ No newline at end of file diff --git a/app/lib/actions/monthActions.ts b/app/lib/actions/monthActions.ts index 3e7948a..7b3d843 100644 --- a/app/lib/actions/monthActions.ts +++ b/app/lib/actions/monthActions.ts @@ -5,6 +5,7 @@ import { ObjectId } from 'mongodb'; import { Bill, BillingLocation, YearMonth } from '../db-types'; import { AuthenticatedUser } from '../types/next-auth'; import { withUser } from '../auth'; +import { unstable_noStore as noStore } from 'next/cache'; /** * Server-side action which adds a new month to the database @@ -15,6 +16,8 @@ import { withUser } from '../auth'; * @returns */ export const addMonth = withUser(async (user:AuthenticatedUser, { year, month }: YearMonth) => { + noStore(); + const { id: userId } = user; // update the bill in the mongodb @@ -60,6 +63,8 @@ export const addMonth = withUser(async (user:AuthenticatedUser, { year, month }: }); export const fetchAvailableYears = withUser(async (user:AuthenticatedUser) => { + noStore(); + const { id: userId } = user; const dbClient = await getDbClient(); diff --git a/app/lib/asyncTimeout.ts b/app/lib/asyncTimeout.ts new file mode 100644 index 0000000..c65d4ae --- /dev/null +++ b/app/lib/asyncTimeout.ts @@ -0,0 +1,2 @@ + +export const asyncTimeout = (ms: number = 1000) => new Promise(resolve => setTimeout(resolve, ms)); \ No newline at end of file diff --git a/app/location/[id]/edit/LocationEditPage.tsx b/app/location/[id]/edit/LocationEditPage.tsx new file mode 100644 index 0000000..50d0b31 --- /dev/null +++ b/app/location/[id]/edit/LocationEditPage.tsx @@ -0,0 +1,16 @@ +import { notFound } from 'next/navigation'; +import { LocationEditForm } from '@/app/ui/LocationEditForm'; +import { fetchLocationById } from '@/app/lib/actions/locationActions'; + +export default async function LocationEditPage({ locationId }: { locationId:string }) { + + const location = await fetchLocationById(locationId); + + if (!location) { + return(notFound()); + } + + const result = ; + + return (result); +} \ No newline at end of file diff --git a/app/location/[id]/edit/page.tsx b/app/location/[id]/edit/page.tsx index b28cd30..251f944 100644 --- a/app/location/[id]/edit/page.tsx +++ b/app/location/[id]/edit/page.tsx @@ -1,13 +1,15 @@ -import { notFound } from 'next/navigation'; -import { LocationEditForm } from '@/app/ui/LocationEditForm'; -import { fetchLocationById } from '@/app/lib/actions/locationActions'; +import { Suspense } from 'react'; +import LocationEditPage from './LocationEditPage'; +import { Main } from '@/app/ui/Main'; +import { LocationEditFormSkeleton } from '@/app/ui/LocationEditForm'; export default async function Page({ params:{ id } }: { params: { id:string } }) { - const location = await fetchLocationById(id); - - if (!location) { - return(notFound()); - } - return (); + return ( +
+ }> + + +
+ ); } \ No newline at end of file diff --git a/app/ui/BillDeleteForm.tsx b/app/ui/BillDeleteForm.tsx index df76fa0..f12fc50 100644 --- a/app/ui/BillDeleteForm.tsx +++ b/app/ui/BillDeleteForm.tsx @@ -4,22 +4,20 @@ import { FC } from "react"; import { Bill, BillingLocation } from "../lib/db-types"; import { useFormState } from "react-dom"; import { Main } from "./Main"; -import { gotoHome } from "../lib/actions/navigationActions"; import { deleteBillById } from "../lib/actions/billActions"; +import Link from "next/link"; export interface BillDeleteFormProps { bill: Bill, location: BillingLocation } -export const BillDeleteForm:FC = ({ bill, location }) => -{ - const handleAction = deleteBillById.bind(null, location._id, bill._id, location.yearMonth.year, location.yearMonth.month); +export const BillDeleteForm:FC = ({ bill, location }) => { + + const { year, month } = location.yearMonth; + const handleAction = deleteBillById.bind(null, location._id, bill._id, year, month); const [ state, dispatch ] = useFormState(handleAction, null); - const handleCancel = () => { - gotoHome(location.yearMonth); - }; return(
@@ -31,7 +29,7 @@ export const BillDeleteForm:FC = ({ bill, location }) =>

- + Cancel
diff --git a/app/ui/BillEditForm.tsx b/app/ui/BillEditForm.tsx index 443eb02..500ca12 100644 --- a/app/ui/BillEditForm.tsx +++ b/app/ui/BillEditForm.tsx @@ -126,7 +126,7 @@ export const BillEditForm:FC = ({ location, bill }) => {
- + Cancel
diff --git a/app/ui/LocationDeleteForm.tsx b/app/ui/LocationDeleteForm.tsx index 30fe546..ad2f3c8 100644 --- a/app/ui/LocationDeleteForm.tsx +++ b/app/ui/LocationDeleteForm.tsx @@ -6,6 +6,7 @@ import { deleteLocationById } from "../lib/actions/locationActions"; import { useFormState } from "react-dom"; import { Main } from "./Main"; import { gotoUrl } from "../lib/actions/navigationActions"; +import Link from "next/link"; export interface LocationDeleteFormProps { /** location which should be deleted */ @@ -31,7 +32,7 @@ export const LocationDeleteForm:FC = ({ location }) =>

- + Cancel
diff --git a/app/ui/LocationEditForm.tsx b/app/ui/LocationEditForm.tsx index 97b5c49..31568d8 100644 --- a/app/ui/LocationEditForm.tsx +++ b/app/ui/LocationEditForm.tsx @@ -5,15 +5,19 @@ import { FC } from "react"; import { BillingLocation, YearMonth } from "../lib/db-types"; import { updateOrAddLocation } from "../lib/actions/locationActions"; import { useFormState } from "react-dom"; -import { Main } from "./Main"; import Link from "next/link"; import { gotoHome } from "../lib/actions/navigationActions"; -export interface LocationEditFormProps { +export type LocationEditFormProps = { /** location which should be edited */ - location?: BillingLocation, + location: BillingLocation, /** year adn month at a new billing location should be assigned */ - yearMonth?: YearMonth + yearMonth: undefined +} | { + /** location which should be edited */ + location: undefined, + /** year adn month at a new billing location should be assigned */ + yearMonth: YearMonth } export const LocationEditForm:FC = ({ location, yearMonth }) => @@ -22,62 +26,65 @@ export const LocationEditForm:FC = ({ location, yearMonth const handleAction = updateOrAddLocation.bind(null, location?._id, yearMonth); const [ state, dispatch ] = useFormState(handleAction, initialState); - // redirect to the main page - const handleCancel = () => { - if(location) - gotoHome(location?.yearMonth); - else if(yearMonth) - gotoHome(yearMonth); - - }; + let { year, month } = location ? location.yearMonth : yearMonth; return( -
-
-
-
+
+
+ + { + location && + + + + } + +
+ {state.errors?.locationName && + state.errors.locationName.map((error: string) => ( +

+ {error} +

+ ))} +
+ + +
+ {state.errors?.locationNotes && + state.errors.locationNotes.map((error: string) => ( +

+ {error} +

+ ))} +
+ +
{ - location && - - - + state.message && +

+ {state.message} +

} - -
- {state.errors?.locationName && - state.errors.locationName.map((error: string) => ( -

- {error} -

- ))} -
+
- -
- {state.errors?.locationNotes && - state.errors.locationNotes.map((error: string) => ( -

- {error} -

- ))} -
- -
- { - state.message && -

- {state.message} -

- } -
- -
- - -
- -
+
+ + Cancel +
+
-
+ + ) +} + +export const LocationEditFormSkeleton:FC = () => +{ + return( +
+
+ + +
+
) } diff --git a/app/ui/MonthLocationList.tsx b/app/ui/MonthLocationList.tsx index eb484a1..01b3584 100644 --- a/app/ui/MonthLocationList.tsx +++ b/app/ui/MonthLocationList.tsx @@ -7,7 +7,7 @@ import { MonthCard } from "./MonthCard"; import Pagination from "./Pagination"; import { LocationCard } from "./LocationCard"; import { BillingLocation, YearMonth } from "../lib/db-types"; -import { useParams, useRouter, useSearchParams } from "next/navigation"; +import { useRouter, useSearchParams } from "next/navigation"; const getNextYearMonth = (yearMonth:YearMonth) => { const {year, month} = yearMonth; @@ -77,11 +77,15 @@ export const MonthLocationList:React.FC = ({ monthsArray.map(([monthKey, { yearMonth, locations, monthlyExpense }], monthIx) => { - locations.map((location, ix) => ) + yearMonth.month === expandedMonth ? + locations.map((location, ix) => ) + : null } { - // show AddLocationButton as a last item in the first month - monthIx === 0 ? : null + yearMonth.month === expandedMonth ? + // show AddLocationButton as a last item in the first month + (monthIx === 0 ? : null) + : null } )