@adaptabletools/adaptable
Version:
Powerful AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements
91 lines (90 loc) • 3.13 kB
JavaScript
import { EMPTY_ARRAY } from '../../Utilities/Constants/GeneralConstants';
import { THEME_DEFAULT_CURRENT_THEME, SYSTEM_THEMES, } from '../../Utilities/Constants/ReduxConstants';
export const THEME_SET_SYSTEM_THEMES = 'THEME_SET_SYSTEM_THEMES';
export const THEME_SET_USER_THEMES = 'THEME_SET_USER_THEMES';
export const THEME_SELECT = 'THEME_SELECT';
export const THEME_READY = 'THEME_READY';
export const THEME_EDIT = 'THEME_EDIT';
export const THEME_ADD = 'THEME_ADD';
export const THEME_DELETE = 'THEME_DELETE';
export const ThemeSetSystemThemes = (SystemThemes) => ({
type: THEME_SET_SYSTEM_THEMES,
SystemThemes,
});
export const ThemeSetUserThemes = (UserThemes) => ({
type: THEME_SET_USER_THEMES,
UserThemes,
});
export const ThemeSelect = (Theme) => ({
type: THEME_SELECT,
Theme,
});
export const ThemeReady = (themeState) => ({
type: THEME_READY,
themeState,
});
export const ThemeEdit = (theme) => ({
type: THEME_EDIT,
theme,
});
export const ThemeAdd = (theme) => ({
type: THEME_ADD,
theme,
});
export const ThemeDelete = (theme) => ({
type: THEME_DELETE,
theme,
});
const initialState = {
CurrentTheme: THEME_DEFAULT_CURRENT_THEME,
SystemThemes: SYSTEM_THEMES,
UserThemes: EMPTY_ARRAY,
};
export const ThemeReducer = (state = initialState, action) => {
switch (action.type) {
case THEME_SET_SYSTEM_THEMES:
return Object.assign({}, state, {
SystemThemes: action.SystemThemes,
});
case THEME_SET_USER_THEMES:
return Object.assign({}, state, {
UserThemes: action.UserThemes,
});
case THEME_SELECT:
return Object.assign({}, state, {
CurrentTheme: action.Theme,
});
case THEME_EDIT:
let newThemes = null;
const themeToBeReplaced = action.theme;
let replaceIndex = state.UserThemes.findIndex((theme) => themeToBeReplaced.Uuid === theme.Uuid);
if (replaceIndex === -1) {
replaceIndex = state.UserThemes.findIndex((theme) => themeToBeReplaced.Name === theme.Name);
}
if (replaceIndex !== -1) {
newThemes = [...state.UserThemes];
newThemes[replaceIndex] = themeToBeReplaced;
}
if (newThemes === null) {
return state;
}
let currentTheme = state.CurrentTheme;
if (currentTheme !== themeToBeReplaced.Name) {
currentTheme = themeToBeReplaced.Name;
}
return Object.assign({}, state, {
CurrentTheme: currentTheme,
UserThemes: newThemes,
});
case THEME_ADD:
return Object.assign({}, state, {
UserThemes: [...state.UserThemes, action.theme],
});
case THEME_DELETE:
return Object.assign({}, state, {
UserThemes: state.UserThemes.filter((theme) => theme.Uuid !== action.theme.Uuid),
});
default:
return state;
}
};