Moznost nahravat fotky copilotem
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
import {computed, ref, watch} from "vue";
|
||||
import {confirmObject} from "src/composables/dialog";
|
||||
import {useQuasar} from "quasar";
|
||||
|
||||
|
||||
export const usePhotoBatchManagement = ({photos, emits}) => {
|
||||
const batchEdit = ref({});
|
||||
const $q = useQuasar();
|
||||
|
||||
// console.log(photos);
|
||||
for (const photo of photos) {
|
||||
batchEdit.value[photo.id] = false;
|
||||
}
|
||||
|
||||
const handleImageClick = (photo, idx) => {
|
||||
batchEdit.value[photo.id] = !batchEdit.value[photo.id];
|
||||
}
|
||||
|
||||
const selectedPhotos = computed(() => {
|
||||
const selected = [];
|
||||
for (const i in batchEdit.value) {
|
||||
const photo = photos.find(p => p.id === parseInt(i));
|
||||
if (photo && batchEdit.value[i]) selected.push(photo);
|
||||
}
|
||||
|
||||
return selected;
|
||||
});
|
||||
|
||||
const confirmBatchPhotoDelete = () => {
|
||||
$q.dialog(confirmObject("Opravdu chces smazat vybrané fotky?"))
|
||||
.onOk(() => {
|
||||
for (const id in batchEdit.value) {
|
||||
if (!batchEdit.value[id]) continue;
|
||||
emits("delete", parseInt(id));
|
||||
batchEdit.value[id] = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
const openBatchEdit = () => {
|
||||
const photos = selectedPhotos.value;
|
||||
for (const i in batchEdit.value) {
|
||||
batchEdit.value[i] = false;
|
||||
}
|
||||
emits('batch-edit', photos);
|
||||
}
|
||||
|
||||
const batchRotate = (direction) => {
|
||||
const photos = selectedPhotos.value;
|
||||
emits("batch-rotate", {photos, direction});
|
||||
}
|
||||
|
||||
const isBatchEditAllowed = computed(() => Object.values(batchEdit.value).filter(v => v).length > 0);
|
||||
|
||||
const checkAll = ref(false);
|
||||
watch(() => checkAll.value, (newVal) => {
|
||||
if (newVal === null) return;
|
||||
|
||||
for (const photo of photos) {
|
||||
batchEdit.value[photo.id] = newVal;
|
||||
}
|
||||
});
|
||||
|
||||
const batchOptionsProps = computed(() => ({
|
||||
round: true,
|
||||
flat: true,
|
||||
class: "q-ml-lg",
|
||||
disable: !isBatchEditAllowed.value
|
||||
}));
|
||||
|
||||
watch(() => batchEdit.value, () => {
|
||||
let checkedAll = true;
|
||||
let uncheckedAll = true;
|
||||
|
||||
for (const i of Object.keys(batchEdit.value)) {
|
||||
if (typeof batchEdit.value[i] === 'undefined') continue;
|
||||
|
||||
if (batchEdit.value[i]) uncheckedAll = false;
|
||||
if (!batchEdit.value[i]) checkedAll = false;
|
||||
}
|
||||
|
||||
if (checkedAll) {
|
||||
checkAll.value = true;
|
||||
} else if (uncheckedAll) {
|
||||
checkAll.value = false;
|
||||
} else {
|
||||
checkAll.value = null;
|
||||
}
|
||||
}, {deep: true});
|
||||
|
||||
return {
|
||||
batchEdit, handleImageClick, selectedPhotos, confirmBatchPhotoDelete,
|
||||
openBatchEdit, batchRotate, checkAll, isBatchEditAllowed, batchOptionsProps
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user