grafana/public/app/features/profile/state/reducers.ts
kay delaney bad048b7ba
Performance: Standardize lodash imports to use destructured members (#33040)
* Performance: Standardize lodash imports to use destructured members
Changes lodash imports of the form `import x from 'lodash/x'` to
`import { x } from 'lodash'` to reduce bundle size.

* Remove unnecessary _ import from Graph component

* Enforce lodash import style

* Fix remaining lodash imports
2021-04-21 09:38:00 +02:00

41 lines
1.1 KiB
TypeScript

import { isString, isEmpty, set } from 'lodash';
import { PayloadAction, createSlice } from '@reduxjs/toolkit';
import { UserState, ThunkResult } from 'app/types';
import config from 'app/core/config';
import { TimeZone } from '@grafana/data';
import { contextSrv } from 'app/core/core';
export const initialState: UserState = {
orgId: config.bootData.user.orgId,
timeZone: config.bootData.user.timezone,
};
export const slice = createSlice({
name: 'user/profile',
initialState,
reducers: {
updateTimeZone: (state, action: PayloadAction<TimeZone>): UserState => {
return {
...state,
timeZone: action.payload,
};
},
},
});
export const updateTimeZoneForSession = (timeZone: TimeZone): ThunkResult<void> => {
return async (dispatch) => {
const { updateTimeZone } = slice.actions;
if (!isString(timeZone) || isEmpty(timeZone)) {
timeZone = config?.bootData?.user?.timezone;
}
set(contextSrv, 'user.timezone', timeZone);
dispatch(updateTimeZone(timeZone));
};
};
export const userReducer = slice.reducer;
export default { user: slice.reducer };