Move modal to plugin

This commit is contained in:
Ramon Wenger 2020-03-25 12:58:53 +01:00
parent 18d52f8d2e
commit 32e3c6067b
5 changed files with 94 additions and 14 deletions

View File

@ -1,5 +1,6 @@
<template>
<div :class="{'no-scroll': showModal || showMobileNavigation}" class="app" id="app">
<component :is="showModalDeprecated" v-if="showModalDeprecated"></component>
<component :is="showModal" v-if="showModal"></component>
<component :is="layout"></component>
<mobile-navigation v-if="showMobileNavigation"></mobile-navigation>
@ -62,7 +63,13 @@
layout() {
return (this.$route.meta.layout || 'default') + '-layout';
},
...mapGetters(['showModal', 'showMobileNavigation'])
...mapGetters({
showModalDeprecated: 'showModal', // don't use this any more todo: remove this
showMobileNavigation: 'showMobileNavigation'
}),
showModal() {
return this.$modal.state.component;
}
},
mounted() {

View File

@ -58,32 +58,29 @@
<script>
import Modal from '@/components/Modal';
import {mapGetters} from 'vuex';
export default {
components: {
Modal
},
computed: {
...mapGetters(['modulePayload']),
myself() {
return this.modulePayload[0] || false
return this.$modal.state.payload.myself;
},
name() {
return this.modulePayload[1] || ''
return this.$modal.state.payload.name;
},
schoolClass() {
return this.modulePayload[2] || ''
return this.$modal.state.payload.className;
}
},
methods: {
confirm() {
this.$store.dispatch('confirmModal');
this.$modal.confirm();
},
cancel() {
this.$store.dispatch('cancelModal');
this.$modal.cancel();
}
}
}

View File

@ -17,9 +17,12 @@ import {dateFilter} from './filters/date-filter';
import autoGrow from '@/directives/auto-grow'
import clickOutside from '@/directives/click-outside'
import ME_QUERY from '@/graphql/gql/meQuery.gql';
import VueModal from '@/plugins/modal';
Vue.config.productionTip = false;
Vue.use(VueModal);
// TODO: Move into a separate project as a plugin
//
function getRidOfEdges(collection) {
@ -141,6 +144,7 @@ router.beforeEach(async (to, from, next) => {
// handle logout
if (to.path === '/logout') {
privateApolloClient.resetStore();
publicApolloClient.resetStore();
next({name: 'login'});
return
}

View File

@ -61,11 +61,11 @@
this.changeMember(member, true);
},
remove(member) {
this.$store.dispatch('deactivateUser', [
member.id === this.me.id, // myself
`${member.firstName} ${member.lastName}`, // full name
this.me.selectedClass.name // class name
])
this.$modal.open('deactivate-person', {
myself: member.id === this.me.id,
name: `${member.firstName} ${member.lastName}`,
className: this.me.selectedClass.name,
})
.then(() => {
this.changeMember(member, false);
})

View File

@ -0,0 +1,72 @@
// adapted from
// https://stackoverflow.com/questions/41791193/vuejs-reactive-binding-for-a-plugin-how-to/41801107#41801107
import Vue from 'vue';
class ModalStore {
constructor(data = {}) {
this.vm = new Vue({
data: () => ({
component: '',
payload: {},
_resolve: () => {
},
_reject: () => {
}
})
});
}
get state() {
return this.vm.$data;
}
// set component(c) {
// console.log(c);
// this.vm.$data.component = c;
// }
}
const ModalPlugin = {
// Store: ModalStore,
install(Vue, options) {
const store = new ModalStore({});
const reset = () => {
store.state.component = '';
store.state.payload = {};
store.state._resolve = () => {
};
store.state._reject = () => {
};
};
Vue.prototype.$modal = {
state: store.state,
component: store.state.component,
payload: store.state.payload,
open: (component, payload) => {
store.state.payload = payload;
store.state.component = component;
return new Promise((resolve, reject) => {
this._resolve = resolve;
this._reject = reject;
});
},
confirm: () => {
reset();
this._resolve();
},
cancel: () => {
reset();
this._reject();
},
_resolve: () => {
},
_reject: () => {
},
};
}
};
export default ModalPlugin;