2021-01-16 00:54:31 +08:00
|
|
|
<template>
|
|
|
|
<component :is="layout">
|
2021-01-22 03:38:17 +08:00
|
|
|
<!-- <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 { Notyf } from 'notyf';
|
|
|
|
import { defineComponent, ref, computed, watch, inject } from 'vue';
|
2021-01-16 00:54:31 +08:00
|
|
|
import { useRouter } from 'vue-router';
|
2021-04-09 06:37:58 +08:00
|
|
|
import { mapGetters, mapActions, useStore } from "vuex";
|
2021-10-20 23:19:56 +08:00
|
|
|
import { ActionTypes } from './store/actions'
|
2021-01-16 00:54:31 +08:00
|
|
|
const defaultLayout = 'default';
|
|
|
|
export default defineComponent({
|
2021-04-09 06:37:58 +08:00
|
|
|
setup(props, context) {
|
2021-01-16 00:54:31 +08:00
|
|
|
const { currentRoute } = useRouter();
|
2021-04-09 06:37:58 +08:00
|
|
|
const store = useStore();
|
2021-10-20 23:19:56 +08:00
|
|
|
const router = useRouter();
|
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: [
|
2021-10-20 23:19:56 +08:00
|
|
|
{
|
|
|
|
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,
|
2021-10-20 23:19:56 +08:00
|
|
|
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-10-20 23:19:56 +08:00
|
|
|
const fireAlert = (options) => {
|
2021-04-09 06:37:58 +08:00
|
|
|
Swal.fire({
|
|
|
|
title: 'Yay!',
|
|
|
|
text: options.message,
|
|
|
|
icon: options.icon,
|
|
|
|
confirmButtonText: 'Cool'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-20 23:19:56 +08:00
|
|
|
watch(store.state.toast.alert, (current, prev) => {
|
|
|
|
fireAlert(current)
|
|
|
|
});
|
|
|
|
|
|
|
|
watch(store.state.toast.notification, (current, prev) => {
|
|
|
|
if(!current.data) return;
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-10-20 23:19:56 +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>
|