diff --git a/app/lib/actions/locationActions.ts b/app/lib/actions/locationActions.ts index d1b29ed..88f21e4 100644 --- a/app/lib/actions/locationActions.ts +++ b/app/lib/actions/locationActions.ts @@ -302,7 +302,7 @@ export const fetchLocationById = async (locationID:string) => { return(billLocation); }; -export const deleteLocationById = withUser(async (user:AuthenticatedUser, locationID:string, yearMonth:YearMonth) => { +export const deleteLocationById = withUser(async (user:AuthenticatedUser, locationID:string, yearMonth:YearMonth, _prevState:any, formData: FormData) => { noStore(); @@ -310,8 +310,35 @@ export const deleteLocationById = withUser(async (user:AuthenticatedUser, locati const { id: userId } = user; - // find a location with the given locationID - await dbClient.collection("lokacije").deleteOne({ _id: locationID, userId }); + const deleteInSubsequentMonths = formData.get('deleteInSubsequentMonths') === 'on'; + + if (deleteInSubsequentMonths) { + // Get the location name first to find all locations with the same name + const location = await dbClient.collection("lokacije") + .findOne({ _id: locationID, userId }); + + if (location) { + // Delete all locations with the same name in current and subsequent months + await dbClient.collection("lokacije").deleteMany({ + userId, + name: location.name, + $or: [ + { "yearMonth.year": { $gt: yearMonth.year } }, + { + "yearMonth.year": yearMonth.year, + "yearMonth.month": { $gte: yearMonth.month } + } + ] + }); + } + } else { + // Delete only the specific location (current behavior) + await dbClient.collection("lokacije").deleteOne({ _id: locationID, userId }); + } - await gotoHome(yearMonth) + await gotoHome(yearMonth); + + return { + message: null + }; }) \ No newline at end of file diff --git a/app/ui/LocationDeleteForm.tsx b/app/ui/LocationDeleteForm.tsx index 0524aa9..1fc694b 100644 --- a/app/ui/LocationDeleteForm.tsx +++ b/app/ui/LocationDeleteForm.tsx @@ -4,7 +4,6 @@ import { FC, ReactNode } from "react"; import { BillingLocation } from "../lib/db-types"; import { deleteLocationById } from "../lib/actions/locationActions"; import { useFormState } from "react-dom"; -import { gotoUrl } from "../lib/actions/navigationActions"; import Link from "next/link"; import { useTranslations } from "next-intl"; @@ -16,14 +15,9 @@ export interface LocationDeleteFormProps { export const LocationDeleteForm:FC = ({ location }) => { const handleAction = deleteLocationById.bind(null, location._id, location.yearMonth); - const [ state, dispatch ] = useFormState(handleAction, null); + const [ , dispatch ] = useFormState(handleAction, null); const t = useTranslations("location-delete-form"); - - const handleCancel = () => { - gotoUrl(`/location/${location._id}/edit/`); - }; - return(
@@ -36,6 +30,13 @@ export const LocationDeleteForm:FC = ({ location }) => }) }

+ +
+ +
{t("cancel-button")} diff --git a/messages/en.json b/messages/en.json index caa76ac..7c61f48 100644 --- a/messages/en.json +++ b/messages/en.json @@ -88,9 +88,10 @@ "back-button": "Back" }, "location-delete-form": { - "text": "Please confirm deletion of realestate “{name}””.", + "text": "Please confirm deletion of realestate “{name}“.", "cancel-button": "Cancel", - "confirm-button": "Confirm" + "confirm-button": "Confirm", + "delete-in-subsequent-months": "Delete in all subsequent months" }, "location-edit-form": { "location-name-placeholder": "Realestate name", diff --git a/messages/hr.json b/messages/hr.json index 41c8fb4..b4e7bec 100644 --- a/messages/hr.json +++ b/messages/hr.json @@ -87,9 +87,10 @@ "back-button": "Nazad" }, "location-delete-form": { - "text": "Molim potvrdi brisanje nekretnine “{name}””.", + "text": "Molim potvrdi brisanje nekretnine “{name}“.", "cancel-button": "Odustani", - "confirm-button": "Potvrdi" + "confirm-button": "Potvrdi", + "delete-in-subsequent-months": "Obriši u svim mjesecima koji slijede" }, "location-edit-form": { "location-name-placeholder": "Ime nekretnine",