Enhance error handling in massSetDestinations: Add notifications for failures, logging support, and update modal behavior to prevent data clearing on errors.

This commit is contained in:
Jan 2026-01-06 17:40:07 +01:00
parent 81233db437
commit 8ef279e735
2 changed files with 43 additions and 23 deletions

View file

@ -267,7 +267,7 @@ export default {
return this.premiseEditStore.showProcessingModal || this.destinationEditStore.showProcessingModal; return this.premiseEditStore.showProcessingModal || this.destinationEditStore.showProcessingModal;
}, },
shownProcessingMessage() { shownProcessingMessage() {
if(this.premiseEditStore.showProcessingModal) if (this.premiseEditStore.showProcessingModal)
return "Please wait. Prepare calculation ..." return "Please wait. Prepare calculation ..."
return this.processingMessage; return this.processingMessage;
@ -444,6 +444,7 @@ export default {
async closeEditModalAction(action) { async closeEditModalAction(action) {
let massUpdate = false; let massUpdate = false;
let success = true;
if (this.modalType === 'amount' || this.modalType === 'routes' || this.modalType === "destinations") { if (this.modalType === 'amount' || this.modalType === 'routes' || this.modalType === "destinations") {
@ -453,18 +454,21 @@ export default {
const setMatrix = this.$refs.modalComponent?.destMatrix; const setMatrix = this.$refs.modalComponent?.destMatrix;
if (setMatrix) { if (setMatrix) {
await this.destinationEditStore.massSetDestinations(setMatrix); success = await this.destinationEditStore.massSetDestinations(setMatrix);
} }
} else { } else {
massUpdate = true massUpdate = true
} }
} }
// Clear data
this.fillData(this.modalType);
this.modalType = null;
if(massUpdate) { if (success) {
// Clear data
this.fillData(this.modalType);
this.modalType = null;
} else return;
if (massUpdate) {
await this.destinationEditStore.massUpdateDestinations(this.editIds); await this.destinationEditStore.massUpdateDestinations(this.editIds);
} }

View file

@ -1,6 +1,8 @@
import {defineStore} from 'pinia' import {defineStore} from 'pinia'
import performRequest from "@/backend.js"; import performRequest from "@/backend.js";
import {config} from '@/config' import {config} from '@/config'
import logger from "@/logger.js";
import {useNotificationStore} from "@/store/notification.js";
export const useDestinationEditStore = defineStore('destinationEdit', { export const useDestinationEditStore = defineStore('destinationEdit', {
state: () => ({ state: () => ({
@ -83,30 +85,44 @@ export const useDestinationEditStore = defineStore('destinationEdit', {
async massSetDestinations(updateMatrix) { async massSetDestinations(updateMatrix) {
this.loading = true; this.loading = true;
const toBeAdded = {}; try {
const toBeDeletedMap = new Map();
updateMatrix.forEach(row => { const toBeAdded = {};
toBeAdded[row.id] = row.destinations.filter(d => d.selected).map(d => d.id); const toBeDeletedMap = new Map();
toBeDeletedMap.set(row.id, row.destinations.filter(d => !d.selected).map(d => d.id));
});
const url = `${config.backendUrl}/calculation/destination`; updateMatrix.forEach(row => {
const { toBeAdded[row.id] = row.destinations.filter(d => d.selected).map(d => d.id);
data: data, toBeDeletedMap.set(row.id, row.destinations.filter(d => !d.selected).map(d => d.id));
headers: headers });
} = await performRequest(this, 'POST', url, {'destination_node_ids': toBeAdded});
this.destinations.forEach((destinations, premiseId) => { const url = `${config.backendUrl}/calculation/destination`;
const toBeDeleted = toBeDeletedMap.get(premiseId); const {
data: data,
headers: headers
} = await performRequest(this, 'POST', url, {'destination_node_ids': toBeAdded});
const filtered = destinations !== null ? destinations.filter(d => !toBeDeleted?.includes(d.destination_node.id)) : []; this.destinations.forEach((destinations, premiseId) => {
const toBeDeleted = toBeDeletedMap.get(premiseId);
this.destinations.set(premiseId, [...filtered, ...data[premiseId]]); const filtered = destinations !== null ? destinations.filter(d => !toBeDeleted?.includes(d.destination_node.id)) : [];
});
this.destinations.set(premiseId, [...filtered, ...data[premiseId]]);
});
this.loading = false; } catch (error) {
logger.error('Error in massSetDestinations:', error);
useNotificationStore().addNotification({
title: 'Unable to set destinations',
message: error.message ?? error.toString(),
variant: 'exception',
icon: 'bug',
})
return false;
} finally {
this.loading = false;
}
return true;
}, },
async massUpdateDestinations(premiseIds) { async massUpdateDestinations(premiseIds) {
this.loading = true; this.loading = true;