Newer
Older
import axios from 'axios'
import logger from '@/logging'
import _ from '@/lodash'
Eliot Berriot
committed
function getDefaultUrl () {
return (
window.location.protocol + '//' + window.location.hostname +
(window.location.port ? ':' + window.location.port : '')
)
}
export default {
namespaced: true,
state: {
frontSettings: {},
Eliot Berriot
committed
instanceUrl: process.env.VUE_APP_INSTANCE_URL,
instance: {
name: {
value: ''
},
short_description: {
value: ''
},
long_description: {
value: ''
}
},
},
upload_quota: {
value: 0
Eliot Berriot
committed
subsonic: {
enabled: {
value: true
}
}
}
},
mutations: {
settings: (state, value) => {
_.merge(state.settings, value)
},
event: (state, value) => {
state.events.unshift(value)
if (state.events.length > state.maxEvents) {
state.events = state.events.slice(0, state.maxEvents)
}
},
events: (state, value) => {
state.events = value
frontSettings: (state, value) => {
state.frontSettings = value
},
instanceUrl: (state, value) => {
if (value && !value.endsWith('/')) {
value = value + '/'
}
state.instanceUrl = value
if (!value) {
axios.defaults.baseURL = null
return
}
let suffix = 'api/v1/'
axios.defaults.baseURL = state.instanceUrl + suffix
}
},
getters: {
Eliot Berriot
committed
defaultUrl: (state) => () => {
return getDefaultUrl()
},
absoluteUrl: (state) => (relativeUrl) => {
if (relativeUrl.startsWith('http')) {
return relativeUrl
}
if (state.instanceUrl.endsWith('/') && relativeUrl.startsWith('/')) {
relativeUrl = relativeUrl.slice(1)
}
Eliot Berriot
committed
let instanceUrl = state.instanceUrl || getDefaultUrl()
return instanceUrl + relativeUrl
setUrl ({commit, dispatch}, url) {
commit('instanceUrl', url)
let modules = [
'auth',
'favorites',
'player',
'playlists',
'queue',
'radios'
]
modules.forEach(m => {
commit(`${m}/reset`, null, {root: true})
})
},
// Send a request to the login URL and save the returned JWT
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)
})
},
fetchFrontSettings ({commit}) {
return axios.get('/front/settings.json').then(response => {
commit('frontSettings', response.data)
}, response => {
logger.default.error('Error when fetching front-end configuration (or no customization available)')
})