From 09712320501a3e64541c429ca460c30eefced099 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Wed, 9 Jan 2019 23:11:19 +0100 Subject: Provide initial state for backupcodes in template This saves a direct request to the server when loading the backup codes. There is no need for this as the data is already known. Signed-off-by: Roeland Jago Douma --- .../src/service/BackupCodesService.js | 6 -- apps/twofactor_backupcodes/src/settings.js | 14 +++-- apps/twofactor_backupcodes/src/store.js | 69 ++++++++++++++++++++++ .../src/views/PersonalSettings.vue | 33 ++++------- 4 files changed, 92 insertions(+), 30 deletions(-) create mode 100644 apps/twofactor_backupcodes/src/store.js (limited to 'apps/twofactor_backupcodes/src') diff --git a/apps/twofactor_backupcodes/src/service/BackupCodesService.js b/apps/twofactor_backupcodes/src/service/BackupCodesService.js index 62e69ead68f..a42b7b8b5ad 100644 --- a/apps/twofactor_backupcodes/src/service/BackupCodesService.js +++ b/apps/twofactor_backupcodes/src/service/BackupCodesService.js @@ -1,11 +1,5 @@ import Axios from 'nextcloud-axios' -export function getState () { - const url = OC.generateUrl('/apps/twofactor_backupcodes/settings/state'); - - return Axios.get(url).then(resp => resp.data); -} - export function generateCodes () { const url = OC.generateUrl('/apps/twofactor_backupcodes/settings/create'); diff --git a/apps/twofactor_backupcodes/src/settings.js b/apps/twofactor_backupcodes/src/settings.js index 9534d802824..00b28bfde63 100644 --- a/apps/twofactor_backupcodes/src/settings.js +++ b/apps/twofactor_backupcodes/src/settings.js @@ -1,9 +1,15 @@ import Vue from 'vue'; import PersonalSettings from './views/PersonalSettings'; +import store from './store'; Vue.prototype.t = t; -export default new Vue({ - el: '#twofactor-backupcodes-settings', - render: h => h(PersonalSettings) -}); +const initialStateElem = JSON.parse(atob(document.getElementById('twofactor-backupcodes-initial-state').value)); +store.replaceState( + initialStateElem +) + +const View = Vue.extend(PersonalSettings) +new View({ + store +}).$mount('#twofactor-backupcodes-settings') diff --git a/apps/twofactor_backupcodes/src/store.js b/apps/twofactor_backupcodes/src/store.js new file mode 100644 index 00000000000..3af299ca619 --- /dev/null +++ b/apps/twofactor_backupcodes/src/store.js @@ -0,0 +1,69 @@ +/* + * @copyright 2019 Roeland Jago Douma + * + * @author 2019 Roeland Jago Douma + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import Vue from 'vue' +import Vuex from 'vuex' + +import {generateCodes} from './service/BackupCodesService' + +Vue.use(Vuex) + +export const mutations = { + setEnabled(state, enabled) { + Vue.set(state, 'enabled', enabled) + }, + setTotal(state, total) { + Vue.set(state, 'total', total) + }, + setUsed(state, used) { + Vue.set(state, 'used', used) + }, + setCodes(state, codes) { + Vue.set(state, 'codes', codes) + } +} + +export const actions = { + generate ({commit}) { + commit('setEnabled', false); + + return generateCodes() + .then(({codes, state}) => { + commit('setEnabled', state.enabled); + commit('setTotal', state.total); + commit('setUsed', state.used); + commit('setCodes', codes); + return true; + }); + } +} + +export default new Vuex.Store({ + strict: process.env.NODE_ENV !== 'production', + state: { + enabled: false, + total: 0, + used: 0, + codes: undefined + }, + mutations, + actions +}) diff --git a/apps/twofactor_backupcodes/src/views/PersonalSettings.vue b/apps/twofactor_backupcodes/src/views/PersonalSettings.vue index 0a9ac4b1d48..2ca389461c8 100644 --- a/apps/twofactor_backupcodes/src/views/PersonalSettings.vue +++ b/apps/twofactor_backupcodes/src/views/PersonalSettings.vue @@ -34,17 +34,13 @@