From aa67be9063061e682dea2e4b728d14a19166b74b Mon Sep 17 00:00:00 2001 From: Eliot Berriot Date: Sat, 17 Feb 2018 21:22:52 +0100 Subject: [PATCH] Now load instance settings on page load --- front/src/App.vue | 3 + front/src/store/index.js | 2 + front/src/store/instance.js | 42 ++++++++++++ front/test/unit/specs/store/instance.spec.js | 70 ++++++++++++++++++++ 4 files changed, 117 insertions(+) create mode 100644 front/src/store/instance.js create mode 100644 front/test/unit/specs/store/instance.spec.js diff --git a/front/src/App.vue b/front/src/App.vue index afaea8215..7e7984d55 100644 --- a/front/src/App.vue +++ b/front/src/App.vue @@ -31,6 +31,9 @@ import Sidebar from '@/components/Sidebar' export default { name: 'app', components: { Sidebar } + created () { + this.$store.dispatch('instance/fetchSettings') + } } diff --git a/front/src/store/index.js b/front/src/store/index.js index a5df7c240..74f9d42b1 100644 --- a/front/src/store/index.js +++ b/front/src/store/index.js @@ -4,6 +4,7 @@ import createPersistedState from 'vuex-persistedstate' import favorites from './favorites' import auth from './auth' +import instance from './instance' import queue from './queue' import radios from './radios' import player from './player' @@ -14,6 +15,7 @@ export default new Vuex.Store({ modules: { auth, favorites, + instance, queue, radios, player diff --git a/front/src/store/instance.js b/front/src/store/instance.js new file mode 100644 index 000000000..a0071f096 --- /dev/null +++ b/front/src/store/instance.js @@ -0,0 +1,42 @@ +import axios from 'axios' +import logger from '@/logging' +import _ from 'lodash' + +export default { + namespaced: true, + state: { + settings: { + raven: { + front_enabled: { + value: false + }, + front_dsn: { + value: null + } + } + } + }, + mutations: { + settings: (state, value) => { + _.merge(state.settings, value) + } + }, + actions: { + // Send a request to the login URL and save the returned JWT + fetchSettings ({commit}) { + return axios.get('instance/settings/').then(response => { + logger.default.info('Successfully fetched instance settings') + let sections = {} + response.data.forEach(e => { + sections[e.section] = {} + }) + response.data.forEach(e => { + sections[e.section][e.name] = e + }) + commit('settings', sections) + }, response => { + logger.default.error('Error while fetching settings', response.data) + }) + } + } +} diff --git a/front/test/unit/specs/store/instance.spec.js b/front/test/unit/specs/store/instance.spec.js new file mode 100644 index 000000000..4b06cb5f0 --- /dev/null +++ b/front/test/unit/specs/store/instance.spec.js @@ -0,0 +1,70 @@ +var sinon = require('sinon') +import moxios from 'moxios' +import store from '@/store/instance' +import { testAction } from '../../utils' + +describe('store/instance', () => { + var sandbox + + beforeEach(function () { + sandbox = sinon.sandbox.create() + moxios.install() + }) + afterEach(function () { + sandbox.restore() + moxios.uninstall() + }) + + describe('mutations', () => { + it('settings', () => { + const state = {settings: {raven: {front_dsn: {value: 'test'}}}} + let settings = {raven: {front_enabled: {value: true}}} + store.mutations.settings(state, settings) + expect(state.settings).to.deep.equal({ + raven: {front_dsn: {value: 'test'}, front_enabled: {value: true}} + }) + }) + }) + describe('actions', () => { + it('fetchSettings', (done) => { + moxios.stubRequest('instance/settings/', { + status: 200, + response: [ + { + section: 'raven', + name: 'front_dsn', + value: 'test' + }, + { + section: 'raven', + name: 'front_enabled', + value: false + } + ] + }) + testAction({ + action: store.actions.fetchSettings, + payload: null, + expectedMutations: [ + { + type: 'settings', + payload: { + raven: { + front_dsn: { + section: 'raven', + name: 'front_dsn', + value: 'test' + }, + front_enabled: { + section: 'raven', + name: 'front_enabled', + value: false + } + } + } + } + ] + }, done) + }) + }) +})