From 82e8b7c1a5f0123c05c51d3bf1ec07f498a0351f Mon Sep 17 00:00:00 2001 From: amansinghbais Date: Thu, 11 Jan 2024 19:06:08 +0530 Subject: [PATCH 1/2] Improved: firebase code to run notification methods only in case of Bopis app --- src/utils/firebase.ts | 50 +++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/src/utils/firebase.ts b/src/utils/firebase.ts index 7ca8626a..26deade8 100644 --- a/src/utils/firebase.ts +++ b/src/utils/firebase.ts @@ -1,5 +1,7 @@ import { initializeApp } from "firebase/app"; import { getMessaging, getToken, onMessage } from "firebase/messaging"; +import { defineStore } from "pinia"; +let app; const initialiseFirebaseApp = async ( appFirebaseConfig: any, @@ -9,29 +11,35 @@ const initialiseFirebaseApp = async ( ) => { const firebaseConfig = appFirebaseConfig - const app = initializeApp(firebaseConfig); - const messaging = getMessaging(app); - const permission = await Notification.requestPermission(); + app = initializeApp(firebaseConfig); - if (permission === "granted") { - const token = await getToken(messaging, { - vapidKey: appFirebaseVapidKey - }); - await storeClientRegistrationToken(token) - - // handle foreground message - onMessage(messaging, (payload: any) => { - addNotification({ notification: payload, isForeground: true }); - }); - - // handle background message (service worker) - const broadcast = new BroadcastChannel('FB_BG_MESSAGES'); - broadcast.onmessage = (event) => { - addNotification({ notification: event.data, isForeground: false }); - }; - } else { - alert("You denied notifications."); + // Check for notifications required only in bopis app. + if(addNotification) { + const messaging = getMessaging(app); + const permission = await Notification.requestPermission(); + + if (permission === "granted") { + const token = await getToken(messaging, { + vapidKey: appFirebaseVapidKey + }); + await storeClientRegistrationToken(token) + + // handle foreground message + onMessage(messaging, (payload: any) => { + addNotification({ notification: payload, isForeground: true }); + }); + + // handle background message (service worker) + const broadcast = new BroadcastChannel('FB_BG_MESSAGES'); + broadcast.onmessage = (event) => { + addNotification({ notification: event.data, isForeground: false }); + }; + } else { + alert("You denied notifications."); + } } + console.log(app); + }; export { From 04168ae851e0e1222a6bb22b88d23513c30280e0 Mon Sep 17 00:00:00 2001 From: amansinghbais Date: Fri, 12 Jan 2024 12:32:40 +0530 Subject: [PATCH 2/2] Implemented: general basic methods for get and add data to firebase (382-firestoreConfig) --- src/index.ts | 5 ++++- src/utils/firebase.ts | 23 ++++++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index e5983e79..f9ccced4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,7 +5,7 @@ import { useProductIdentificationStore } from "./store/productIdentification"; import { useAuthStore } from "./store/auth"; import { DxpAppVersionInfo, DxpImage, DxpLanguageSwitcher, DxpLogin, DxpMenuFooterNavigation, DxpOmsInstanceNavigator, DxpProductIdentifier, DxpShopifyImg, DxpUserProfile } from "./components"; import { goToOms, getProductIdentificationValue } from "./utils"; -import { initialiseFirebaseApp } from "./utils/firebase" +import { addDocument, getDocument, initialiseFirebaseApp, updateDocument } from "./utils/firebase" import piniaPluginPersistedstate from 'pinia-plugin-persistedstate' import { createI18n } from 'vue-i18n' import { useUserStore } from "./store/user"; @@ -103,6 +103,7 @@ export let dxpComponents = { export { appContext, + addDocument, DxpImage, DxpLogin, DxpMenuFooterNavigation, @@ -110,6 +111,7 @@ export { DxpProductIdentifier, DxpShopifyImg, DxpUserProfile, + getDocument, getProductIdentificationValue, goToOms, i18n, @@ -120,6 +122,7 @@ export { productIdentificationContext, shopifyImgContext, translate, + updateDocument, useAuthStore, useProductIdentificationStore, useUserStore, diff --git a/src/utils/firebase.ts b/src/utils/firebase.ts index 26deade8..12bfc6c7 100644 --- a/src/utils/firebase.ts +++ b/src/utils/firebase.ts @@ -1,7 +1,9 @@ import { initializeApp } from "firebase/app"; import { getMessaging, getToken, onMessage } from "firebase/messaging"; -import { defineStore } from "pinia"; +import { doc, getFirestore, getDoc, setDoc, updateDoc } from "firebase/firestore"; + let app; +let database = {} as any const initialiseFirebaseApp = async ( appFirebaseConfig: any, @@ -12,6 +14,7 @@ const initialiseFirebaseApp = async ( const firebaseConfig = appFirebaseConfig app = initializeApp(firebaseConfig); + database = getFirestore(app); // Check for notifications required only in bopis app. if(addNotification) { @@ -38,10 +41,24 @@ const initialiseFirebaseApp = async ( alert("You denied notifications."); } } - console.log(app); - }; +const getDocument = async (collection: any, document: any) => { + const querySnapshot = await getDoc(doc(database, collection, document)); + return querySnapshot.data() +} + +const addDocument = async (collection: any, document: any, data: any) => { + return await setDoc(doc(database, collection, document), data); +} + +const updateDocument = async (collection: any, document: any, data: any) => { + return await updateDoc(doc(database, collection, document), data); +} + export { + addDocument, initialiseFirebaseApp, + getDocument, + updateDocument } \ No newline at end of file