42 lines
1.3 KiB
JavaScript
42 lines
1.3 KiB
JavaScript
import {defineStore} from 'pinia'
|
|
import {apolloWrapper} from "src/graphql/apollo";
|
|
import {CREATE_EVENT, EDIT_EVENT} from "src/graphql/mutations/event";
|
|
import {updateInItems} from "stores/helpers";
|
|
import {EVENT_DETAIL, EVENTS_LIST} from "src/graphql/queries/event";
|
|
|
|
export const useEventsStore = defineStore('Events', {
|
|
state: () => ({
|
|
items: [],
|
|
isMutationRunning: false,
|
|
}),
|
|
|
|
actions: {
|
|
fetchDetail(eventId) {
|
|
return apolloWrapper.query(EVENT_DETAIL, {id: eventId}).then(({data}) => data.event);
|
|
},
|
|
fetch() {
|
|
return apolloWrapper.query(EVENTS_LIST).then(result => {
|
|
this.items = result.data.events;
|
|
return result.data.events;
|
|
});
|
|
},
|
|
create(formData) {
|
|
delete formData['id'];
|
|
|
|
this.isMutationRunning = true;
|
|
return apolloWrapper.mutate(CREATE_EVENT, {input: formData}).then(({data}) => {
|
|
const event = data.createEvent;
|
|
this.items.push(event);
|
|
return event;
|
|
}).finally(() => this.isMutationRunning = false);
|
|
},
|
|
update(id, formData) {
|
|
return apolloWrapper.mutate(EDIT_EVENT, {id: id, input: formData}).then(({data}) => {
|
|
const event = data.editEvent;
|
|
updateInItems(this.items, "id", event);
|
|
return event;
|
|
}).finally(() => this.isMutationRunning = false);
|
|
},
|
|
}
|
|
})
|