0
0
Fork 0
This commit is contained in:
Pau 2022-11-24 09:32:15 +01:00
parent 7d198a819b
commit cbeeea8f88
3 changed files with 152 additions and 44 deletions

View File

@ -292,11 +292,8 @@ export default {
notificationsManager: { notificationsManager: {
activeNotifications: 'Active notifications', activeNotifications: 'Active notifications',
availableNotifications: 'Available notifications', availableNotifications: 'Available notifications',
table: { subscribed: 'Subscribed to the notification',
name: 'Permission description', unsubscribed: 'Unsubscribed from the notification',
subscribed: 'Subscribed',
role: 'Necessary role',
}
}, },
}, },
components: { components: {

View File

@ -290,6 +290,8 @@ export default {
notificationsManager: { notificationsManager: {
activeNotifications: 'Notificaciones activas', activeNotifications: 'Notificaciones activas',
availableNotifications: 'Notificaciones disponibles', availableNotifications: 'Notificaciones disponibles',
subscribed: 'Te has suscrito a la notificación',
unsubscribed: 'Te has dado de baja de la notificación',
}, },
}, },
components: { components: {

View File

@ -2,6 +2,7 @@
import { onMounted, computed, ref } from 'vue'; import { onMounted, computed, ref } from 'vue';
import { useRoute } from 'vue-router'; import { useRoute } from 'vue-router';
import { useI18n } from 'vue-i18n'; import { useI18n } from 'vue-i18n';
import { useQuasar } from 'quasar';
import { useSession } from 'src/composables/useSession'; import { useSession } from 'src/composables/useSession';
import axios from 'axios'; import axios from 'axios';
@ -18,8 +19,7 @@ onMounted(async () => await fetch());
const route = useRoute(); const route = useRoute();
const { t } = useI18n(); const { t } = useI18n();
const { getToken } = useSession(); const { getToken } = useSession();
const quasar = useQuasar();
t == t;
const entityId = computed(() => { const entityId = computed(() => {
return $props.id || route.params.id; return $props.id || route.params.id;
@ -47,6 +47,9 @@ const workerFilter = {
}, },
], ],
}; };
const notificationAcls = ref([]);
async function fetch() { async function fetch() {
const { data } = await axios.get(`Workers/${entityId.value}`, { const { data } = await axios.get(`Workers/${entityId.value}`, {
params: { params: {
@ -81,14 +84,15 @@ async function fetch() {
data.subscribedNotifs = subscribedNotifs.data; data.subscribedNotifs = subscribedNotifs.data;
const filterAcl = { const filterAcl = {
where: {
roleFk: data.user.roleFk,
},
include: [ include: [
{ {
relation: 'notification', relation: 'notification',
scope: { scope: {
relation: 'notificationSubscription', include: [
{
relation: 'subscription',
},
],
}, },
}, },
{ {
@ -106,13 +110,121 @@ async function fetch() {
}, },
}); });
data.notifsAcl = notifsAcl.data; let notifications = [];
notifsAcl.data.forEach((acl) => {
let notification = {
id: acl.notification.id,
name: acl.notification.name,
description: acl.notification.description,
active: false,
allowed: false,
};
if (acl.roleFk == data.user.roleFk) {
notification.allowed = true;
} else {
notification.allowed = false;
}
if (data.subscribedNotifs.length > 0) {
data.subscribedNotifs.forEach((sub) => {
if (sub.notificationFk == acl.notification.id) {
if (notification.allowed) {
notification.active = true;
}
}
});
}
// Check if notification is already in the array
let found = false;
notifications.forEach((notif) => {
if (notif.id == notification.id) {
found = true;
}
});
if (!found) {
if (!notification.allowed) {
notification.active = null;
}
notifications.push(notification);
} else {
// Check if current notification is allowed and the other one is not
// If so, replace the existing one with the current one
notifications.forEach((notif) => {
if (notif.id == notification.id) {
if (notification.allowed && !notif.allowed) {
notif.allowed = true;
notif.active = notification.active;
}
}
});
}
});
notificationAcls.value = notifications;
sortNotifs();
} catch (e) { } catch (e) {
console.log(e); console.log(e);
} }
worker.value = data; worker.value = data;
} }
function sortNotifs() {
notificationAcls.value.sort((a, b) => {
if (a.allowed && !b.allowed) {
return -1;
} else if (!a.allowed && b.allowed) {
return 1;
} else {
if (a.active && !b.active) {
return 1;
} else if (!a.active && b.active) {
return -1;
} else {
return 0;
}
}
});
}
async function toggleNotif(notif, chip) {
if (chip) {
notif.active = !notif.active;
}
if (notif.active) {
await axios.post(
`NotificationSubscriptions`,
{
notificationFk: notif.id,
userFk: entityId.value,
},
{
headers: {
Authorization: getToken(),
},
}
);
quasar.notify({
type: 'positive',
message: t('worker.notificationsManager.subscribed'),
});
} else {
await axios.post(`NotificationSubscriptions/deleteSubscription`, {
notificationId: notif.id + '',
userId: entityId.value,
headers: {
Authorization: getToken(),
},
});
quasar.notify({
type: 'positive',
message: t('worker.notificationsManager.unsubscribed'),
});
}
}
</script> </script>
<template> <template>
<q-card> <q-card>
@ -124,49 +236,46 @@ async function fetch() {
{{ t('worker.notificationsManager.activeNotifications') }} {{ t('worker.notificationsManager.activeNotifications') }}
</q-item-label> </q-item-label>
<q-item> <q-item>
<div class="q-pa-sm"> <div v-for="notif in notificationAcls" :key="notif.id">
<q-chip <q-chip
v-for="notif in worker.subscribedNotifs" v-if="notif.active"
:key="notif.notificationFk" :key="notif.id"
:label="notif.notification.name" :label="notif.name"
text-color="white" text-color="white"
color="primary" color="primary"
class="q-mr-sm" class="q-mr-sm"
removable removable
@remove="removeNotif(notif)" @remove="toggleNotif(notif, true)"
/> />
</div> </div>
</q-item> </q-item>
<q-item-label header class="text-h6"> <q-item-label header class="text-h6">
{{ t('worker.notificationsManager.availableNotifications') }} {{ t('worker.notificationsManager.availableNotifications') }}
</q-item-label> </q-item-label>
<q-item>
<q-table :rows="worker.notifsAcl"> <div class="row">
<template #header="props"> <q-item
<q-tr :props="props"> class="col"
<q-th auto-width>{{ t('worker.notificationsManager.table.name') }}</q-th> :key="notif.id"
<q-th auto-width>{{ t('worker.notificationsManager.table.subscribed') }}</q-th> v-for="notif in notificationAcls"
<q-th auto-width>{{ t('worker.notificationsManager.table.role') }}</q-th> style="min-width: 350px; max-width: 350px"
</q-tr> >
</template> <q-item-section>
<template #body="props"> <q-item-label>{{ notif.name }}</q-item-label>
<q-tr :props="props"> <q-item-label caption>{{ notif.description }}</q-item-label>
<q-td>{{ props.row.notification.description }}</q-td> </q-item-section>
<q-td> <q-item-section side top>
<q-toggle <q-toggle
:disable="!notif.allowed"
checked-icon="check" checked-icon="check"
unchecked-icon="close" unchecked-icon="close"
indeterminate-icon="block" indeterminate-icon="block"
v-model="props.row.subscribed" v-model="notif.active"
:value="props.row.subscribed" @update:model-value="toggleNotif(notif)"
@input="toggleNotif(props.row)"
/> />
</q-td> </q-item-section>
<q-td>{{ props.row.role.name }}</q-td>
</q-tr>
</template>
</q-table>
</q-item> </q-item>
</div>
</q-list> </q-list>
</template> </template>
</q-card> </q-card>