2026-04-08 17:49:32 +00:00
|
|
|
import { defineStore } from "pinia";
|
|
|
|
|
import { ref } from "vue";
|
2026-04-09 00:25:21 +00:00
|
|
|
import { PublicReport, type PublicReportDTO } from "@/type/api";
|
2026-04-08 17:49:32 +00:00
|
|
|
|
2026-04-09 00:25:21 +00:00
|
|
|
export const useStorePublicReport = defineStore("publicreport", () => {
|
2026-04-08 17:49:32 +00:00
|
|
|
// State
|
2026-04-09 00:25:21 +00:00
|
|
|
const _byID = ref<Map<string, PublicReport>>(new Map());
|
2026-04-08 17:49:32 +00:00
|
|
|
const error = ref(null);
|
|
|
|
|
const loading = ref(false);
|
2026-04-09 00:25:21 +00:00
|
|
|
//const ongoingFetch = ref<Promise<PublicReport[]> | null>(null);
|
2026-04-08 17:49:32 +00:00
|
|
|
|
2026-04-09 00:25:21 +00:00
|
|
|
function add(pr: PublicReport) {
|
2026-04-08 17:49:32 +00:00
|
|
|
_byID.value.set(pr.id, pr);
|
|
|
|
|
}
|
|
|
|
|
// Actions
|
2026-04-09 00:25:21 +00:00
|
|
|
async function byID(id: string): Promise<PublicReport | undefined> {
|
2026-04-08 17:49:32 +00:00
|
|
|
loading.value = true;
|
|
|
|
|
error.value = null;
|
|
|
|
|
try {
|
|
|
|
|
const url = `/api/publicreport/${id}`;
|
|
|
|
|
const response = await fetch(url);
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
|
|
|
}
|
2026-04-09 00:25:21 +00:00
|
|
|
const body: PublicReportDTO = await response.json();
|
|
|
|
|
const report = PublicReport.fromJSON(body);
|
2026-04-08 22:54:20 +00:00
|
|
|
_byID.value.set(id, report);
|
|
|
|
|
return report;
|
2026-04-08 17:49:32 +00:00
|
|
|
} catch (err) {
|
|
|
|
|
console.error("Error loading users:", err);
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
// Actions
|
|
|
|
|
add,
|
|
|
|
|
byID,
|
|
|
|
|
};
|
|
|
|
|
});
|