Client - get application config

This commit is contained in:
Sam 2021-08-15 12:30:39 +02:00
parent 0235a93c5b
commit 0b95b93e06
10 changed files with 103 additions and 6 deletions

View File

@ -7,8 +7,10 @@
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import { defineComponent, onBeforeMount } from 'vue'
import { ROOT_STORE } from '@/store/constants'
import { useStore } from '@/use/useStore'
import Footer from '@/components/Footer.vue'
import NavBar from '@/components/NavBar.vue'
@ -18,6 +20,12 @@
Footer,
NavBar,
},
setup() {
const store = useStore()
onBeforeMount(() =>
store.dispatch(ROOT_STORE.ACTIONS.GET_APPLICATION_CONFIG)
)
},
})
</script>

View File

@ -1,4 +1,8 @@
import { RootGetters, RootMutations } from '@/store/modules/root/enums'
import {
RootActions,
RootGetters,
RootMutations,
} from '@/store/modules/root/enums'
import {
UserActions,
UserGetters,
@ -6,6 +10,7 @@ import {
} from '@/store/modules/user/enums'
export const ROOT_STORE = {
ACTIONS: RootActions,
GETTERS: RootGetters,
MUTATIONS: RootMutations,
}

View File

@ -0,0 +1,27 @@
import { ActionContext, ActionTree } from 'vuex'
import { ROOT_STORE } from '@/store/constants'
import { IRootActions, IRootState } from '@/store/modules/root/interfaces'
import authApi from '@/api/authApi'
import { handleError } from '@/utils'
export const actions: ActionTree<IRootState, IRootState> & IRootActions = {
[ROOT_STORE.ACTIONS.GET_APPLICATION_CONFIG](
context: ActionContext<IRootState, IRootState>
): void {
context.commit(ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGES)
authApi
.get('config')
.then((res) => {
if (res.data.status === 'success') {
context.commit(
ROOT_STORE.MUTATIONS.UPDATE_APPLICATION_CONFIG,
res.data.data
)
} else {
handleError(context, null)
}
})
.catch((error) => handleError(context, error))
},
}

View File

@ -1,3 +1,7 @@
export enum RootActions {
GET_APPLICATION_CONFIG = 'GET_APPLICATION_CONFIG',
}
export enum RootGetters {
ERROR_MESSAGES = 'ERROR_MESSAGES',
LANGUAGE = 'LANGUAGE',
@ -6,5 +10,6 @@ export enum RootGetters {
export enum RootMutations {
EMPTY_ERROR_MESSAGES = 'EMPTY_ERROR_MESSAGES',
SET_ERROR_MESSAGES = 'SET_ERROR_MESSAGES',
UPDATE_APPLICATION_CONFIG = 'UPDATE_APPLICATION_CONFIG',
UPDATE_LANG = 'UPDATE_LANG',
}

View File

@ -1,5 +1,6 @@
import { Module, ModuleTree } from 'vuex'
import { actions } from '@/store/modules/root/actions'
import { getters } from '@/store/modules/root/getters'
import { IRootState } from '@/store/modules/root/interfaces'
import { mutations } from '@/store/modules/root/mutations'
@ -12,6 +13,7 @@ const modules: ModuleTree<IRootState> = {
const root: Module<IRootState, IRootState> = {
state,
actions,
getters,
mutations,
modules,

View File

@ -1,9 +1,39 @@
import { ROOT_STORE } from '@/store/constants'
import { ActionContext } from 'vuex'
export interface IAppStatistics {
sports: number
uploads_dir_size: number
users: number
workouts: number
}
export interface IAppConfig {
federation_enabled: boolean
gpx_limit_import: number | null
is_registration_enabled: boolean
map_attribution: string
max_single_file_size: number | null
max_users: number | null
max_zip_file_size: number | null
}
export interface IApplication {
statistics: IAppStatistics
config: IAppConfig
}
export interface IRootState {
root: boolean
language: string
errorMessages: string | string[] | null
application: IApplication
}
export interface IRootActions {
[ROOT_STORE.ACTIONS.GET_APPLICATION_CONFIG](
context: ActionContext<IRootState, IRootState>
): void
}
export interface IRootGetters {

View File

@ -1,7 +1,7 @@
import { MutationTree } from 'vuex'
import { ROOT_STORE } from '@/store/constants'
import { IRootState } from '@/store/modules/root/interfaces'
import { IAppConfig, IRootState } from '@/store/modules/root/interfaces'
import { TRootMutations } from '@/store/modules/root/types'
export const mutations: MutationTree<IRootState> & TRootMutations = {
@ -14,6 +14,12 @@ export const mutations: MutationTree<IRootState> & TRootMutations = {
) {
state.errorMessages = errorMessages
},
[ROOT_STORE.MUTATIONS.UPDATE_APPLICATION_CONFIG](
state: IRootState,
config: IAppConfig
) {
state.application.config = config
},
[ROOT_STORE.MUTATIONS.UPDATE_LANG](state: IRootState, language: string) {
state.language = language
},

View File

@ -1,7 +1,9 @@
import { IRootState } from '@/store/modules/root/interfaces'
import { IApplication } from '@/store/modules/root/interfaces'
export const state: IRootState = {
root: true,
language: 'en',
errorMessages: null,
application: <IApplication>{},
}

View File

@ -1,7 +1,11 @@
import { Store as VuexStore, CommitOptions } from 'vuex'
import { Store as VuexStore, CommitOptions, DispatchOptions } from 'vuex'
import { ROOT_STORE } from '@/store/constants'
import { IRootGetters, IRootState } from '@/store/modules/root/interfaces'
import {
IRootActions,
IRootGetters,
IRootState,
} from '@/store/modules/root/interfaces'
export type TRootMutations<S = IRootState> = {
[ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGES](state: S): void
@ -16,6 +20,12 @@ export type TRootStoreModule<S = IRootState> = Omit<
VuexStore<S>,
'commit' | 'getters' | 'dispatch'
> & {
dispatch<K extends keyof IRootActions>(
key: K,
payload?: Parameters<IRootActions[K]>[1],
options?: DispatchOptions
): ReturnType<IRootActions[K]>
} & {
getters: {
[K in keyof IRootGetters]: ReturnType<IRootGetters[K]>
}

View File

@ -14,7 +14,9 @@ export const getApiUrl = (): string => {
const removeLastEndOfLine = (text: string): string => text.replace(/\n$/gm, '')
const removeLastDot = (text: string): string => text.replace(/\.$/gm, '')
export const handleError = (
context: ActionContext<IUserState, IRootState>,
context:
| ActionContext<IRootState, IRootState>
| ActionContext<IUserState, IRootState>,
error: AxiosError | null,
msg = 'UNKNOWN'
): void => {