nidus-sync/ts/store/communication.ts
Eli Ribble a82732a49c
Return communication database rows from communication API
This is a pretty big refactor of how communication works to start moving
us in the direction we want to go long-term. This adds the new
communication row and migrates existing reports to add rows for
communication.

There's also a bunch of automatic fixes from the new linter. I should
have added them separately, but whatever.
2026-05-01 21:00:23 +00:00

54 lines
1.4 KiB
TypeScript

import { defineStore } from "pinia";
import { ref } from "vue";
import { apiClient } from "@/client";
import { SSEManager, SSEMessageResource } from "@/SSEManager";
import { useSessionStore } from "@/store/session";
import { Communication, CommunicationDTO } from "@/type/api";
export const useCommunicationStore = defineStore("communication", () => {
// State
const all = ref<Communication[] | null>(null);
const loading = ref(false);
const error = ref(null);
// Subscription
SSEManager.subscribe((msg: SSEMessageResource) => {
if (msg.resource.startsWith("rmo:")) {
fetchAll();
}
});
// Actions
async function fetchAll(): Promise<Communication[]> {
const session = useSessionStore();
if (session.urls == null) {
throw new Error("can't fetch without user URL data");
}
loading.value = true;
error.value = null;
try {
const params = new URLSearchParams();
params.append("sort", "-created");
//if (typeFilter.value) params.append("type", typeFilter.value);
const url = `${session.urls.api.communication}?${params}`;
const data = (await apiClient.JSONGet(url)) as CommunicationDTO[];
all.value = data.map((c: CommunicationDTO) => Communication.fromJSON(c));
return all.value;
} catch (err) {
console.error("Error loading communications:", err);
throw err;
} finally {
loading.value = false;
}
}
return {
// State
all,
loading,
// Actions
fetchAll,
};
});