90 lines
3.0 KiB
JavaScript
90 lines
3.0 KiB
JavaScript
import {ref} from "vue";
|
|
import {usePhotosStore} from "stores/Photos";
|
|
import {errorObject, successObject, warningObject} from "src/composables/dialog";
|
|
import {useQuasar} from "quasar";
|
|
import {usePointsOfInterestStore} from "stores/PointsOfInterest";
|
|
import {storeToRefs} from "pinia";
|
|
import {useFlightsStore} from "stores/Flights";
|
|
|
|
export const useBasicPhotoOperations = ({copilotUploadToken}) => {
|
|
const $q = useQuasar();
|
|
|
|
const {flight} = storeToRefs(useFlightsStore());
|
|
const {photos} = storeToRefs(usePhotosStore());
|
|
|
|
// TODO: V PRIVATNI CASTI JE TO ZATIM ROZBITE!!!
|
|
|
|
|
|
const uploadInProgress = ref(false);
|
|
const handlePhotosUpload = (data) => {
|
|
const promises = [];
|
|
uploadInProgress.value = true;
|
|
|
|
for (const photo of data.photos) {
|
|
promises.push(usePhotosStore().upload(flight.value.id, {photo, copilotUploadToken}).then((data) => {
|
|
photos.value.push(data);
|
|
photo._uploaded = true;
|
|
}));
|
|
}
|
|
|
|
Promise.allSettled(promises).then((values) => {
|
|
const success = values.filter(v => v.status === 'fulfilled');
|
|
const failed = values.filter(v => v.status === 'rejected'); // TODO: nebo vse - success?
|
|
|
|
if (success.length > 0)
|
|
$q.notify(successObject(`Úspěšně nahráno ${success.length} fotek`));
|
|
|
|
if (failed.length > 0)
|
|
$q.notify(warningObject(`Nepovedlo se nahrát ${failed.length} fotek. Zde jsou důvody: ${failed.map(f => f.reason)}`));
|
|
|
|
data.photos = [];
|
|
}).finally(() => uploadInProgress.value = false);
|
|
}
|
|
|
|
const _findPhotoIndex = (photoId) => photos.value.findIndex((object) => object.id === photoId);
|
|
const deletePhoto = (id) => {
|
|
usePhotosStore().destroy(id, copilotUploadToken).then((data) => {
|
|
photos.value.splice(_findPhotoIndex(data.id), 1);
|
|
});
|
|
}
|
|
|
|
const batchRotatePhotos = (data) => {
|
|
const selectedPhotos = data.photos;
|
|
const direction = data.direction;
|
|
|
|
const promises = [];
|
|
for (const photo of selectedPhotos) {
|
|
promises.push(usePhotosStore().changeOrientation(photo.id, direction, copilotUploadToken).then(data => {
|
|
const idx = _findPhotoIndex(data.id);
|
|
data.thumbnailUrl += "?t=" + (new Date()).getTime();
|
|
photos.value[idx] = data;
|
|
}));
|
|
}
|
|
|
|
let success = 0;
|
|
Promise.allSettled(promises).then(result => {
|
|
for (const item of result) {
|
|
if (item.status === 'fulfilled') success++;
|
|
else console.error(item.reason)
|
|
}
|
|
const error = result.length - success;
|
|
if (success > 0) $q.notify(successObject("Otočeno " + success + " fotografií"));
|
|
if (error > 0) $q.notify(errorObject(`Nepovedlo se otočit ${error} fotografi`))
|
|
});
|
|
}
|
|
|
|
const updateSinglePhoto = (id, formData) => {
|
|
formData.copilots = formData.copilots.map(c => ({id: c.id, name: c.name}));
|
|
|
|
return usePhotosStore().update(id, formData).then((data) => {
|
|
const idx = _findPhotoIndex(data.id);
|
|
photos.value[idx] = data;
|
|
usePointsOfInterestStore().fetch({});
|
|
});
|
|
}
|
|
|
|
return {
|
|
uploadInProgress, handlePhotosUpload, batchRotatePhotos, deletePhoto, updateSinglePhoto
|
|
}
|
|
}
|