Moved snackbars to their own store and component
This commit is contained in:
41
app/imports/ui/components/snackbars/Snackbars.vue
Normal file
41
app/imports/ui/components/snackbars/Snackbars.vue
Normal file
@@ -0,0 +1,41 @@
|
||||
<template lang="html">
|
||||
<v-snackbar
|
||||
v-if="snackbar"
|
||||
:key="snackbar.text"
|
||||
auto-height
|
||||
bottom
|
||||
:value="true"
|
||||
:timeout="0"
|
||||
>
|
||||
{{ snackbar.text }}
|
||||
<v-btn
|
||||
v-if="snackbar.callback"
|
||||
flat
|
||||
icon
|
||||
@click="snackbar.callback"
|
||||
>
|
||||
<v-icon>{{ snackbar.callbackName }}</v-icon>
|
||||
</v-btn>
|
||||
<v-btn
|
||||
v-if="snackbar.showCloseButton"
|
||||
flat
|
||||
icon
|
||||
@click="$store.dispatch('closeSnackbar')"
|
||||
>
|
||||
<v-icon>close</v-icon>
|
||||
</v-btn>
|
||||
</v-snackbar>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
computed: {
|
||||
snackbar(){
|
||||
return this.$store.state.snackbars.snackbars[0];
|
||||
}
|
||||
},
|
||||
}
|
||||
</script>
|
||||
|
||||
<style lang="css" scoped>
|
||||
</style>
|
||||
47
app/imports/ui/components/snackbars/snackboxStore.js
Normal file
47
app/imports/ui/components/snackbars/snackboxStore.js
Normal file
@@ -0,0 +1,47 @@
|
||||
const snackbarStore = {
|
||||
state: {
|
||||
snackbars: [],
|
||||
snackbarTimout: undefined,
|
||||
},
|
||||
mutations: {
|
||||
addSnackbar(state, value){
|
||||
state.snackbars.push(value)
|
||||
},
|
||||
closeCurrentSnackbar (state){
|
||||
state.snackbars.shift();
|
||||
},
|
||||
cancelSnackbarTimeout (state){
|
||||
if(state.snackbarTimout){
|
||||
clearTimeout(state.snackbarTimout);
|
||||
}
|
||||
},
|
||||
setSnackbarTimout(state, value){
|
||||
state.snackbarTimout = value;
|
||||
},
|
||||
},
|
||||
actions: {
|
||||
snackbar({dispatch, commit}, value){
|
||||
// value = {
|
||||
// text,
|
||||
// showCloseButton,
|
||||
// callback,
|
||||
// callbackName
|
||||
// }
|
||||
commit('addSnackbar', value);
|
||||
commit('setSnackbarTimout', setTimeout(() => {
|
||||
dispatch('closeSnackbar');
|
||||
}, 5000));
|
||||
},
|
||||
closeSnackbar({dispatch, commit, state}){
|
||||
commit('closeCurrentSnackbar');
|
||||
commit('cancelSnackbarTimeout');
|
||||
if (state.snackbars.length){
|
||||
commit('setSnackbarTimout', setTimeout(() => {
|
||||
dispatch('closeSnackbar');
|
||||
}, 5000));
|
||||
}
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
export default snackbarStore;
|
||||
@@ -93,9 +93,6 @@
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
data(){return {
|
||||
snackbars: [],
|
||||
}},
|
||||
reactiveProvide: {
|
||||
name: 'context',
|
||||
include: ['creature', 'editPermission'],
|
||||
@@ -124,10 +121,10 @@
|
||||
added(doc){
|
||||
if (!that.$subReady.singleCharacter) return;
|
||||
if (that.$store.state.rightDrawer) return;
|
||||
if (that.snackbars.some(o => o._id === doc._id)) return;
|
||||
doc.open = true;
|
||||
that.$store.commit('snackbar', {
|
||||
doc
|
||||
if (that.$store.state.snackbars.snackbars.some(o => o._id === doc._id)) return;
|
||||
that.$store.dispatch('snackbar', {
|
||||
text: doc.text,
|
||||
showCloseButton: true,
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
@@ -67,31 +67,7 @@
|
||||
name="rightDrawer"
|
||||
/>
|
||||
<dialog-stack />
|
||||
<v-snackbar
|
||||
v-for="snackbar in snackbars"
|
||||
:key="snackbar._id"
|
||||
:value="snackbar.open"
|
||||
auto-height
|
||||
bottom
|
||||
>
|
||||
{{ snackbar.text.split(/\n+/).pop() }}
|
||||
<v-btn
|
||||
v-if="snackbar.callback"
|
||||
flat
|
||||
icon
|
||||
@click="snackbar.callback"
|
||||
>
|
||||
<v-icon>{{ snackbar.callbackName }}</v-icon>
|
||||
</v-btn>
|
||||
<v-btn
|
||||
v-if="snackbar.showCloseButton"
|
||||
flat
|
||||
icon
|
||||
@click="snackbar.open = false"
|
||||
>
|
||||
<v-icon>close</v-icon>
|
||||
</v-btn>
|
||||
</v-snackbar>
|
||||
<snackbars />
|
||||
</v-app>
|
||||
</template>
|
||||
|
||||
@@ -101,11 +77,13 @@
|
||||
import DialogStack from '/imports/ui/dialogStack/DialogStack.vue';
|
||||
import { theme, darkTheme } from '/imports/ui/theme.js';
|
||||
import { mapMutations } from 'vuex';
|
||||
import Snackbars from '/imports/ui/components/snackbars/Snackbars.vue';
|
||||
|
||||
export default {
|
||||
components: {
|
||||
Sidebar,
|
||||
DialogStack,
|
||||
Snackbars,
|
||||
},
|
||||
data(){return {
|
||||
name: 'Home',
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
import Vue from 'vue';
|
||||
import Vuex from 'vuex';
|
||||
import dialogStackStore from '/imports/ui/dialogStack/dialogStackStore.js';
|
||||
import snackbarStore from '/imports/ui/components/snackbars/snackboxStore.js';
|
||||
|
||||
Vue.use(Vuex);
|
||||
const store = new Vuex.Store({
|
||||
strict: process.env.NODE_ENV !== 'production',
|
||||
modules: {
|
||||
dialogStack: dialogStackStore,
|
||||
snackbars: snackbarStore,
|
||||
},
|
||||
state: {
|
||||
drawer: undefined,
|
||||
rightDrawer: undefined,
|
||||
pageTitle: undefined,
|
||||
snackbars: [],
|
||||
snackbarTimout: undefined,
|
||||
},
|
||||
mutations: {
|
||||
toggleDrawer (state) {
|
||||
@@ -32,43 +32,7 @@ const store = new Vuex.Store({
|
||||
state.pageTitle = value;
|
||||
document.title = value;
|
||||
},
|
||||
addSnackbar(state, value){
|
||||
value.open = true;
|
||||
state.snackbars.push(value)
|
||||
},
|
||||
closeCurrentSnackbar (state){
|
||||
state.snackbars.shift();
|
||||
},
|
||||
cancelSnackbarTimeout (state){
|
||||
if(state.snackbarTimout){
|
||||
clearTimeout(state.snackbarTimout);
|
||||
}
|
||||
},
|
||||
setSnackbarTimout(state, value){
|
||||
state.snackbarTimout = value;
|
||||
},
|
||||
},
|
||||
actions: {
|
||||
snackbar({commit}, value){
|
||||
// value = {
|
||||
// text,
|
||||
// showCloseButton,
|
||||
// callback,
|
||||
// callbackName
|
||||
// }
|
||||
commit('addSnackbar', value);
|
||||
},
|
||||
closeSnackbar({dispatch, commit, state}){
|
||||
commit('closeCurrentSnackbar');
|
||||
commit('cancelSnackbarTimeout');
|
||||
if (state.snackbars.length){
|
||||
commit('setSnackbarTimout');
|
||||
state.snackbarTimout = setTimeout(() => {
|
||||
dispatch('closeSnackbar');
|
||||
}, 5000);
|
||||
}
|
||||
},
|
||||
}
|
||||
});
|
||||
|
||||
export default store;
|
||||
|
||||
Reference in New Issue
Block a user