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(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 { 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, }; });