add update scope options for location editing

- Added radio button group to LocationEditForm with three update modes:
  1. Current month only (default) - updates specific location
  2. Current and all future months - updates current and subsequent months
  3. All months - updates all locations with same name across all time periods
- Enhanced updateOrAddLocation action with smart update logic based on scope
- Uses name-based matching to find related locations across months
- Added compact radio button styling with reduced spacing and indentation
- Added translations for update scope options in Croatian and English
- Maintains backward compatibility with existing single-location updates

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-08-11 11:42:48 +02:00
parent 80e30deace
commit a6d0cc77ac
4 changed files with 94 additions and 12 deletions

View File

@@ -28,6 +28,7 @@ const FormSchema = (t:IntlTemplateFn) => z.object({
locationName: z.coerce.string().min(1, t("location-name-required")), locationName: z.coerce.string().min(1, t("location-name-required")),
locationNotes: z.string(), locationNotes: z.string(),
addToSubsequentMonths: z.boolean().optional(), addToSubsequentMonths: z.boolean().optional(),
updateScope: z.enum(["current", "subsequent", "all"]).optional(),
}) })
// dont include the _id field in the response // dont include the _id field in the response
.omit({ _id: true }); .omit({ _id: true });
@@ -49,6 +50,7 @@ export const updateOrAddLocation = withUser(async (user:AuthenticatedUser, locat
locationName: formData.get('locationName'), locationName: formData.get('locationName'),
locationNotes: formData.get('locationNotes'), locationNotes: formData.get('locationNotes'),
addToSubsequentMonths: formData.get('addToSubsequentMonths') === 'on', addToSubsequentMonths: formData.get('addToSubsequentMonths') === 'on',
updateScope: formData.get('updateScope') as "current" | "subsequent" | "all" | undefined,
}); });
// If form validation fails, return errors early. Otherwise, continue... // If form validation fails, return errors early. Otherwise, continue...
@@ -63,6 +65,7 @@ export const updateOrAddLocation = withUser(async (user:AuthenticatedUser, locat
locationName, locationName,
locationNotes, locationNotes,
addToSubsequentMonths, addToSubsequentMonths,
updateScope,
} = validatedFields.data; } = validatedFields.data;
// update the bill in the mongodb // update the bill in the mongodb
@@ -71,17 +74,68 @@ export const updateOrAddLocation = withUser(async (user:AuthenticatedUser, locat
const { id: userId, email: userEmail } = user; const { id: userId, email: userEmail } = user;
if(locationId) { if(locationId) {
// Get the current location first to find its name
const currentLocation = await dbClient.collection<BillingLocation>("lokacije")
.findOne({ _id: locationId, userId });
if (!currentLocation) {
return {
message: "Location not found",
errors: undefined,
};
}
// Handle different update scopes
if (updateScope === "current" || !updateScope) {
// Update only the current location (default behavior)
await dbClient.collection<BillingLocation>("lokacije").updateOne( await dbClient.collection<BillingLocation>("lokacije").updateOne(
{ {
_id: locationId, // find a location with the given locationID _id: locationId,
userId // make sure the location belongs to the user userId
}, },
{ {
$set: { $set: {
name: locationName, name: locationName,
notes: locationNotes, notes: locationNotes,
} }
}); }
);
} else if (updateScope === "subsequent") {
// Update current and all subsequent months
await dbClient.collection<BillingLocation>("lokacije").updateMany(
{
userId,
name: currentLocation.name,
$or: [
{ "yearMonth.year": { $gt: currentLocation.yearMonth.year } },
{
"yearMonth.year": currentLocation.yearMonth.year,
"yearMonth.month": { $gte: currentLocation.yearMonth.month }
}
]
},
{
$set: {
name: locationName,
notes: locationNotes,
}
}
);
} else if (updateScope === "all") {
// Update all locations with the same name across all months
await dbClient.collection<BillingLocation>("lokacije").updateMany(
{
userId,
name: currentLocation.name
},
{
$set: {
name: locationName,
notes: locationNotes,
}
}
);
}
} else if(yearMonth) { } else if(yearMonth) {
// Always add location to the specified month // Always add location to the specified month
await dbClient.collection<BillingLocation>("lokacije").insertOne({ await dbClient.collection<BillingLocation>("lokacije").insertOne({

View File

@@ -60,14 +60,34 @@ export const LocationEditForm:FC<LocationEditFormProps> = ({ location, yearMonth
))} ))}
</div> </div>
{/* Show toggle only when adding a new location (not editing) */} {/* Show different options for add vs edit operations */}
{!location && ( {!location ? (
<div className="form-control"> <div className="form-control">
<label className="label cursor-pointer"> <label className="label cursor-pointer">
<span className="label-text">{t("add-to-subsequent-months")}</span> <span className="label-text">{t("add-to-subsequent-months")}</span>
<input type="checkbox" name="addToSubsequentMonths" className="toggle toggle-primary" /> <input type="checkbox" name="addToSubsequentMonths" className="toggle toggle-primary" />
</label> </label>
</div> </div>
) : (
<div className="form-control">
<div className="label">
<span className="label-text font-medium">{t("update-scope")}</span>
</div>
<div className="flex flex-col gap-1 ml-4">
<label className="label cursor-pointer justify-start gap-3 py-1">
<input type="radio" name="updateScope" value="current" className="radio radio-primary" defaultChecked />
<span className="label-text">{t("update-current-month")}</span>
</label>
<label className="label cursor-pointer justify-start gap-3 py-1">
<input type="radio" name="updateScope" value="subsequent" className="radio radio-primary" />
<span className="label-text">{t("update-subsequent-months")}</span>
</label>
<label className="label cursor-pointer justify-start gap-3 py-1">
<input type="radio" name="updateScope" value="all" className="radio radio-primary" />
<span className="label-text">{t("update-all-months")}</span>
</label>
</div>
</div>
)} )}
<div id="status-error" aria-live="polite" aria-atomic="true"> <div id="status-error" aria-live="polite" aria-atomic="true">

View File

@@ -102,6 +102,10 @@
"cancel-button": "Cancel", "cancel-button": "Cancel",
"delete-tooltip": "Delete realestate", "delete-tooltip": "Delete realestate",
"add-to-subsequent-months": "Add to all subsequent months", "add-to-subsequent-months": "Add to all subsequent months",
"update-scope": "Update scope:",
"update-current-month": "current month only",
"update-subsequent-months": "current and all future months",
"update-all-months": "all months",
"validation": { "validation": {
"location-name-required": "Relaestate name is required" "location-name-required": "Relaestate name is required"
} }

View File

@@ -101,6 +101,10 @@
"cancel-button": "Odbaci", "cancel-button": "Odbaci",
"delete-tooltip": "Brisanje nekretnine", "delete-tooltip": "Brisanje nekretnine",
"add-to-subsequent-months": "Dodaj u sve mjesece koji slijede", "add-to-subsequent-months": "Dodaj u sve mjesece koji slijede",
"update-scope": "Opseg ažuriranja:",
"update-current-month": "samo trenutni mjesec",
"update-subsequent-months": "trenutni i svi budući mjeseci",
"update-all-months": "svi mjeseci",
"validation": { "validation": {
"location-name-required": "Ime nekretnine je obavezno" "location-name-required": "Ime nekretnine je obavezno"
} }