Skip to content

Commit

Permalink
feat: always use localStorage if no settingsApi (#603)
Browse files Browse the repository at this point in the history
  • Loading branch information
artemmufazalov authored Dec 7, 2023
1 parent 8faeabc commit ff692df
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
3 changes: 1 addition & 2 deletions src/services/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ import type {StorageApiRequestParams} from '../store/reducers/storage/types';

import {backend as BACKEND} from '../store';
import {prepareSortValue} from '../utils/filters';
import {settingsApi} from '../utils/settings';

const config = {withCredentials: !window.custom_backend};

const settingsApi = window.web_version ? window.systemSettings?.settingsApi : undefined;

type AxiosOptions = {
concurrentId?: string;
};
Expand Down
20 changes: 14 additions & 6 deletions src/store/reducers/settings/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ import {
import '../../../services/api';
import {parseJson} from '../../../utils/utils';
import {QUERY_ACTIONS, QUERY_MODES} from '../../../utils/query';
import {readSavedSettingsValue, systemSettings, userSettings} from '../../../utils/settings';
import {
readSavedSettingsValue,
settingsApi,
systemSettings,
userSettings,
} from '../../../utils/settings';

import {TENANT_PAGES_IDS} from '../tenant/constants';

Expand Down Expand Up @@ -115,13 +120,16 @@ export const setSettingValue = (
name: string,
value: string,
): ThunkAction<void, RootState, unknown, SetSettingValueAction> => {
return (dispatch, getState) => {
return (dispatch) => {
dispatch({type: SET_SETTING_VALUE, data: {name, value}});
const {singleClusterMode} = getState();
if (singleClusterMode) {
localStorage.setItem(name, value);
} else {

// If there is no settingsApi, use localStorage
if (settingsApi) {
window.api.postSetting(name, value);
} else {
try {
localStorage.setItem(name, value);
} catch {}
}
};
};
Expand Down
5 changes: 4 additions & 1 deletion src/utils/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import {getValueFromLS} from './utils';
export const userSettings = window.userSettings || {};
export const systemSettings = window.systemSettings || {};

export const settingsApi = window.web_version ? systemSettings.settingsApi : undefined;

export function readSavedSettingsValue(key: string, defaultValue?: string) {
const savedValue = window.web_version ? userSettings[key] : getValueFromLS(key);
// If there is no settingsApi, use localStorage
const savedValue = settingsApi ? userSettings[key] : getValueFromLS(key);

return savedValue ?? defaultValue;
}

0 comments on commit ff692df

Please sign in to comment.