2024-06-12 17:17:49 +07:00
|
|
|
import type { Poll } from "~/server/models/poll"
|
|
|
|
|
export const usePollStore = defineStore('usePollStore', () => {
|
|
|
|
|
const currentPoll = shallowReactive<Poll>({})
|
2024-06-06 13:29:22 +07:00
|
|
|
async function fetchById(id: string) {
|
2024-06-12 17:17:49 +07:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const { data } = await useFetch<any>(`/api/services/poll-by-id`, {
|
|
|
|
|
query: {
|
|
|
|
|
pollId: id
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
data.value && (Object.assign(currentPoll, data.value))
|
|
|
|
|
return currentPoll
|
|
|
|
|
} catch(error) {
|
|
|
|
|
|
2024-06-06 13:29:22 +07:00
|
|
|
}
|
2024-06-12 17:17:49 +07:00
|
|
|
}
|
2024-06-06 13:29:22 +07:00
|
|
|
|
2024-06-12 17:17:49 +07:00
|
|
|
async function categoryId() {
|
|
|
|
|
try {
|
|
|
|
|
const { data } = await useFetch(`/api/services/category-tree`)
|
|
|
|
|
return data.value
|
|
|
|
|
} catch (error) {}
|
|
|
|
|
|
2024-06-06 13:29:22 +07:00
|
|
|
}
|
|
|
|
|
|
2024-06-12 17:17:49 +07:00
|
|
|
return { fetchById, currentPoll, categoryId }
|
2024-06-06 13:29:22 +07:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if(import.meta.hot) {
|
2024-06-12 17:17:49 +07:00
|
|
|
import.meta.hot.accept(acceptHMRUpdate(usePollStore, import.meta.hot))
|
2024-06-06 13:29:22 +07:00
|
|
|
}
|