import {defineStore} from 'pinia' import {CREATE_EVENT, EDIT_EVENT} from "src/graphql/mutations/event"; import {mutate, query, rewriteObject, setItems, updateInItems} from "stores/helpers"; import {ref} from "vue"; import {EVENT_DETAIL, EVENT_PUBLIC_DETAIL, EVENTS_LIST, EVENTS_PUBLIC_LIST} from "src/graphql/queries/event"; export const useEventsStore = defineStore('Events', () => { const event = ref(null); const events = ref([]); const totalItemsCount = ref(0); const isMutationRunning = ref(false); const isQueryRunning = ref(false); const fetchDetail = (id) => { return query({ isQueryRunning, query: EVENT_DETAIL, variables: {id}, }).then(item => event.value = item); }; const fetchPublicDetail = (urlSlug, username) => { return query({ isQueryRunning, query: EVENT_PUBLIC_DETAIL, variables: {urlSlug, username}, }).then(item => event.value = item); }; const fetch = ({limit = 100, offset, appendItems}) => { return query({ isQueryRunning, query: EVENTS_LIST, variables: {limit, offset} }).then(data => setItems(data, appendItems, events, totalItemsCount)); } const fetchPublic = (username, limit = 100, offset, appendItems) => { return query({ isQueryRunning, query: EVENTS_PUBLIC_LIST, variables: {username, limit, offset} }).then(data => setItems(data, appendItems, events, totalItemsCount)); } const create = (input) => { return mutate({ isMutationRunning, mutation: CREATE_EVENT, variables: {input}, }).then(item => events.value.push(item)) } const update = (id, input) => { return mutate({ isMutationRunning, mutation: EDIT_EVENT, variables: {id, input} }).then(item => { updateInItems(events.value, 'id', item); event.value = rewriteObject(item, event.value); return item; }) } return { event, events, isMutationRunning, isQueryRunning, fetch, fetchDetail, create, update, fetchPublic, fetchPublicDetail, totalItemsCount } })