Files
2026-07-27 14:07:26 +08:00

60 lines
1.6 KiB
JavaScript

"use strict";
const common_vendor = require("../common/vendor.js");
const api_index = require("../api/index.js");
const usePriceStore = common_vendor.defineStore("price", () => {
const prices = common_vendor.ref([]);
const date = common_vendor.ref("");
const isLoading = common_vendor.ref(false);
const loadPrices = async () => {
isLoading.value = true;
try {
const data = await api_index.priceApi.getPrices();
if (data) {
prices.value = data.prices;
date.value = data.date;
}
} catch (error) {
console.error("Failed to load prices:", error);
}
isLoading.value = false;
};
const getPriceByCategory = (category) => {
return prices.value.find((p) => p.category === category);
};
const addPrice = async (priceItem) => {
try {
await api_index.priceApi.addPrice(priceItem);
await loadPrices();
} catch (error) {
console.error("Failed to add price:", error);
}
};
const updatePrice = async (priceItem, originalCategory) => {
try {
await api_index.priceApi.updatePrice(priceItem, originalCategory);
await loadPrices();
} catch (error) {
console.error("Failed to update price:", error);
}
};
const deletePrice = async (category) => {
try {
await api_index.priceApi.deletePrice(category);
await loadPrices();
} catch (error) {
console.error("Failed to delete price:", error);
}
};
return {
prices,
date,
isLoading,
loadPrices,
getPriceByCategory,
addPrice,
updatePrice,
deletePrice
};
});
exports.usePriceStore = usePriceStore;