Ne uplne otestovany prepis storu
This commit is contained in:
+66
-52
@@ -1,65 +1,79 @@
|
||||
import {defineStore} from "pinia";
|
||||
import {apolloWrapper} from "src/graphql/apollo";
|
||||
import {ADJUST_PHOTO, CHANGE_PHOTO_ORIENTATION, DELETE_PHOTO, EDIT_PHOTO, UPLOAD_PHOTO} from "src/graphql/mutations/photo";
|
||||
import {PHOTO_DETAIL} from "src/graphql/queries/photo";
|
||||
import {ref} from "vue";
|
||||
import {mutate, query} from "stores/helpers";
|
||||
|
||||
export const usePhotosStore = defineStore('Photos', {
|
||||
state: () => ({
|
||||
isMutationRunning: false,
|
||||
}),
|
||||
export const usePhotosStore = defineStore('Photos', () => {
|
||||
const photo = ref(null);
|
||||
const isMutationRunning = ref(false);
|
||||
const isQueryRunning = ref(false);
|
||||
|
||||
actions: {
|
||||
fetchDetail(id) {
|
||||
return apolloWrapper.query(PHOTO_DETAIL, {id}).then(({data}) => {
|
||||
return data.photo;
|
||||
});
|
||||
},
|
||||
upload(flightId, formData) {
|
||||
if (!flightId) {
|
||||
console.error("No flightId specified for photo upload! Aborting");
|
||||
return;
|
||||
}
|
||||
const fetchDetail = (id) => query({
|
||||
isQueryRunning,
|
||||
query: PHOTO_DETAIL,
|
||||
variables: {id}
|
||||
}).then(item => photo.value = item);
|
||||
|
||||
formData.flightId = flightId;
|
||||
const upload = (flightId, input) => {
|
||||
if (!flightId) {
|
||||
console.error("No flightId specified for photo upload! Aborting");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!formData.name) {
|
||||
formData.name = "";
|
||||
}
|
||||
input.flightId = flightId;
|
||||
|
||||
if (!formData.description) {
|
||||
formData.description = "";
|
||||
}
|
||||
if (!input.name) {
|
||||
input.name = "";
|
||||
}
|
||||
|
||||
this.isMutationRunning = true;
|
||||
return apolloWrapper.mutate(UPLOAD_PHOTO, {input: formData}).then(({data}) => {
|
||||
return data.uploadPhoto;
|
||||
}).finally(() => this.isMutationRunning = false);
|
||||
},
|
||||
update(id, formData) {
|
||||
this.isMutationRunning = true;
|
||||
return apolloWrapper.mutate(EDIT_PHOTO, {id, input: formData}).then(({data}) => {
|
||||
return data.editPhoto;
|
||||
}).finally(() => this.isMutationRunning = false);
|
||||
},
|
||||
if (!input.description) {
|
||||
input.description = "";
|
||||
}
|
||||
|
||||
changeOrientation(id, direction) {
|
||||
this.isMutationRunning = true;
|
||||
return apolloWrapper.mutate(CHANGE_PHOTO_ORIENTATION, {id, direction}).then(({data}) => {
|
||||
return data.changeOrientation;
|
||||
}).finally(() => this.isMutationRunning = false);
|
||||
},
|
||||
// TODO: ^^ patri tohle do storu?
|
||||
|
||||
adjust(id, adjustment) {
|
||||
this.isMutationRunning = true;
|
||||
return apolloWrapper.mutate(ADJUST_PHOTO, {id, adjustment}).then(({data}) => {
|
||||
return data.adjustPhoto;
|
||||
}).finally(() => this.isMutationRunning = false);
|
||||
},
|
||||
delete(photoId) {
|
||||
this.isMutationRunning = true;
|
||||
return apolloWrapper.mutate(DELETE_PHOTO, {id: photoId}).then(({data}) => {
|
||||
return data.deletePhoto;
|
||||
}).finally(() => this.isMutationRunning = false);
|
||||
return mutate({
|
||||
isMutationRunning,
|
||||
mutation: UPLOAD_PHOTO,
|
||||
variables: {input}
|
||||
})
|
||||
};
|
||||
|
||||
const update = (id, input) => mutate({
|
||||
isMutationRunning,
|
||||
mutation: EDIT_PHOTO,
|
||||
variables: {id, input}
|
||||
}).then(item => {
|
||||
photo.value = item;
|
||||
return item;
|
||||
})
|
||||
|
||||
const changeOrientation = (id, direction) => mutate({
|
||||
isMutationRunning,
|
||||
mutation: CHANGE_PHOTO_ORIENTATION,
|
||||
variables: {id, direction}
|
||||
}).then(item => {
|
||||
photo.value = item;
|
||||
return item;
|
||||
});
|
||||
|
||||
const adjust = (id, adjustment) => mutate({
|
||||
isMutationRunning,
|
||||
mutation: ADJUST_PHOTO,
|
||||
variables: {id, adjustment}
|
||||
}).then(item => {
|
||||
photo.value = item;
|
||||
return item;
|
||||
});
|
||||
|
||||
const destroy = (id) => mutate({
|
||||
isMutationRunning,
|
||||
mutation: DELETE_PHOTO,
|
||||
variables: {id}
|
||||
});
|
||||
|
||||
return {
|
||||
photo, isMutationRunning, isQueryRunning, fetchDetail, upload, update, changeOrientation, adjust, destroy
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user