49 lines
1.3 KiB
JavaScript
49 lines
1.3 KiB
JavaScript
import {defineStore} from 'pinia'
|
|
import {COPILOT_DETAIL, COPILOTS_LIST} from "src/graphql/queries/copilots";
|
|
import {CREATE_COPILOT, EDIT_COPILOT} from "src/graphql/mutations/copilot";
|
|
import {mutate, query, updateInItems} from "stores/helpers";
|
|
import {ref} from "vue";
|
|
|
|
export const useCopilotsStore = defineStore('Copilots', () => {
|
|
const copilot = ref(null);
|
|
const copilots = ref([]);
|
|
const isMutationRunning = ref(false);
|
|
const isQueryRunning = ref(false);
|
|
|
|
const fetchDetail = (id) => {
|
|
return query({
|
|
isQueryRunning,
|
|
query: COPILOT_DETAIL,
|
|
variables: {id},
|
|
}).then(item => copilot.value = item);
|
|
};
|
|
|
|
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
|
|
}
|
|
})
|