Fix flow of setting instance url

This commit is contained in:
wvffle 2022-06-12 10:23:25 +00:00 committed by Georg Krause
parent 344f1af058
commit c4dd04e0b8
3 changed files with 41 additions and 37 deletions

View File

@ -211,6 +211,7 @@ export default {
}, },
created () { created () {
const self = this const self = this
// TODO (wvffle): Await action result and remove callback from the instance store
this.$store.dispatch('instance/fetchSettings', { this.$store.dispatch('instance/fetchSettings', {
callback: function () { callback: function () {
self.isLoadingInstanceSetting = false self.isLoadingInstanceSetting = false

View File

@ -25,14 +25,7 @@ export const install: InitModule = async ({ store, router }) => {
} }
if (!store.state.instance.instanceUrl) { if (!store.state.instance.instanceUrl) {
// We have several way to guess the API server url. By order of precedence: const defaultInstanceUrl = store.state.instance.frontSettings.defaultServerUrl
// 1. use the url provided in settings.json, if any
// 2. use the url specified when building via VUE_APP_INSTANCE_URL
// 3. use the current url
const defaultInstanceUrl = store.state.instance.frontSettings.defaultServerUrl ||
import.meta.env.VUE_APP_INSTANCE_URL ||
location.origin
store.commit('instance/instanceUrl', defaultInstanceUrl) store.commit('instance/instanceUrl', defaultInstanceUrl)
} else { } else {
// needed to trigger initialization of axios / service worker / web socket // needed to trigger initialization of axios / service worker / web socket

View File

@ -5,16 +5,18 @@ import { Module } from 'vuex'
import { RootState } from '~/store/index' import { RootState } from '~/store/index'
export interface State { export interface State {
frontSettings: { frontSettings: FrontendSettings
defaultServerUrl: string
additionalStylesheets: string[] // TODO (wvffle): Ensure it's not nullable
}
instanceUrl?: string instanceUrl?: string
knownInstances: string[] knownInstances: string[]
nodeinfo: unknown | null // TODO (wvffle): Get nodeinfo type from swagger automatically nodeinfo: unknown | null // TODO (wvffle): Get nodeinfo type from swagger automatically
settings: Settings settings: Settings
} }
interface FrontendSettings {
defaultServerUrl: string
additionalStylesheets: string[] // TODO (wvffle): Ensure it's not nullable
}
interface InstanceSettings { interface InstanceSettings {
name: { value: string } name: { value: string }
short_description: { value: string } short_description: { value: string }
@ -46,14 +48,20 @@ interface Settings {
const logger = useLogger() const logger = useLogger()
// We have several way to guess the API server url. By order of precedence:
// 1. use the url provided in settings.json, if any
// 2. use the url specified when building via VUE_APP_INSTANCE_URL
// 3. use the current url
const instanceUrl = import.meta.env.VUE_APP_INSTANCE_URL as string || location.origin
const store: Module<State, RootState> = { const store: Module<State, RootState> = {
namespaced: true, namespaced: true,
state: { state: {
frontSettings: { frontSettings: {
defaultServerUrl: location.origin, defaultServerUrl: instanceUrl,
additionalStylesheets: [] additionalStylesheets: []
}, },
instanceUrl: import.meta.env.VUE_APP_INSTANCE_URL as string, instanceUrl,
knownInstances: [], knownInstances: [],
nodeinfo: null, nodeinfo: null,
settings: { settings: {
@ -102,9 +110,6 @@ const store: Module<State, RootState> = {
nodeinfo: (state, value) => { nodeinfo: (state, value) => {
state.nodeinfo = value state.nodeinfo = value
}, },
frontSettings: (state, value) => {
state.frontSettings = value
},
instanceUrl: (state, value) => { instanceUrl: (state, value) => {
if (value && !value.endsWith('/')) { if (value && !value.endsWith('/')) {
value = value + '/' value = value + '/'
@ -158,8 +163,12 @@ const store: Module<State, RootState> = {
}) })
}, },
// Send a request to the login URL and save the returned JWT // Send a request to the login URL and save the returned JWT
fetchSettings ({ commit }, payload) { async fetchSettings ({ commit }, payload) {
return axios.get('instance/settings/').then(response => { const response = await axios.get('instance/settings/')
.catch(err => logger.error('Error while fetching settings', err.response.data))
if (!response) return
logger.info('Successfully fetched instance settings') logger.info('Successfully fetched instance settings')
type SettingsSection = { section: string, name: string } type SettingsSection = { section: string, name: string }
@ -171,16 +180,17 @@ const store: Module<State, RootState> = {
commit('settings', sections) commit('settings', sections)
payload?.callback?.() payload?.callback?.()
}, response => {
logger.error('Error while fetching settings', response.data)
})
}, },
fetchFrontSettings ({ commit }) { async fetchFrontSettings ({ state }) {
return axios.get('/settings.json').then(response => { const response = await axios.get('/front/settings.json')
commit('frontSettings', response.data) .catch(() => logger.error('Error when fetching front-end configuration (or no customization available)'))
}, () => {
logger.error('Error when fetching front-end configuration (or no customization available)') if (!response) return
})
for (const [key, value] of Object.entries(response.data as FrontendSettings)) {
if (key === 'defaultServerUrl' && !value) continue
state.frontSettings[key as keyof FrontendSettings] = value
}
} }
} }
} }