diff --git a/frontend/package.json b/frontend/package.json index 11654fc1c..d0ef29d92 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -18,7 +18,8 @@ "js-cookie": "^2.2.0", "vue": "^2.5.2", "vue-resource": "^1.5.0", - "vue-router": "^3.0.1" + "vue-router": "^3.0.1", + "vuex": "^3.0.1" }, "devDependencies": { "autoprefixer": "^7.1.2", diff --git a/frontend/src/App.vue b/frontend/src/App.vue index ae3e7c736..6ee9007f4 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -1,29 +1,20 @@ diff --git a/frontend/src/main.js b/frontend/src/main.js index 897b63ade..46522a3c6 100644 --- a/frontend/src/main.js +++ b/frontend/src/main.js @@ -7,6 +7,7 @@ import Cookies from 'js-cookie' import App from './App' import router from './router' +import store from './store' Vue.use(Buefy) @@ -20,6 +21,7 @@ Vue.http.headers.common['X-CSRFTOKEN'] = csrftoken new Vue({ el: '#app', router, + store, components: { App }, template: '' }) diff --git a/frontend/src/router/index.js b/frontend/src/router/index.js index fa5e3bfd4..37aa263d4 100644 --- a/frontend/src/router/index.js +++ b/frontend/src/router/index.js @@ -16,6 +16,12 @@ export default new Router({ path: '/domains/add/', name: 'DomainAdd', component: DomainForm + }, + { + path: '/domains/:domainPk([0-9]+)/', + name: 'DomainEdit', + component: DomainForm } + ] }) diff --git a/frontend/src/store/index.js b/frontend/src/store/index.js new file mode 100644 index 000000000..9e0595c7a --- /dev/null +++ b/frontend/src/store/index.js @@ -0,0 +1,16 @@ +import Vue from 'vue' +import Vuex from 'vuex' + +import domains from './modules/domains' + +Vue.use(Vuex) + +const options = { + modules: { + domains + }, + strict: process.env.NODE_ENV !== 'production' +} + +export default new Vuex.Store(options) +export { options } diff --git a/frontend/src/store/modules/domains.js b/frontend/src/store/modules/domains.js new file mode 100644 index 000000000..b92d29322 --- /dev/null +++ b/frontend/src/store/modules/domains.js @@ -0,0 +1,70 @@ +import Vue from 'vue' +import * as api from '@/api' + +// mutation types +const SET_DOMAINS = 'SET_DOMAINS' +const ADD_DOMAIN = 'ADD_DOMAIN' +const UPDATE_DOMAIN = 'UPDATE_DOMAIN' + +// initial state +const state = { + domainsLoaded: false, + domains: [] +} + +// getters +const getters = { + domains: state => state.domains, + getDomainByPk: state => pk => { + for (var domain of state.domains) { + if (domain.pk === parseInt(pk, 10)) { + return domain + } + } + return null + } +} + +// actions +const actions = { + getDomains ({ commit }) { + return api.getDomains().then(response => { + commit(SET_DOMAINS, { domains: response.data }) + }) + }, + createDomain ({ commit }, data) { + return api.createDomain(data).then(response => { + commit(ADD_DOMAIN, { domain: response.data }) + }) + }, + updateDomain ({ commit }, data) { + return api.updateDomain(data).then(response => { + commit(UPDATE_DOMAIN, { domain: response.data }) + }) + } +} + +// mutations +const mutations = { + [SET_DOMAINS] (state, { domains }) { + state.domainsLoaded = true + state.domains = domains + }, + [ADD_DOMAIN] (state, { domain }) { + state.domains.push(domain) + }, + [UPDATE_DOMAIN] (state, { domain }) { + state.domains.filter(function (item, pos) { + if (item.pk === domain.pk) { + Vue.set(state.domains, pos, domain) + } + }) + } +} + +export default { + state, + getters, + actions, + mutations +} diff --git a/modoboa/admin/views/base.py b/modoboa/admin/views/base.py index 418a89e5b..232341463 100644 --- a/modoboa/admin/views/base.py +++ b/modoboa/admin/views/base.py @@ -4,8 +4,10 @@ from __future__ import unicode_literals from django.views import generic +from django.contrib.auth import mixins as auth_mixins -class AdminIndexView(generic.TemplateView): + +class AdminIndexView(auth_mixins.LoginRequiredMixin, generic.TemplateView): """Admin index view.""" template_name = "admin/index.html"