import { defineStore } from "pinia"; import { ref } from "vue"; import type { District } from "@/type/api"; export const useStoreDistrict = defineStore("district", () => { // State const _byURI = ref>(new Map()); const error = ref(null); const loading = ref(false); const ongoingFetch = ref | null>(null); // Actions async function byURI(uri: string): Promise { let district = _byURI.value.get(uri); console.log("district by uri", uri, district); if (district) { return district; } loading.value = true; error.value = null; try { const response = await fetch(uri); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const body = await response.json(); _byURI.value.set(uri, body); return body; } catch (e) { console.error("Error loading users:", e); error.value = e instanceof Error ? e.message : "an error ocurred"; throw e; } finally { loading.value = false; } } async function fetchDistricts(): Promise { loading.value = true; error.value = null; try { const response = await fetch("/api/district"); if (!response.ok) throw new Error("Failed to fetch districts"); const data: District[] = await response.json(); data.forEach((d: District) => { _byURI.value.set(d.uri, d); }); return data; } catch (e) { error.value = e instanceof Error ? e.message : "an error ocurred"; console.error("Error fetching districts:", e); throw new Error(error.value); } finally { loading.value = false; } } async function list(): Promise { if (_byURI.value.size > 0) { return Array.from(_byURI.value.values()); } if (ongoingFetch.value !== null) { return ongoingFetch.value; } const s = await fetchDistricts(); ongoingFetch.value = null; return s; } return { // Actions byURI, list, }; });