50 lines
1.4 KiB
JavaScript
50 lines
1.4 KiB
JavaScript
import {defineStore} from 'pinia'
|
|
import {POI_DETAIL, POI_LIST} from "src/graphql/queries/poi";
|
|
import {CREATE_POI, EDIT_POI} from "src/graphql/mutations/poi";
|
|
import {mutate, query, updateInItems} from "stores/helpers";
|
|
import {ref} from "vue";
|
|
|
|
|
|
export const usePointsOfInterestStore = defineStore('PointsOfInterest', () => {
|
|
|
|
const pointOfInterest = ref(null);
|
|
const pointsOfInterest = ref([]);
|
|
const isMutationRunning = ref(false);
|
|
const isQueryRunning = ref(false);
|
|
|
|
const fetchDetail = (id) => {
|
|
return query({
|
|
isQueryRunning,
|
|
query: POI_DETAIL,
|
|
variables: {id},
|
|
}).then(item => pointOfInterest.value = item);
|
|
};
|
|
|
|
const fetch = () => {
|
|
return query({
|
|
isQueryRunning,
|
|
query: POI_LIST,
|
|
}).then(items => pointsOfInterest.value = items);
|
|
}
|
|
|
|
const create = (input) => {
|
|
return mutate({
|
|
isMutationRunning,
|
|
mutation: CREATE_POI,
|
|
variables: {input},
|
|
}).then(item => pointsOfInterest.value.push(item))
|
|
}
|
|
|
|
const update = (id, input) => {
|
|
return mutate({
|
|
isMutationRunning,
|
|
mutation: EDIT_POI,
|
|
variables: {id, input}
|
|
}).then(item => updateInItems(pointsOfInterest.value, 'id', item))
|
|
}
|
|
|
|
return {
|
|
pointOfInterest, pointsOfInterest, isMutationRunning, isQueryRunning, fetch, fetchDetail, create, update
|
|
}
|
|
})
|