Ne uplne otestovany prepis storu
This commit is contained in:
@@ -1,52 +1,49 @@
|
||||
import {defineStore} from 'pinia'
|
||||
import {apolloWrapper} from "src/graphql/apollo";
|
||||
import {POI_DETAIL, POI_LIST} from "src/graphql/queries/poi";
|
||||
import {CREATE_POI, EDIT_POI} from "src/graphql/mutations/poi";
|
||||
import {updateInItems} from "stores/helpers";
|
||||
import {mutate, query, updateInItems} from "stores/helpers";
|
||||
import {ref} from "vue";
|
||||
|
||||
|
||||
export const usePointsOfInterestStore = defineStore('PointsOfInterest', {
|
||||
state: () => ({
|
||||
items: [],
|
||||
isMutationRunning: false,
|
||||
}),
|
||||
export const usePointsOfInterestStore = defineStore('PointsOfInterest', () => {
|
||||
|
||||
getters: {
|
||||
getDetail: (state) => (poiId) => {
|
||||
return state.items.find(p => p.id === poiId);
|
||||
},
|
||||
},
|
||||
const pointOfInterest = ref(null);
|
||||
const pointsOfInterest = ref([]);
|
||||
const isMutationRunning = ref(false);
|
||||
const isQueryRunning = ref(false);
|
||||
|
||||
actions: {
|
||||
create(input) {
|
||||
this.isMutationRunning = true;
|
||||
return apolloWrapper.mutate(CREATE_POI, {input}).then((data) => {
|
||||
const poi = data.data.createPointOfInterest;
|
||||
this.items.push(poi);
|
||||
return poi;
|
||||
}).finally(() => this.isMutationRunning = false);
|
||||
},
|
||||
update(id, input) {
|
||||
this.isMutationRunning = true;
|
||||
const fetchDetail = (id) => {
|
||||
return query({
|
||||
isQueryRunning,
|
||||
query: POI_DETAIL,
|
||||
variables: {id},
|
||||
}).then(item => pointOfInterest.value = item);
|
||||
};
|
||||
|
||||
return apolloWrapper.mutate(EDIT_POI, {id, input}).then((data) => {
|
||||
const poi = data.data.editPointOfInterest;
|
||||
updateInItems(this.items, "id", poi);
|
||||
return poi;
|
||||
}).finally(() => this.isMutationRunning = false);
|
||||
},
|
||||
const fetch = () => {
|
||||
return query({
|
||||
isQueryRunning,
|
||||
query: POI_LIST,
|
||||
}).then(items => pointsOfInterest.value = items);
|
||||
}
|
||||
|
||||
fetchDetail(id) {
|
||||
if (typeof id !== "number") {
|
||||
id = parseInt(id);
|
||||
}
|
||||
return apolloWrapper.query(POI_DETAIL, {id}).then(result => result.data.pointOfInterest);
|
||||
},
|
||||
fetch() {
|
||||
return apolloWrapper.query(POI_LIST).then(result => {
|
||||
this.items = result.data.pointsOfInterest;
|
||||
return result.data.pointsOfInterest;
|
||||
});
|
||||
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
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user