nidus-sync/ts/store/sync.ts

51 lines
1.2 KiB
TypeScript
Raw Normal View History

2026-04-14 19:05:10 +00:00
import { defineStore } from "pinia";
import { ref } from "vue";
import { Sync } from "@/type/api";
import { SSEManager, SSEMessageResource } from "@/SSEManager";
2026-04-14 19:05:10 +00:00
import { useSessionStore } from "@/store/session";
export const useStoreSync = defineStore("sync", () => {
// State
const all = ref<Sync[] | null>(null);
const loading = ref(false);
const error = ref(null);
// Subscription
SSEManager.subscribe((msg: SSEMessageResource) => {
2026-04-14 19:05:10 +00:00
if (msg.resource.startsWith("sync:sync")) {
fetchAll();
}
});
// Actions
async function fetchAll(): Promise<Sync[]> {
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();
const response = await fetch(`${session.urls.api.sync}?${params}`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = (await response.json()) as Sync[];
all.value = data;
return data;
} catch (err) {
console.error("Error loading communications:", err);
throw err;
}
}
return {
// State
all,
// Actions
fetchAll,
};
});