nidus-sync/ts/store/communication.ts
Eli Ribble 63ebe382b6
Properly convert communication API objects
Gets rid of a warning when showing relative time.
2026-04-27 19:50:47 +00:00

56 lines
1.4 KiB
TypeScript

import { defineStore } from "pinia";
import { ref } from "vue";
import { Communication, CommunicationDTO } from "@/type/api";
import { SSEManager, SSEMessage } from "@/SSEManager";
import { useSessionStore } from "@/store/session";
export const useCommunicationStore = defineStore("communication", () => {
// State
const all = ref<Communication[] | null>(null);
const loading = ref(false);
const error = ref(null);
// Subscription
SSEManager.subscribe((msg: SSEMessage) => {
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 response = await fetch(
`${session.urls.api.communication}?${params}`,
);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
all.value = data.communications.map((c: CommunicationDTO) =>
Communication.fromJSON(c),
);
return data.communications;
} catch (err) {
console.error("Error loading communications:", err);
throw err;
}
}
return {
// State
all,
// Actions
fetchAll,
};
});