Refactor price loading logic in RevenueSection and SaleSection components: Replace for-loops with Promise.all for concurrent API requests, improving performance and responsiveness. This change enhances the handling of loading states and ensures better management of price data retrieval.
This commit is contained in:
@@ -117,13 +117,11 @@
|
|||||||
if (this.currentRegionId === null || this.currentRegionId === undefined) {
|
if (this.currentRegionId === null || this.currentRegionId === undefined) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (const product of this.products) {
|
const requests = this.products.map(async (product) => {
|
||||||
if (this.loadingPrices.has(product.id)) continue;
|
if (this.loadingPrices.has(product.id)) return;
|
||||||
this.loadingPrices.add(product.id);
|
this.loadingPrices.add(product.id);
|
||||||
try {
|
try {
|
||||||
// Verwende den gerundeten Preis aus calculateProductRevenue (wie gewollt)
|
|
||||||
const currentPrice = parseFloat(this.calculateProductRevenue(product).absolute);
|
const currentPrice = parseFloat(this.calculateProductRevenue(product).absolute);
|
||||||
|
|
||||||
const { data } = await apiClient.get('/api/falukant/products/prices-in-cities', {
|
const { data } = await apiClient.get('/api/falukant/products/prices-in-cities', {
|
||||||
params: {
|
params: {
|
||||||
productId: product.id,
|
productId: product.id,
|
||||||
@@ -131,8 +129,6 @@
|
|||||||
currentRegionId: this.currentRegionId
|
currentRegionId: this.currentRegionId
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
// Speichere betterPrices in einem separaten Map, nicht auf dem product Objekt
|
|
||||||
// In Vue 3 müssen wir ein neues Objekt erstellen, um die Reaktivität zu triggern
|
|
||||||
this.betterPricesMap = {
|
this.betterPricesMap = {
|
||||||
...this.betterPricesMap,
|
...this.betterPricesMap,
|
||||||
[product.id]: data || []
|
[product.id]: data || []
|
||||||
@@ -146,7 +142,8 @@
|
|||||||
} finally {
|
} finally {
|
||||||
this.loadingPrices.delete(product.id);
|
this.loadingPrices.delete(product.id);
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
await Promise.all(requests);
|
||||||
},
|
},
|
||||||
getBetterPrices(productId) {
|
getBetterPrices(productId) {
|
||||||
return this.betterPricesMap[productId] || [];
|
return this.betterPricesMap[productId] || [];
|
||||||
|
|||||||
@@ -294,12 +294,11 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
async loadPricesForInventory() {
|
async loadPricesForInventory() {
|
||||||
for (const item of this.inventory) {
|
const requests = this.inventory.map(async (item) => {
|
||||||
const itemKey = `${item.region.id}-${item.product.id}-${item.quality}`;
|
const itemKey = `${item.region.id}-${item.product.id}-${item.quality}`;
|
||||||
if (this.loadingPrices.has(itemKey)) continue;
|
if (this.loadingPrices.has(itemKey)) return;
|
||||||
this.loadingPrices.add(itemKey);
|
this.loadingPrices.add(itemKey);
|
||||||
try {
|
try {
|
||||||
// Aktueller Preis basierend auf sellCost
|
|
||||||
const currentPrice = item.product.sellCost || 0;
|
const currentPrice = item.product.sellCost || 0;
|
||||||
const { data } = await apiClient.get('/api/falukant/products/prices-in-cities', {
|
const { data } = await apiClient.get('/api/falukant/products/prices-in-cities', {
|
||||||
params: {
|
params: {
|
||||||
@@ -314,7 +313,8 @@
|
|||||||
} finally {
|
} finally {
|
||||||
this.loadingPrices.delete(itemKey);
|
this.loadingPrices.delete(itemKey);
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
await Promise.all(requests);
|
||||||
},
|
},
|
||||||
getCityPriceClass(branchType) {
|
getCityPriceClass(branchType) {
|
||||||
if (branchType === 'store') return 'city-price-green';
|
if (branchType === 'store') return 'city-price-green';
|
||||||
|
|||||||
Reference in New Issue
Block a user