60 lines
2.1 KiB
TypeScript
60 lines
2.1 KiB
TypeScript
import { defineStore } from "pinia";
|
|
import { WordResponseDTO } from "~/netcode/events/dto/wordresponse.dto";
|
|
|
|
export const useWordStore = defineStore("word", {
|
|
state: () => ({
|
|
words: {} as Record<string, WordResponseDTO>,
|
|
}),
|
|
actions: {
|
|
async fetchWordList() {
|
|
const config = useRuntimeConfig();
|
|
const data = await useFetch<WordResponseDTO[]>(config.public.endpoint + `/words/all`, {
|
|
credentials: "include",
|
|
})
|
|
if (data.data.value && !data.error.value) {
|
|
|
|
for (const word of data.data.value) {
|
|
this.words[word.id] = word
|
|
}
|
|
}
|
|
return data
|
|
},
|
|
async enableWord(word: WordResponseDTO) {
|
|
const config = useRuntimeConfig();
|
|
const data = await $fetch<WordResponseDTO>(config.public.endpoint + `/words/` + word.id + "/enable", {
|
|
credentials: "include",
|
|
method: "PUT"
|
|
})
|
|
word.enabled = true
|
|
return data
|
|
},
|
|
async disableWord(word: WordResponseDTO) {
|
|
const config = useRuntimeConfig();
|
|
const data = await $fetch<WordResponseDTO>(config.public.endpoint + `/words/` + word.id + "/disable", {
|
|
credentials: "include",
|
|
method: "PUT"
|
|
})
|
|
word.enabled = false
|
|
return data
|
|
},
|
|
|
|
async createWord(value: string) {
|
|
const config = useRuntimeConfig();
|
|
const data = await $fetch<WordResponseDTO>(config.public.endpoint + `/words/`, {
|
|
credentials: "include",
|
|
body: { value },
|
|
method: "POST"
|
|
})
|
|
this.words[data.id] = data
|
|
return data
|
|
},
|
|
async deleteWord(word: WordResponseDTO){
|
|
const config = useRuntimeConfig();
|
|
const data = await $fetch<WordResponseDTO>(config.public.endpoint + `/words/`+word.id, {
|
|
credentials: "include",
|
|
method: "DELETE"
|
|
})
|
|
delete this.words[word.id]
|
|
}
|
|
},
|
|
}); |