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"; export const useCopilotsStore = defineStore('Copilots', { state: () => ({ items: [], isMutationRunning: 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']; 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); }, } })