88 lines
2.5 KiB
TypeScript
88 lines
2.5 KiB
TypeScript
import { defineStore } from "pinia";
|
|
import { subscribe } from "~/netcode";
|
|
import type { ConceptResponseDTO } from "~/netcode/events/dto/conceptresponse.dto";
|
|
import { handlers } from "~/netcode/handlers/concept.handler";
|
|
|
|
export const useConceptStore = defineStore("concept", {
|
|
state: () => ({
|
|
concepts: {} as Record<string, ConceptResponseDTO>,
|
|
eventSource: null as null | EventSource,
|
|
}),
|
|
actions: {
|
|
async subscribe() {
|
|
this.eventSource = subscribe(`/concepts/subscribe`);
|
|
this.eventSource.addEventListener("message", async (message) => {
|
|
const data = JSON.parse(message.data);
|
|
const type = data.type as string;
|
|
if (type in handlers) {
|
|
handlers[type as keyof typeof handlers](data);
|
|
}
|
|
});
|
|
},
|
|
async close() {
|
|
this.eventSource?.close();
|
|
},
|
|
async fetchConceptList() {
|
|
const config = useRuntimeConfig();
|
|
const data = await useFetch<ConceptResponseDTO[]>(
|
|
config.public.endpoint + `/concepts/all`,
|
|
{
|
|
credentials: "include",
|
|
}
|
|
);
|
|
if (data.data.value && !data.error.value) {
|
|
for (const concept of data.data.value) {
|
|
this.concepts[concept.id] = concept;
|
|
}
|
|
}
|
|
return data;
|
|
},
|
|
async enableConcept(concepts: ConceptResponseDTO) {
|
|
const config = useRuntimeConfig();
|
|
const data = await $fetch<ConceptResponseDTO>(
|
|
config.public.endpoint + `/concepts/` + concepts.id + "/enable",
|
|
{
|
|
credentials: "include",
|
|
method: "PUT",
|
|
}
|
|
);
|
|
// word.enabled = true;
|
|
// return data;
|
|
},
|
|
async disableConcept(concepts: ConceptResponseDTO) {
|
|
const config = useRuntimeConfig();
|
|
const data = await $fetch<ConceptResponseDTO>(
|
|
config.public.endpoint + `/concepts/` + concepts.id + "/disable",
|
|
{
|
|
credentials: "include",
|
|
method: "PUT",
|
|
}
|
|
);
|
|
// word.enabled = false;
|
|
// return data;
|
|
},
|
|
|
|
async createConcept(value: string) {
|
|
const config = useRuntimeConfig();
|
|
const data = await $fetch<ConceptResponseDTO>(
|
|
config.public.endpoint + `/concepts/`,
|
|
{
|
|
credentials: "include",
|
|
body: { value },
|
|
method: "POST",
|
|
}
|
|
);
|
|
},
|
|
async deleteConcept(id: number) {
|
|
const config = useRuntimeConfig();
|
|
const data = await $fetch<ConceptResponseDTO>(
|
|
config.public.endpoint + `/concepts/` + id,
|
|
{
|
|
credentials: "include",
|
|
method: "DELETE",
|
|
}
|
|
);
|
|
},
|
|
},
|
|
});
|