Skip to content

Commit

Permalink
Save setting to localstorage
Browse files Browse the repository at this point in the history
  • Loading branch information
drewdaemon committed Feb 10, 2022
1 parent c5dd500 commit 49069af
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
15 changes: 11 additions & 4 deletions x-pack/plugins/lens/public/app_plugin/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import './app.scss';
import React, { useState, useEffect, useCallback, useMemo } from 'react';
import { i18n } from '@kbn/i18n';
import { EuiBreadcrumb } from '@elastic/eui';
import useLocalStorage from 'react-use/lib/useLocalStorage';
import {
createKbnUrlStateStorage,
Storage,
withNotifyOnErrors,
} from '../../../../../src/plugins/kibana_utils/public';
import { useKibana } from '../../../../../src/plugins/kibana_react/public';
Expand All @@ -38,6 +40,7 @@ import { LensInspector } from '../lens_inspector_service';
import { getEditPath } from '../../common';
import { isLensEqual } from './lens_document_equality';
import { disableAutoApply } from '../state_management/lens_slice';
import { readFromStorage, writeToStorage } from '../settings_storage';

export type SaveProps = Omit<OnSaveProps, 'onTitleDuplicate' | 'newDescription'> & {
returnToOrigin: boolean;
Expand Down Expand Up @@ -296,10 +299,14 @@ export function App({
);

const autoApplyEnabled = !Boolean(appliedState);
const toggleAutoApply = useCallback(
() => dispatch(autoApplyEnabled ? disableAutoApply() : enableAutoApply()),
[dispatch, autoApplyEnabled]
);
const toggleAutoApply = useCallback(() => {
writeToStorage(
new Storage(localStorage),
'autoApplyDisabled',
String(typeof appliedState === 'undefined')
);
dispatch(autoApplyEnabled ? disableAutoApply() : enableAutoApply());
}, [dispatch, appliedState, autoApplyEnabled]);

return (
<>
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/lens/public/settings_storage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ export const readFromStorage = (storage: IStorageWrapper, key: string) => {
return data && data[key];
};
export const writeToStorage = (storage: IStorageWrapper, key: string, value: string) => {
storage.set(STORAGE_KEY, { [key]: value });
storage.set(STORAGE_KEY, { ...storage.get(STORAGE_KEY), [key]: value });
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import { MiddlewareAPI } from '@reduxjs/toolkit';
import { i18n } from '@kbn/i18n';
import { History } from 'history';
import { setState, initEmpty, LensStoreDeps } from '..';
import { getPreloadedState } from '../lens_slice';
import { disableAutoApply, getPreloadedState } from '../lens_slice';
import { SharingSavedObjectProps } from '../../types';
import { LensEmbeddableInput, LensByReferenceInput } from '../../embeddable/embeddable';
import { getInitialDatasourceId } from '../../utils';
import { initializeDatasources } from '../../editor_frame_service/editor_frame';
import { LensAppServices } from '../../app_plugin/types';
import { getEditPath, getFullPath, LENS_EMBEDDABLE_TYPE } from '../../../common/constants';
import { Document } from '../../persistence';
import { readFromStorage } from '../../settings_storage';
import { Storage } from '../../../../../../src/plugins/kibana_utils/public';

export const getPersisted = async ({
initialInput,
Expand Down Expand Up @@ -209,6 +211,12 @@ export function loadInitial(
isLoading: false,
})
);

const autoApplyDisabled =
readFromStorage(new Storage(localStorage), 'autoApplyDisabled') === 'true';
if (autoApplyDisabled) {
store.dispatch(disableAutoApply());
}
})
.catch((e: { message: string }) =>
notifications.toasts.addDanger({
Expand Down

0 comments on commit 49069af

Please sign in to comment.