wrbapp/frontend/app.vue

149 lines
4.4 KiB
Vue
Raw Normal View History

2022-09-15 19:15:07 +02:00
<template>
2022-09-20 15:53:44 +02:00
<div v-if="!userLoaded" class="bg-neutral-100 dark:bg-neutral-900 text-primary h-screen flex flex-col">
Loading
</div>
<div v-else>
<div v-if="isAuthenticated" class="bg-neutral-100 dark:bg-neutral-900 text-primary h-screen flex flex-col">
<LayoutTopbar />
<div class="overflow-y-auto pt-3">
<NuxtPage />
</div>
<LayoutNavbar class="mt-auto" />
</div>
<div v-else class="bg-neutral-100 dark:bg-neutral-900 text-primary h-screen flex flex-col">
<Login />
2022-09-19 15:11:13 +02:00
</div>
2022-09-15 19:15:07 +02:00
</div>
2022-09-19 09:36:47 +02:00
</template>
<script setup>
2022-09-22 16:39:11 +02:00
import { doc, getFirestore, serverTimestamp, writeBatch, collection, getDoc } from "firebase/firestore";
2022-09-20 15:53:44 +02:00
import { getAuth, onAuthStateChanged, signInWithEmailAndPassword } from "firebase/auth";
2022-09-29 16:34:00 +02:00
import { PushNotifications } from '@capacitor/push-notifications';
2022-09-30 14:24:36 +02:00
import { Device } from '@capacitor/device';
2022-09-19 09:36:47 +02:00
const db = getFirestore()
2022-09-20 15:53:44 +02:00
const route = useRoute()
const isAuthenticated = ref(false)
const user = ref('frikandel')
const auth = ref(null)
const userLoaded = ref(false)
2022-09-22 16:39:11 +02:00
const userData = ref(null)
const userPersons = ref([])
2022-09-26 16:23:45 +02:00
const userAllPersons = ref([])
2022-09-27 16:00:09 +02:00
const calEvents = ref([])
2022-09-27 17:33:00 +02:00
const news = ref(null)
2022-09-20 15:53:44 +02:00
2022-09-30 14:24:36 +02:00
if (process.client) {
window.addEventListener('load', () => {
if (!('serviceWorker' in navigator)) {
throw new Error('serviceWorker is not supported in current browser!')
}
navigator.serviceWorker.register('/sw.js')
})
}
2022-09-20 15:53:44 +02:00
onMounted(() => {
auth.value = getAuth()
2022-09-22 16:39:11 +02:00
onAuthStateChanged(auth.value, async (usr) => {
2022-09-20 15:53:44 +02:00
if (usr) {
user.value = usr
2022-09-22 16:39:11 +02:00
let docRef = doc(db, "users", user.value.uid);
let docSnap = await getDoc(docRef);
if (docSnap.exists()) {
const data = docSnap.data()
userData.value = data
getPersons(userData.value.relatiecodes)
}
2022-09-27 17:33:00 +02:00
if (!userData.value.sendNews && route.path === '/news/newmessage') navigateTo('/')
if (!userData.value.admin && route.path.startsWith('/settings/admin')) navigateTo('/')
2022-09-22 16:39:11 +02:00
isAuthenticated.value = true
2022-09-30 14:24:36 +02:00
logDeviceInfo()
2022-09-29 16:34:00 +02:00
2022-09-20 15:53:44 +02:00
} else {
isAuthenticated.value = false
user.value = null
2022-09-22 16:39:11 +02:00
userData.value = null
userPersons.value = []
2022-09-20 15:53:44 +02:00
}
userLoaded.value = true
})
})
2022-09-19 09:36:47 +02:00
2022-09-22 16:39:11 +02:00
const getPersons = async (persons) => {
userPersons.value = [];
for (let i = 0; i < persons.length; i++) {
const docRef = doc(db, "ledenlijst", persons[i]);
const docSnap = await getDoc(docRef);
2022-09-19 09:36:47 +02:00
2022-09-22 16:39:11 +02:00
if (docSnap.exists()) {
userPersons.value.push(docSnap.data())
}
}
}
2022-09-29 16:34:00 +02:00
const setupNotifications = () => {
console.log('Initializing HomePage');
// Request permission to use push notifications
// iOS will prompt user and return if they granted permission or not
// Android will just grant without prompting
PushNotifications.requestPermissions().then(result => {
if (result.receive === 'granted') {
// Register with Apple / Google to receive push via APNS/FCM
PushNotifications.register();
} else {
// Show some error
}
});
// On success, we should be able to receive notifications
PushNotifications.addListener('registration',
(token) => {
alert('Push registration success, token: ' + token.value);
}
);
// Some issue with our setup and push will not work
PushNotifications.addListener('registrationError',
(error) => {
alert('Error on registration: ' + JSON.stringify(error));
}
);
// Show us the notification payload if the app is open on our device
PushNotifications.addListener('pushNotificationReceived',
(notification) => {
alert('Push received: ' + JSON.stringify(notification));
}
);
// Method called when tapping on a notification
PushNotifications.addListener('pushNotificationActionPerformed',
(notification) => {
alert('Push action performed: ' + JSON.stringify(notification));
}
);
}
2022-09-30 14:24:36 +02:00
const logDeviceInfo = async () => {
const info = await Device.getInfo();
console.log(info);
if (info.platform !== 'web') setupNotifications()
};
2022-09-22 16:39:11 +02:00
const ledenlijst = ref([])
2022-09-19 15:11:13 +02:00
2022-09-27 17:33:00 +02:00
provide('firebase', { db, ledenlijst, isAuthenticated, user, userData, userPersons, auth, userAllPersons, getPersons, calEvents, news })
2022-09-19 09:36:47 +02:00
</script>