Ne uplne otestovany prepis storu
This commit is contained in:
+40
-33
@@ -1,41 +1,48 @@
|
||||
import {defineStore} from 'pinia'
|
||||
import {apolloWrapper} from "src/graphql/apollo";
|
||||
import {COPILOT_DETAIL, COPILOTS_LIST} from "src/graphql/queries/copilots";
|
||||
import {CREATE_COPILOT, EDIT_COPILOT} from "src/graphql/mutations/copilot";
|
||||
import {updateInItems} from "stores/helpers";
|
||||
import {mutate, query, updateInItems} from "stores/helpers";
|
||||
import {ref} from "vue";
|
||||
|
||||
export const useCopilotsStore = defineStore('Copilots', {
|
||||
state: () => ({
|
||||
items: [],
|
||||
isMutationRunning: false,
|
||||
}),
|
||||
export const useCopilotsStore = defineStore('Copilots', () => {
|
||||
const copilot = ref(null);
|
||||
const copilots = ref([]);
|
||||
const isMutationRunning = ref(false);
|
||||
const isQueryRunning = ref(false);
|
||||
|
||||
actions: {
|
||||
fetchDetail(copilotId, pilotUsername) {
|
||||
return apolloWrapper.query(COPILOT_DETAIL, {id: copilotId, pilotUsername: pilotUsername}).then(({data}) => data.copilot);
|
||||
},
|
||||
fetch() {
|
||||
return apolloWrapper.query(COPILOTS_LIST).then(result => {
|
||||
this.items = result.data.copilots;
|
||||
return result.data.copilots;
|
||||
});
|
||||
},
|
||||
create(formData) {
|
||||
delete formData['id'];
|
||||
const fetchDetail = (id) => {
|
||||
return query({
|
||||
isQueryRunning,
|
||||
query: COPILOT_DETAIL,
|
||||
variables: {id},
|
||||
}).then(item => copilot.value = item);
|
||||
};
|
||||
|
||||
this.isMutationRunning = true;
|
||||
return apolloWrapper.mutate(CREATE_COPILOT, {input: formData}).then(({data}) => {
|
||||
const copilot = data.createCopilot;
|
||||
this.items.push(copilot);
|
||||
return copilot;
|
||||
}).finally(() => this.isMutationRunning = false);
|
||||
},
|
||||
update(id, formData) {
|
||||
return apolloWrapper.mutate(EDIT_COPILOT, {id: id, input: formData}).then(({data}) => {
|
||||
const copilot = data.editCopilot;
|
||||
updateInItems(this.items, "id", copilot);
|
||||
return copilot;
|
||||
}).finally(() => this.isMutationRunning = false);
|
||||
},
|
||||
const fetch = () => {
|
||||
return query({
|
||||
isQueryRunning,
|
||||
query: COPILOTS_LIST,
|
||||
}).then(items => copilots.value = items);
|
||||
}
|
||||
|
||||
const create = (input) => {
|
||||
return mutate({
|
||||
isMutationRunning,
|
||||
mutation: CREATE_COPILOT,
|
||||
variables: {input},
|
||||
}).then(item => copilots.value.push(item))
|
||||
}
|
||||
|
||||
const update = (id, input) => {
|
||||
return mutate({
|
||||
isMutationRunning,
|
||||
mutation: EDIT_COPILOT,
|
||||
variables: {id, input}
|
||||
}).then(item => updateInItems(copilots.value, 'id', item))
|
||||
}
|
||||
|
||||
return {
|
||||
copilot, copilots, isMutationRunning, isQueryRunning,
|
||||
fetch, fetchDetail, create, update
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user