79 lines
2.5 KiB
JavaScript
79 lines
2.5 KiB
JavaScript
import { defineStore } from 'pinia';
|
|
import { ref, computed } from 'vue';
|
|
|
|
const useRegionStore = defineStore("region", () => {
|
|
const regions = ref([]);
|
|
const activeRegion = ref(null);
|
|
const isLoading = ref(false);
|
|
const error = ref(null);
|
|
const defaultRegion = computed(() => {
|
|
return regions.value.find((r) => r.country_code === "DEFAULT") || null;
|
|
});
|
|
const regionMap = computed(() => {
|
|
const map = /* @__PURE__ */ new Map();
|
|
for (const r of regions.value) {
|
|
map.set(r.country_code, r);
|
|
}
|
|
return map;
|
|
});
|
|
const activeCurrency = computed(() => {
|
|
var _a, _b;
|
|
return ((_a = activeRegion.value) == null ? void 0 : _a.currency) || ((_b = defaultRegion.value) == null ? void 0 : _b.currency) || "EUR";
|
|
});
|
|
const activeLocale = computed(() => {
|
|
var _a, _b;
|
|
return ((_a = activeRegion.value) == null ? void 0 : _a.locale_code) || ((_b = defaultRegion.value) == null ? void 0 : _b.locale_code) || "en-EU";
|
|
});
|
|
const activeVatRate = computed(() => {
|
|
var _a, _b, _c, _d;
|
|
return (_d = (_c = (_a = activeRegion.value) == null ? void 0 : _a.default_vat_rate) != null ? _c : (_b = defaultRegion.value) == null ? void 0 : _b.default_vat_rate) != null ? _d : 0;
|
|
});
|
|
const activeTimezone = computed(() => {
|
|
var _a, _b;
|
|
return ((_a = activeRegion.value) == null ? void 0 : _a.timezone) || ((_b = defaultRegion.value) == null ? void 0 : _b.timezone) || "Europe/Brussels";
|
|
});
|
|
async function fetchRegions() {
|
|
var _a;
|
|
if (regions.value.length > 0) return;
|
|
isLoading.value = true;
|
|
error.value = null;
|
|
try {
|
|
const data = await $fetch("/api/v1/system/regions");
|
|
regions.value = data || [];
|
|
} catch (err) {
|
|
error.value = ((_a = err == null ? void 0 : err.data) == null ? void 0 : _a.detail) || (err == null ? void 0 : err.message) || "Failed to load regions";
|
|
console.error("[RegionStore] Fetch error:", err);
|
|
} finally {
|
|
isLoading.value = false;
|
|
}
|
|
}
|
|
function setActiveRegion(countryCode) {
|
|
const found = regions.value.find((r) => r.country_code === countryCode);
|
|
activeRegion.value = found || defaultRegion.value;
|
|
}
|
|
function getRegion(countryCode) {
|
|
return regionMap.value.get(countryCode);
|
|
}
|
|
return {
|
|
// State
|
|
regions,
|
|
activeRegion,
|
|
isLoading,
|
|
error,
|
|
// Getters
|
|
defaultRegion,
|
|
regionMap,
|
|
activeCurrency,
|
|
activeLocale,
|
|
activeVatRate,
|
|
activeTimezone,
|
|
// Actions
|
|
fetchRegions,
|
|
setActiveRegion,
|
|
getRegion
|
|
};
|
|
});
|
|
|
|
export { useRegionStore as u };
|
|
//# sourceMappingURL=region-0mvXqaMM.mjs.map
|