Ne uplne otestovany prepis storu
This commit is contained in:
+40
-34
@@ -1,42 +1,48 @@
|
||||
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 {mutate, query, updateInItems} from "stores/helpers";
|
||||
import {ref} from "vue";
|
||||
import {EVENT_DETAIL, EVENTS_LIST} from "src/graphql/queries/event";
|
||||
|
||||
export const useEventsStore = defineStore('Events', {
|
||||
state: () => ({
|
||||
items: [],
|
||||
isMutationRunning: false,
|
||||
}),
|
||||
export const useEventsStore = defineStore('Copilots', () => {
|
||||
const event = ref(null);
|
||||
const events = ref([]);
|
||||
const isMutationRunning = ref(false);
|
||||
const isQueryRunning = ref(false);
|
||||
|
||||
actions: {
|
||||
fetchDetail(eventId, username) {
|
||||
return apolloWrapper.query(EVENT_DETAIL, {id: eventId, username: username}).then(({data}) => data.event);
|
||||
},
|
||||
fetch(username) {
|
||||
return apolloWrapper.query(EVENTS_LIST, {username}).then(result => {
|
||||
this.items = result.data.events;
|
||||
return result.data.events;
|
||||
});
|
||||
},
|
||||
create(formData) {
|
||||
delete formData['id'];
|
||||
const fetchDetail = (id) => {
|
||||
return query({
|
||||
isQueryRunning,
|
||||
query: EVENT_DETAIL,
|
||||
variables: {id},
|
||||
}).then(item => event.value = item);
|
||||
};
|
||||
|
||||
this.isMutationRunning = true;
|
||||
return apolloWrapper.mutate(CREATE_EVENT, {input: formData}).then(({data}) => {
|
||||
const event = data.createEvent;
|
||||
event.flights = event.flights || [];
|
||||
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);
|
||||
},
|
||||
const fetch = (username = undefined) => {
|
||||
return query({
|
||||
isQueryRunning,
|
||||
query: EVENTS_LIST,
|
||||
variables: {username}
|
||||
}).then(items => events.value = items);
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
|
||||
return {
|
||||
event, events, isMutationRunning, isQueryRunning, fetch, fetchDetail, create, update
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user