2026-03-22 00:55:48 +00:00
|
|
|
import { defineStore } from "pinia";
|
2026-04-09 00:25:21 +00:00
|
|
|
import { ref } from "vue";
|
2026-04-28 14:49:02 +00:00
|
|
|
|
|
|
|
|
import { apiClient } from "@/client";
|
2026-04-28 17:06:21 +00:00
|
|
|
import { SSEManager, SSEMessageResource } from "@/SSEManager";
|
2026-04-09 00:25:21 +00:00
|
|
|
import { useSessionStore } from "@/store/session";
|
2026-04-28 14:49:02 +00:00
|
|
|
import { Communication, CommunicationDTO } from "@/type/api";
|
2026-03-22 00:55:48 +00:00
|
|
|
|
|
|
|
|
export const useCommunicationStore = defineStore("communication", () => {
|
|
|
|
|
// State
|
2026-03-22 04:53:50 +00:00
|
|
|
const all = ref<Communication[] | null>(null);
|
2026-03-22 00:55:48 +00:00
|
|
|
const loading = ref(false);
|
|
|
|
|
const error = ref(null);
|
|
|
|
|
|
2026-03-22 07:57:55 +00:00
|
|
|
// Subscription
|
2026-04-28 17:06:21 +00:00
|
|
|
SSEManager.subscribe((msg: SSEMessageResource) => {
|
2026-05-11 23:21:16 +00:00
|
|
|
if (
|
|
|
|
|
msg.resource.startsWith("sync:communication") &&
|
|
|
|
|
msg.type == "updated"
|
|
|
|
|
) {
|
|
|
|
|
fetchOne(msg.uri);
|
2026-03-22 07:57:55 +00:00
|
|
|
}
|
|
|
|
|
});
|
2026-03-22 00:55:48 +00:00
|
|
|
// Actions
|
2026-03-31 14:52:53 +00:00
|
|
|
async function fetchAll(): Promise<Communication[]> {
|
|
|
|
|
const session = useSessionStore();
|
|
|
|
|
if (session.urls == null) {
|
2026-03-22 02:37:10 +00:00
|
|
|
throw new Error("can't fetch without user URL data");
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-22 00:55:48 +00:00
|
|
|
loading.value = true;
|
|
|
|
|
error.value = null;
|
|
|
|
|
try {
|
|
|
|
|
const params = new URLSearchParams();
|
|
|
|
|
params.append("sort", "-created");
|
2026-03-22 02:37:10 +00:00
|
|
|
//if (typeFilter.value) params.append("type", typeFilter.value);
|
2026-03-22 00:55:48 +00:00
|
|
|
|
2026-04-28 14:49:02 +00:00
|
|
|
const url = `${session.urls.api.communication}?${params}`;
|
2026-05-01 20:49:37 +00:00
|
|
|
const data = (await apiClient.JSONGet(url)) as CommunicationDTO[];
|
2026-03-22 00:55:48 +00:00
|
|
|
|
2026-05-01 20:49:37 +00:00
|
|
|
all.value = data.map((c: CommunicationDTO) => Communication.fromJSON(c));
|
|
|
|
|
return all.value;
|
2026-03-22 00:55:48 +00:00
|
|
|
} catch (err) {
|
|
|
|
|
console.error("Error loading communications:", err);
|
|
|
|
|
throw err;
|
2026-04-28 14:49:02 +00:00
|
|
|
} finally {
|
|
|
|
|
loading.value = false;
|
2026-03-22 00:55:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
2026-05-11 23:21:16 +00:00
|
|
|
async function fetchOne(uri: string) {
|
|
|
|
|
const data = (await apiClient.JSONGet(uri)) as CommunicationDTO;
|
|
|
|
|
if (!all.value) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
for (var i = 0; i < all.value.length; i++) {
|
|
|
|
|
const c = all.value[i];
|
|
|
|
|
if (c.uri == data.uri) {
|
|
|
|
|
all.value[i] = Communication.fromJSON(data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-22 02:37:10 +00:00
|
|
|
return {
|
|
|
|
|
// State
|
|
|
|
|
all,
|
2026-04-28 14:49:02 +00:00
|
|
|
loading,
|
2026-03-22 02:37:10 +00:00
|
|
|
// Actions
|
2026-03-22 03:01:49 +00:00
|
|
|
fetchAll,
|
2026-03-22 02:37:10 +00:00
|
|
|
};
|
2026-03-22 00:55:48 +00:00
|
|
|
});
|