From a9d51f8f8a38adb291d05eb4b8c1c9f7050c4e81 Mon Sep 17 00:00:00 2001 From: Shariq Ansari Date: Mon, 29 Jan 2024 20:35:33 +0530 Subject: [PATCH] fix: created notifications store to get all notifications --- crm/api/notifications.py | 18 ++++++++++ .../src/components/Layouts/AppSidebar.vue | 4 ++- frontend/src/stores/notifications.js | 36 +++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 crm/api/notifications.py create mode 100644 frontend/src/stores/notifications.js diff --git a/crm/api/notifications.py b/crm/api/notifications.py new file mode 100644 index 00000000..70abee02 --- /dev/null +++ b/crm/api/notifications.py @@ -0,0 +1,18 @@ +import frappe + + +@frappe.whitelist() +def get_notifications(): + if frappe.session.user == "Guest": + frappe.throw("Authentication failed", exc=frappe.AuthenticationError) + + Notification = frappe.qb.DocType("CRM Notification") + query = ( + frappe.qb.from_(Notification) + .select("*") + .where(Notification.to_user == frappe.session.user) + .where(Notification.read == False) + .orderby("creation") + ) + notifications = query.run(as_dict=True) + return notifications diff --git a/frontend/src/components/Layouts/AppSidebar.vue b/frontend/src/components/Layouts/AppSidebar.vue index c2c12c3d..c7ed5b5b 100644 --- a/frontend/src/components/Layouts/AppSidebar.vue +++ b/frontend/src/components/Layouts/AppSidebar.vue @@ -12,7 +12,7 @@ label="Notifications" :icon="NotificationsIcon" :isCollapsed="isSidebarCollapsed" - @click="() => {}" + @click="() => toggleNotificationPanel()" class="mx-2 my-0.5" /> @@ -93,10 +93,12 @@ import CollapseSidebar from '@/components/Icons/CollapseSidebar.vue' import NotificationsIcon from '@/components/Icons/NotificationsIcon.vue' import SidebarLink from '@/components/SidebarLink.vue' import { viewsStore } from '@/stores/views' +import { notificationsStore } from '@/stores/notifications' import { useStorage } from '@vueuse/core' import { computed } from 'vue' const { getPinnedViews, getPublicViews } = viewsStore() +const { toggle: toggleNotificationPanel } = notificationsStore() const isSidebarCollapsed = useStorage('sidebar_is_collapsed', false) const links = [ diff --git a/frontend/src/stores/notifications.js b/frontend/src/stores/notifications.js new file mode 100644 index 00000000..dbe83977 --- /dev/null +++ b/frontend/src/stores/notifications.js @@ -0,0 +1,36 @@ +import { defineStore } from 'pinia' +import { sessionStore } from '@/stores/session' +import { createResource } from 'frappe-ui' +import { reactive, ref } from 'vue' + +export const notificationsStore = defineStore('crm-notifications', () => { + const { user } = sessionStore() + + let visible = ref(false) + let unreadNotifications = reactive([]) + + const notifications = createResource({ + url: 'crm.api.notifications.get_notifications', + cache: 'crm-notifications', + initialData: [], + auto: true, + transform(data) { + unreadNotifications = data + return data + }, + }) + + function toggle() { + visible.value = !visible.value + } + + function getUnreadNotifications() { + return unreadNotifications || [] + } + + return { + visible, + toggle, + getUnreadNotifications, + } +})