nidus-sync/ts/store/publicreport.ts
Eli Ribble f88ca57d97
Migrate existing ts types from the API into the API module
This makes it possible to start hydrating the types into valid data
types like Dates which means I can get type safety guarantees when
displaying information.
2026-04-09 00:25:21 +00:00

41 lines
1 KiB
TypeScript

import { defineStore } from "pinia";
import { ref } from "vue";
import { PublicReport, type PublicReportDTO } from "@/type/api";
export const useStorePublicReport = defineStore("publicreport", () => {
// State
const _byID = ref<Map<string, PublicReport>>(new Map());
const error = ref(null);
const loading = ref(false);
//const ongoingFetch = ref<Promise<PublicReport[]> | null>(null);
function add(pr: PublicReport) {
_byID.value.set(pr.id, pr);
}
// Actions
async function byID(id: string): Promise<PublicReport | undefined> {
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}`);
}
const body: PublicReportDTO = await response.json();
const report = PublicReport.fromJSON(body);
_byID.value.set(id, report);
return report;
} catch (err) {
console.error("Error loading users:", err);
throw err;
}
}
return {
// Actions
add,
byID,
};
});