Skip to content

Commit

Permalink
fixed notifications and async mecanism
Browse files Browse the repository at this point in the history
  • Loading branch information
Ratatinator97 committed Jun 11, 2024
1 parent 77f5fcf commit d7785c4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 33 deletions.
12 changes: 8 additions & 4 deletions components/navigation/pictalk-navbar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@
:class="'customButton ' + colorClass" />
</b-tooltip>

<b-tooltip v-if="getUserNotifications() && getUserNotifications().length != 0" position="is-bottom" multilined
size="is-small" type="is-primary" :label="$t('TooltipNotifications')" :delay="1000" :triggers="['hover']">
<b-tooltip v-if="notifications && notifications.length != 0" position="is-bottom" multilined size="is-small"
type="is-primary" :label="$t('TooltipNotifications')" :delay="1000" :triggers="['hover']">
<b-dropdown position="is-bottom-left" aria-role="menu" trap-focus append-to-body
class="notificationsdrop"><template #trigger>
<b-button style="background-color: hsl(0, 100%, 100%); color: #ff5757" icon-right="bell-alert"
class="customButton" />
</template>
<b-dropdown-item aria-role="menu-item" :focusable="false" custom class="lessPadding limitHeight">
<div v-for="notification in getUserNotifications()" :key="notification.operation + Math.random()"
<div v-for="notification in notifications" :key="notification.operation + Math.random()"
class="card lessPadding notification">
<div class="card-content noPadding">
<div class="media">
Expand All @@ -94,7 +94,7 @@
<figure class="image is-64x64">
<img @click="
notificationGoToCollectionOrReturn(notification)
" :src="getNotificationImage(notification)" alt="Placeholder image" />
" :src="notification.image" alt="Placeholder image" />
</figure>
<p class="title is-6 notifTitle greyback">
<!--<b-icon
Expand Down Expand Up @@ -221,7 +221,9 @@ export default {
},
async fetch() {
if (process.client) {
console.log(this.notifications)
this.notifications = await this.$store.dispatch("getNotifications");
console.log(this.notifications)
}
},
data() {
Expand Down Expand Up @@ -561,7 +563,9 @@ export default {
}
},
async getNotificationImage(notification) {
console.log(notification.affected);
const collection = await this.getCollectionFromId(parseInt(notification.affected, 10));
console.log("collection", collection);
return collection?.image;
},
notificationGoToCollectionOrReturn(notification) {
Expand Down
4 changes: 2 additions & 2 deletions pages/pictalk/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,10 @@ export default {
async dispatchDownloadCollections() {
await this.$store.dispatch("downloadCollections");
},
loadedSidebarPictos() {
async loadedSidebarPictos() {
return this.loadPictos(this.$store.getters.getSidebarId);
},
loadedPictos() {
async loadedPictos() {
return this.loadPictos(
parseInt(this.$route.query.fatherCollectionId, 10)
);
Expand Down
42 changes: 15 additions & 27 deletions store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ export const actions = {
col.pictos = col.pictos.filter((v, i, a) => a.findIndex(t => (t.id === v.id)) === i);
return col;
}));
console.log(editedCollections);
return db.collection.bulkPut(editedCollections);
},
async dbAddPicto(state, pictos) {
Expand Down Expand Up @@ -421,7 +420,6 @@ export const actions = {
},
async editCollection(vuexContext, collection) {
let formData = new FormData();
console.log("editedCollection: ", collection)
if (collection.speech) {
formData.append("speech", JSON.stringify(collection.speech));
}
Expand Down Expand Up @@ -551,7 +549,6 @@ export const actions = {
}
},
async logout(vuexContext) {
console.log("Logging out")
vuexContext.commit("clearToken");
Cookie.remove("jwt");
Cookie.remove("expirationDate");
Expand Down Expand Up @@ -626,7 +623,6 @@ export const actions = {
let pictosToEdit = [];
let collectionsWithoutFatherCollectionId = [];
toUpdate = await Promise.all(toUpdate);
console.log(toUpdate)
for (let update of toUpdate) {
collectionsToCreate = collectionsToCreate.concat(update.collectionsToCreate);
collectionsToEdit = collectionsToEdit.concat(update.collectionsToEdit);
Expand All @@ -635,14 +631,12 @@ export const actions = {
collectionsWithoutFatherCollectionId = collectionsWithoutFatherCollectionId.concat(update.collectionsWithoutFatherCollectionId);
}
if (collectionsWithoutFatherCollectionId.length > 0) {
console.log("DownloadCollections, collectionsWithoutFatherCollectionId: ", collectionsWithoutFatherCollectionId);
}
// We can find the collectionsWithoutFatherCollectionId in the collectionsToCreate or to edit
let count = 0;
for (let collection of collectionsWithoutFatherCollectionId) {
const index = collectionsToCreate.findIndex((col) => col.id == collection.id);
if (index != -1) {
console.log("Merging collection with id: ", collection.id);
collectionsToCreate[index].fatherCollectionId = collection.fatherCollectionId;
collectionsToCreate.splice(index, 1);
collectionsToCreate.push(collection);
Expand All @@ -651,15 +645,13 @@ export const actions = {
if (index == -1) {
const index2 = collectionsToEdit.findIndex((col) => col.id == collection.id);
if (index2 != -1) {
console.log("Merging collection with id: ", collection.id);
collectionsToEdit[index2].fatherCollectionId = collection.fatherCollectionId;
collectionsToEdit.splice(index2, 1);
collectionsToEdit.push(collection);
count += 1;
}
}
}
console.log("Merged collections: ", count);

if (collectionsToCreate.length > 0) {
await vuexContext.dispatch("dbAddCollection", collectionsToCreate);
Expand Down Expand Up @@ -750,11 +742,11 @@ export const actions = {
const notificationsRequest = await axios.get("/user/notification");
if (notificationsRequest.status !== 200) return;

const notifications = notificationsRequest.data;
let notifications = notificationsRequest.data;
if (notifications?.length != vuexContext.getters.getUser.notifications.length) {
vuexContext.dispatch("downloadCollections");
}
notifications?.forEach(async (notification) => {
notifications = await Promise.all(notifications?.map(async (notification) => {
if (notification.meaning) {
try {
notification.meaning = JSON.parse(notification?.meaning)
Expand All @@ -763,19 +755,15 @@ export const actions = {
}
}
if (notification.affected) {
if (!getCollectionFromId(vuexContext, parseInt(notification.affected, 10))) {
var res = await axios.get("/collection/find/" + parseInt(notification.affected, 10));
parseAndUpdateEntireCollection(vuexContext, res.data);
}
if (notification.affected) {
if (!await getCollectionFromId(vuexContext, parseInt(notification.affected, 10))) {
var res = await axios.get("/collection/find/" + parseInt(notification.affected, 10));
await parseAndUpdateEntireCollection(vuexContext, res.data);
}
notification.image = (await getCollectionFromId(vuexContext, parseInt(notification.affected, 10)))?.image;
let collection = await getCollectionFromId(vuexContext, parseInt(notification.affected, 10));
if (!collection) {
await vuexContext.dispatch('fetchCollection', parseInt(notification.affected, 10));
}
collection = await getCollectionFromId(vuexContext, parseInt(notification.affected, 10));
notification.image = collection?.image;
}
});
return notification;
}));
// Mettre les notifications dans user
let user = { ...vuexContext.getters.getUser };
user.notifications = notifications;
Expand Down Expand Up @@ -923,8 +911,8 @@ async function parseAndUpdateEntireCollection(vuexContext, collection, download
}
}
if (collection.pictos && !collection.pictos.length == 0) {
collection.pictos.map((picto) => {
let localPicto = getPictoFromId(vuexContext, picto.id);
await Promise.all(collection.pictos.map(async (picto) => {
let localPicto = await getPictoFromId(vuexContext, picto.id);
let existsPicto = localPicto?.id == picto.id;
let updatePicto = (localPicto?.updatedDate != picto.updatedDate) && existsPicto;
if (!existsPicto || updatePicto) {
Expand All @@ -943,11 +931,11 @@ async function parseAndUpdateEntireCollection(vuexContext, collection, download
}
}

});
}));
}
if (collection.collections && !collection.collections.length == 0) {
collection.collections.map((col) => {
let localCollections = getCollectionFromId(vuexContext, col.id);
await Promise.all(collection.collections.map(async (col) => {
let localCollections = await getCollectionFromId(vuexContext, col.id);
let existsCollections = localCollections?.id == col.id;
let updateCollection = (localCollections?.updatedDate != col.updatedDate) && existsCollections;
const partialCollection = localCollections?.partial;
Expand Down Expand Up @@ -976,7 +964,7 @@ async function parseAndUpdateEntireCollection(vuexContext, collection, download
collectionsToEdit.push(col);
}
}
});
}));
}
if (!download) {
if (collectionsToCreate.length > 0) {
Expand Down

0 comments on commit d7785c4

Please sign in to comment.