wrbapp/frontend/app.vue

242 lines
6.8 KiB
Vue
Raw Normal View History

2022-09-15 19:15:07 +02:00
<template>
2022-11-14 14:21:59 +01:00
<div v-if="!userLoaded" class="bg-neutral-100 dark:bg-neutral-900 text-primary h-screen flex justify-center items-center">
<div>
<Icon size="4em" name="ion:load-c" class="animate-spin" />
<h2 class="mt-2 font-bold">Loading...</h2>
</div>
2022-09-20 15:53:44 +02:00
</div>
2022-12-04 11:57:53 +01:00
<div v-else class="">
2022-09-20 15:53:44 +02:00
<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-30 21:17:12 +02:00
import { getMessaging, getToken, onMessage } from "firebase/messaging";
import { PushNotifications } from '@capacitor/push-notifications';
2022-09-30 14:24:36 +02:00
import { Device } from '@capacitor/device';
2022-11-14 14:41:41 +01:00
import { useToast } from 'vue-toastification'
2022-09-19 09:36:47 +02:00
const db = getFirestore()
2022-09-20 15:53:44 +02:00
const route = useRoute()
2022-11-14 14:41:41 +01:00
const toast = useToast()
2022-09-20 15:53:44 +02:00
const isAuthenticated = ref(false)
const user = ref('frikandel')
2022-11-13 19:48:40 +01:00
const registrationToken = ref('')
2022-09-20 15:53:44 +02:00
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-12-04 10:29:11 +01:00
const contestTimes = ref(null)
const competitors = ref([])
2022-12-17 13:01:49 +01:00
const users = ref([])
2022-09-20 15:53:44 +02:00
2022-10-01 13:40:29 +02:00
const messaging = ref(null)
2022-09-30 14:24:36 +02:00
2022-09-20 15:53:44 +02:00
onMounted(() => {
2023-01-22 15:34:32 +01:00
auth.value = getAuth()
2023-01-22 20:02:53 +01:00
Device.getInfo().then(info => {
if (info.platform === 'ios') document.getElementsByClassName('top-right')[0].classList.add('toastios')
});
2023-01-22 15:34:32 +01:00
if (process.client) {
if ('serviceWorker' in navigator && window.isSecureContext) {
Device.getInfo().then(info => {
if (info.platform === 'web') registerSW()
2023-01-22 20:02:53 +01:00
else document.getElementsByClassName('top-right')[0].classList.add('toastios')
2023-01-22 15:34:32 +01:00
});
2022-09-30 21:17:12 +02:00
}
2023-01-22 15:34:32 +01:00
}
2022-09-30 21:17:12 +02:00
2023-01-22 15:34:32 +01:00
onAuthStateChanged(auth.value, async (usr) => {
if (usr) {
user.value = usr
2022-09-22 16:39:11 +02:00
2023-01-22 15:34:32 +01:00
let docRef = doc(db, "users", user.value.uid);
let docSnap = await getDoc(docRef);
2022-09-22 16:39:11 +02:00
2023-01-22 15:34:32 +01:00
if (docSnap.exists()) {
const data = docSnap.data()
userData.value = data
getPersons(userData.value.relatiecodes)
} else {
setTimeout(() => window.location.reload(true), 1000)
}
2022-09-22 16:39:11 +02:00
2023-01-22 15:34:32 +01:00
if (!userData.value.sendNews && route.path === '/news/newmessage') navigateTo('/')
if (!userData.value.admin && route.path.startsWith('/settings/admin')) navigateTo('/')
2022-09-27 17:33:00 +02:00
2023-01-22 15:34:32 +01:00
isAuthenticated.value = true
2022-09-22 16:39:11 +02:00
2023-01-22 15:34:32 +01:00
logDeviceInfo()
2022-09-29 16:34:00 +02:00
2023-01-22 15:34:32 +01:00
} else {
isAuthenticated.value = false
user.value = null
userData.value = null
userPersons.value = []
}
2022-09-20 15:53:44 +02:00
2023-01-22 15:34:32 +01:00
userLoaded.value = true
})
2022-09-20 15:53:44 +02:00
})
2022-09-19 09:36:47 +02:00
2022-09-22 16:39:11 +02:00
const getPersons = async (persons) => {
2023-01-22 15:34:32 +01:00
userPersons.value = [];
2022-09-22 16:39:11 +02:00
2023-01-22 15:34:32 +01:00
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
2023-01-22 15:34:32 +01:00
if (docSnap.exists()) {
userPersons.value.push(docSnap.data())
2022-09-22 16:39:11 +02:00
}
2023-01-22 15:34:32 +01:00
}
2022-09-22 16:39:11 +02:00
}
2022-09-29 16:34:00 +02:00
const setupNotifications = () => {
2023-01-21 21:36:22 +01:00
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 {
toast.error('Error tijdens het registrenen van push notificaties')
}
});
// On success, we should be able to receive notifications
PushNotifications.addListener('registration',
async (token) => {
// alert('Push registration success, token: ' + token.value);
registrationToken.value = token
const { error } = await useFetch('/api/subscribetotopic', {
method: 'post',
body: { topic: 'all', registrationToken: token.value }
})
if (error.value) {
console.log(error.value)
return toast.error('Error tijdens het krijgen van relateicodes')
2022-09-29 16:34:00 +02:00
}
2023-01-21 21:36:22 +01:00
console.log('Subscribed to topic!')
}
);
2022-11-14 14:41:41 +01:00
2023-01-21 21:36:22 +01:00
// Some issue with our setup and push will not work
PushNotifications.addListener('registrationError',
(error) => {
toast.error('Error tijdens het registreren van push notificaties')
2022-09-29 16:34:00 +02:00
2023-01-21 21:36:22 +01:00
console.log(error)
}
);
2022-11-07 15:29:15 +01:00
2023-01-21 21:36:22 +01:00
// Show us the notification payload if the app is open on our device
PushNotifications.addListener('pushNotificationReceived',
(notification) => {
2022-11-14 14:41:41 +01:00
2023-01-21 21:36:22 +01:00
toast.info(`${notification.title}`, {
onClick: () => navigateTo('/news')
})
2022-09-29 16:34:00 +02:00
2023-01-21 21:36:22 +01:00
}
);
// Method called when tapping on a notification
PushNotifications.addListener('pushNotificationActionPerformed',
(notification) => {
navigateTo('/news')
}
);
2022-09-29 16:34:00 +02:00
}
2022-09-30 21:17:12 +02:00
const registerFCM = () => {
2023-01-21 21:36:22 +01:00
getToken(messaging.value, { vapidKey: 'BI7l3nyGV6wJcFh7wrwmQ42W7RSXl46bmhXZJmDd4P-0K_JFP0ClTqjO-rr5H5DXBbmVR4kXwxFpUlo_d6cUy4Q' }).then(async (currentToken) => {
2022-09-30 21:17:12 +02:00
if (currentToken) {
console.log(currentToken)
2023-01-21 21:36:22 +01:00
2023-01-22 15:34:32 +01:00
const { error} = await useFetch('/api/subscribetotopic', {
2023-01-21 21:36:22 +01:00
method: 'post',
body: { topic: 'all', registrationToken: currentToken }
})
if (error.value) {
console.log(error.value)
return toast.error('Error tijdens het registreren van push notifications')
}
2023-01-22 15:34:32 +01:00
registrationToken.value = currentToken
2023-01-21 21:36:22 +01:00
console.log('Subscribed to topic!')
2022-09-30 21:17:12 +02:00
} else {
// Show permission request UI
console.log('No registration token available. Request permission to generate one.');
// ...
}
}).catch((err) => {
console.log('An error occurred while retrieving token. ', err);
// ...
});
}
2022-10-04 17:06:49 +02:00
const registerSW = () => {
messaging.value = getMessaging()
navigator.serviceWorker.register('/sw.js').catch(e => alert(e));
onMessage(messaging.value, (payload) => {
console.log('Message received. ', payload);
2022-11-14 14:41:41 +01:00
toast.info(`${payload.notification.title}`, {
onClick: () => navigateTo('/news')
2022-11-07 15:29:15 +01:00
})
2022-10-04 17:06:49 +02:00
});
}
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-30 21:17:12 +02:00
else registerFCM()
2022-09-30 14:24:36 +02:00
};
2022-09-22 16:39:11 +02:00
const ledenlijst = ref([])
2022-09-19 15:11:13 +02:00
2023-01-22 15:34:32 +01:00
provide('firebase', { db, ledenlijst, isAuthenticated, user, userData, userPersons, auth, users, userAllPersons, getPersons, calEvents, news, registrationToken, contestTimes, competitors, registrationToken })
2022-11-13 19:48:40 +01:00
</script>
2022-12-04 10:29:11 +01:00
2023-01-22 20:02:53 +01:00
<style>
2022-12-04 10:29:11 +01:00
.body {
2022-12-04 11:57:53 +01:00
padding-top: 10px;
2022-12-04 10:29:11 +01:00
margin-bottom: env(safe-area-inset-bottom);
}
2023-01-22 20:02:53 +01:00
.toastios {
2023-01-22 21:42:05 +01:00
padding-top: 20px;
2023-01-22 20:02:53 +01:00
}
2022-12-04 10:29:11 +01:00
</style>