Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Implemented: firebase database connectivity with apps #252

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -103,13 +103,15 @@ export let dxpComponents = {

export {
appContext,
addDocument,
DxpImage,
DxpLogin,
DxpMenuFooterNavigation,
DxpOmsInstanceNavigator,
DxpProductIdentifier,
DxpShopifyImg,
DxpUserProfile,
getDocument,
getProductIdentificationValue,
goToOms,
i18n,
Expand All @@ -120,6 +122,7 @@ export {
productIdentificationContext,
shopifyImgContext,
translate,
updateDocument,
useAuthStore,
useProductIdentificationStore,
useUserStore,
Expand Down
69 changes: 47 additions & 22 deletions src/utils/firebase.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { initializeApp } from "firebase/app";
import { getMessaging, getToken, onMessage } from "firebase/messaging";
import { doc, getFirestore, getDoc, setDoc, updateDoc } from "firebase/firestore";

let app;
let database = {} as any

const initialiseFirebaseApp = async (
appFirebaseConfig: any,
Expand All @@ -9,31 +13,52 @@ const initialiseFirebaseApp = async (
) => {
const firebaseConfig = appFirebaseConfig

const app = initializeApp(firebaseConfig);
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.");
app = initializeApp(firebaseConfig);
database = getFirestore(app);

// 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.");
}
}
};

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
}
Loading