From 880d581b929a93146aed2fd456577fd4395cc3ad Mon Sep 17 00:00:00 2001 From: Alexandros SIDIRAS Date: Mon, 3 Feb 2025 19:18:45 +0100 Subject: [PATCH] use edit V2 endpoint for less data loss --- components/navigation/pictalk-navbar.vue | 28 ++++--- mixins/pictogram.js | 102 +++++++++++++---------- store/index.js | 60 +++++++++++++ 3 files changed, 134 insertions(+), 56 deletions(-) diff --git a/components/navigation/pictalk-navbar.vue b/components/navigation/pictalk-navbar.vue index 37021f7..adcada7 100644 --- a/components/navigation/pictalk-navbar.vue +++ b/components/navigation/pictalk-navbar.vue @@ -88,8 +88,8 @@ {{ notification.username .split("@")[0] - .replace(".", " ") - }} + .replace(".", " ") + }} {{ notificationText(notification) }}

@@ -519,19 +519,27 @@ export default { id: this.$store.getters.getShortcutCollectionId.collectionId, }); - await this.$store.dispatch("editCollection", { - id: collection.id, - pictos: collection.pictos, - }); + await this.$store.dispatch("editCollectionV2", { + collection: { id: collection.id }, + collectionsAdded: [], + pictosAdded: [this.$store.getters.getShortcutCollectionId.collectionId], + collectionsRemoved: [], + pictosRemoved: [] + }, + ); $nuxt.$emit("resyncPictoList"); } else { collection.collections.push({ id: this.$store.getters.getShortcutCollectionId.collectionId, }); - await this.$store.dispatch("editCollection", { - id: collection.id, - collections: collection.collections, - }); + await this.$store.dispatch("editCollectionV2", + { + collection: { id: collection.id }, + collectionsAdded: [this.$store.getters.getShortcutCollectionId.collectionId], + pictosAdded: [], + collectionsRemoved: [], + pictosRemoved: [] + }); $nuxt.$emit("resyncPictoList"); } diff --git a/mixins/pictogram.js b/mixins/pictogram.js index 998b8dc..ebf54ca 100644 --- a/mixins/pictogram.js +++ b/mixins/pictogram.js @@ -15,9 +15,12 @@ export default { if (isPicto) { collection.pictos.push(item); try { - await this.$store.dispatch("editCollection", { - id: collection.id, - pictos: collection.pictos, + await this.$store.dispatch("editCollectionV2", { + collection: { id: collection.id }, + collectionsAdded: [], + pictosAdded: [item.id], + collectionsRemoved: [], + pictosRemoved: [], }); const notif = this.$buefy.toast.open({ message: this.$t("PublicCopy"), @@ -32,10 +35,15 @@ export default { } else { collection.collections.push(item); try { - await this.$store.dispatch("editCollection", { - id: collection.id, - collections: collection.collections, - }); + await this.$store.dispatch("editCollectionV2", + { + collection: { id: collection.id }, + collectionsAdded: [item.id], + pictosAdded: [], + collectionsRemoved: [], + pictosRemoved: [], + }, + ); const notif = this.$buefy.toast.open({ message: this.$t("PublicCopy"), type: "is-success", @@ -117,36 +125,34 @@ export default { let sidebar = await this.getCollectionFromId(parseInt(sidebarId, 10)); let currentCollection = await this.getCollectionFromId(parseInt(this.$route.query.fatherCollectionId, 10)) - console.log(sidebar, currentCollection); if (isPicto) { - sidebar.pictos.push({ - id: collectionId, - }); - // Remove the picto from the current collection - currentCollection.pictos = currentCollection.pictos.filter((picto) => picto.id != collectionId); - - await this.$store.dispatch("editCollection", { - id: sidebar.id, - pictos: sidebar.pictos, - }); - await this.$store.dispatch("editCollection", { - id: currentCollection.id, - collections: currentCollection.pictos, + await this.$store.dispatch("editCollectionV2", + { + collection: { id: sidebar.id }, + collectionsAdded: [], + pictosAdded: [collectionId], + collectionsRemoved: [], + pictosRemoved: [], + }, + ); + await this.$store.dispatch("removePicto", { + pictoId: collectionId, + fatherCollectionId: currentCollection.id }); } else { - sidebar.collections.push({ - id: collectionId, - }); currentCollection.collections = currentCollection.collections.filter((picto) => picto.id != collectionId); - await this.$store.dispatch("editCollection", { - id: sidebar.id, - collections: sidebar.collections, - }); - await this.$store.dispatch("editCollection", { - id: currentCollection.id, - collections: currentCollection.collections, + await this.$store.dispatch("editCollectionV2", + { + collection: { id: sidebar.id }, + collectionsAdded: [collectionId], + pictosAdded: [], + collectionsRemoved: [], + pictosRemoved: [], + }); + await this.$store.dispatch("removeCollection", { + collectionId: collectionId, + fatherCollectionId: currentCollection.id, }); - } $nuxt.$emit("resyncPictoList"); } catch (error) { @@ -184,26 +190,30 @@ export default { ) ); if (!isPicto) { - currentCollection.collections.push({ - id: collectionId, - }); - await this.$store.dispatch("editCollection", { - id: currentCollection.id, - collections: currentCollection.collections, - }); + await this.$store.dispatch("editCollectionV2", + { + collection: { id: currentCollection.id }, + collectionsAdded: [collectionId], + pictosAdded: [], + collectionsRemoved: [], + pictosRemoved: [], + } + ); await this.$store.dispatch("removeCollection", { collectionId: collectionId, fatherCollectionId: this.$store.getters.getSidebarId, }); } else { - currentCollection.pictos.push({ - id: collectionId, - }); - await this.$store.dispatch("editCollection", { - id: currentCollection.id, - pictos: currentCollection.pictos, - }); + await this.$store.dispatch("editCollectionV2", + { + collection: { id: currentCollection.id }, + collectionsAdded: [], + pictosAdded: [collectionId], + collectionsRemoved: [], + pictosRemoved: [], + } + ); await this.$store.dispatch("removePicto", { pictoId: collectionId, fatherCollectionId: this.$store.getters.getSidebarId diff --git a/store/index.js b/store/index.js index 93e4ef9..774e544 100644 --- a/store/index.js +++ b/store/index.js @@ -504,6 +504,66 @@ export const actions = { ...(collection.pictohubId && { pictohubId: Number(collection.pictohubId) }), }); }, + async editCollectionV2(vuexContext, { collection, collectionsAdded, pictosAdded, collectionsRemoved, pictosRemoved }) { + let formData = new FormData(); + if (collection.speech) { + formData.append("speech", JSON.stringify(collection.speech)); + } + + if (collection.pictohubId) { + formData.append("pictohubId", collection.pictohubId); + } + + if (collection.meaning) { + formData.append("meaning", JSON.stringify(collection.meaning)); + } + if (collection.color) { + formData.append("color", collection.color); + } + if (collectionsAdded) { + collectionsAdded.map((id, index) => formData.append('collectionsAdded[' + index + ']', id)); + } + if (pictosAdded) { + pictosAdded.map((id, index) => formData.append('pictosAdded[' + index + ']', id)); + } + if (collectionsRemoved) { + collectionsRemoved.map((id, index) => formData.append('collectionsRemoved[' + index + ']', id)); + } + if (pictosRemoved) { + pictosRemoved.map((id, index) => formData.append('pictosRemoved[' + index + ']', id)); + } + if (collection.share) { + formData.append("share", collection.share); + } + if (collection.priority) { + formData.append("priority", collection.priority); + } + if (collection.image) { + formData.append("image", collection.image); + } + console.log(formData) + const editedCollection = (await axios + .put("/collection/V2/" + collection.id, formData, { + headers: { + "Content-Type": "multipart/form-data" + } + })).data; + const nestedCollections = await Promise.all(editedCollection.collections.map((colle) => parseAndUpdateEntireCollection(vuexContext, colle))); + const nestedPictos = await Promise.all(editedCollection.pictos.map((pict) => parseAndUpdatePictogram(vuexContext, pict))); + await vuexContext.dispatch("dbEditCollection", { + ...editedCollection, + ...(editedCollection.meaning && { meaning: editedCollection.meaning }), + ...(editedCollection.speech && { speech: editedCollection.speech }), + ...(editedCollection.priority && { priority: JSON.parse(editedCollection.priority) }), + image: axios.defaults.baseURL + "/image/pictalk/" + editedCollection.image, + createdDate: editedCollection.createdDate, + updatedDate: editedCollection.updatedDate, + collections: nestedCollections, + pictos: nestedPictos, + collection: true, + ...(collection.pictohubId && { pictohubId: Number(collection.pictohubId) }), + }); + }, async removeCollection(vuexContext, { collectionId, fatherCollectionId }) { const res = await axios.delete("/collection/", { params: { collectionId: collectionId, fatherId: fatherCollectionId } }); const parentCollection = await vuexContext.dispatch("getCollectionFromId", fatherCollectionId);