felicity-lims/frontend/vite/src/App.vue

120 lines
2.9 KiB
Vue
Raw Normal View History

2021-01-16 00:54:31 +08:00
<template>
<component :is="layout">
<!-- <div class="text-center">Flash Messages will come here.</div> -->
2021-01-16 00:54:31 +08:00
<router-view />
</component>
</template>
<script lang="ts">
2021-04-09 06:37:58 +08:00
import Swal from 'sweetalert2';
import JSConfetti from 'js-confetti'
2021-04-09 06:37:58 +08:00
import { Notyf } from 'notyf';
2021-11-29 18:51:20 +08:00
import { defineComponent, computed, watch } from 'vue';
2021-01-16 00:54:31 +08:00
import { useRouter } from 'vue-router';
2021-11-29 18:51:20 +08:00
import { useStore } from "vuex";
import { ActionTypes } from './store/actions'
2021-01-16 00:54:31 +08:00
const defaultLayout = 'default';
export default defineComponent({
2021-11-29 18:51:20 +08:00
setup() {
2021-01-16 00:54:31 +08:00
const { currentRoute } = useRouter();
2021-04-09 06:37:58 +08:00
const store = useStore();
const router = useRouter();
2021-04-09 06:37:58 +08:00
const jsConfetti = new JSConfetti();
jsConfetti.addConfetti({
emojis: ['🌈', '⚡️', '💥', '✨', '💫', '🌸'],
})
2021-04-09 06:37:58 +08:00
const notyf = new Notyf({ // https://github.com/caroso1222/notyf
duration: 5000,
position: {
x: 'left',
y: 'bottom',
},
types: [
{
type: 'info',
background: 'blue',
icon: false
},
2021-04-09 06:37:58 +08:00
{
type: 'warning',
background: 'orange',
icon: {
className: 'material-icons',
tagName: 'i',
text: 'warning'
}
},
{
type: 'error',
background: 'indianred',
duration: 2000,
dismissible: false
2021-04-09 06:37:58 +08:00
}
]
});
2021-01-16 00:54:31 +08:00
const layout = computed(() => `${currentRoute.value.meta.layout || defaultLayout}-layout`);
2021-04-09 06:37:58 +08:00
2021-11-23 05:48:31 +08:00
const fireAlert = (options: any) => {
2021-04-09 06:37:58 +08:00
Swal.fire({
title: 'Yay!',
text: options.message,
icon: options.icon,
confirmButtonText: 'Cool'
})
}
2021-11-29 18:51:20 +08:00
watch(() => store.state.toast.alert, (current, prev) => {
if(!current.data) return;
console.log("watch alert: ", current)
fireAlert(current)
});
watch(store.state.toast.notification, (current, prev) => {
if(!current.data) return;
2021-11-29 18:51:20 +08:00
console.log("watch toast: ", current)
let _message = current.data;
let message = _message
console.error(message);
let logout = false;
if(typeof(message) == 'object'){
message = message.message
if(_message.networkError) {
message = _message.networkError.message
// only logout on network error
logout = true;
}
2021-04-09 06:37:58 +08:00
}
if(current.icon === "success"){
notyf.success(current.message);
}else if(current.icon ==="info"){
notyf.open({
type: 'info',
message: current.message
});
}else if(current.icon ==="warning"){
notyf.open({
type: 'warning',
message: current.message
});
}else{
notyf.error(message);
if(logout) store.dispatch(ActionTypes.LOG_OUT).then(_ => router.push({ name: "Login" }));
}
2021-04-09 06:37:58 +08:00
});
2021-01-16 00:54:31 +08:00
return {
layout,
};
},
});
</script>