clientConfig.ts
TLDR
This file, clientConfig.ts
, contains the implementation of a Redux slice for handling client configuration. It includes a method for fetching the client configuration from an external API and a getter function to retrieve the client configuration from the Redux store.
Methods
fetchClientConfig
This method is an async thunk action creator that fetches the client configuration from an external API. It is dispatched with the clientConfig/fetchClientConfig
action type. It uses the octoClient
module to make the API request and returns the fetched client config.
setClientConfig
This method is a reducer action creator that sets the client configuration in the Redux store. It takes a payload of type ClientConfig
and updates the state with the provided value.
Classes
None
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {createSlice, createAsyncThunk, PayloadAction} from '@reduxjs/toolkit'
import {ClientConfig} from '../config/clientConfig'
import {default as client} from '../octoClient'
import {ShowUsername} from '../utils'
import {RootState} from './index'
export const fetchClientConfig = createAsyncThunk(
'clientConfig/fetchClientConfig',
async () => client.getClientConfig(),
)
const clientConfigSlice = createSlice({
name: 'config',
initialState: {value: {telemetry: false, telemetryid: '', enablePublicSharedBoards: false, teammateNameDisplay: ShowUsername, featureFlags: {}, maxFileSize: 0}} as {value: ClientConfig},
reducers: {
setClientConfig: (state, action: PayloadAction<ClientConfig>) => {
state.value = action.payload
},
},
extraReducers: (builder) => {
builder.addCase(fetchClientConfig.fulfilled, (state, action) => {
state.value = action.payload || {telemetry: false, telemetryid: '', enablePublicSharedBoards: false, teammateNameDisplay: ShowUsername, featureFlags: {}, maxFileSize: 0}
})
},
})
export const {setClientConfig} = clientConfigSlice.actions
export const {reducer} = clientConfigSlice
export function getClientConfig(state: RootState): ClientConfig {
return state.clientConfig.value
}