feat: save list options to local storage (#3055)

* feat: save list options to local storage

* fix: removing of the columns field

* fix: filter body column from columns

* fix: typo for raw

---------

Co-authored-by: Vishal Sharma <makeavish786@gmail.com>
This commit is contained in:
Yevhen Shevchenko 2023-07-06 18:56:51 +03:00 committed by GitHub
parent eb2a955323
commit 360029f350
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 62 additions and 24 deletions

View File

@ -2,7 +2,7 @@
"options_menu": { "options_menu": {
"options": "Options", "options": "Options",
"format": "Format", "format": "Format",
"row": "Row", "raw": "Raw",
"default": "Default", "default": "Default",
"column": "Column", "column": "Column",
"maxLines": "Max lines per Row", "maxLines": "Max lines per Row",

View File

@ -2,7 +2,7 @@
"options_menu": { "options_menu": {
"options": "Options", "options": "Options",
"format": "Format", "format": "Format",
"row": "Row", "raw": "Raw",
"default": "Default", "default": "Default",
"column": "Column", "column": "Column",
"maxLines": "Max lines per Row", "maxLines": "Max lines per Row",

View File

@ -6,4 +6,5 @@ export enum LOCALSTORAGE {
THEME = 'THEME', THEME = 'THEME',
LOGS_VIEW_MODE = 'LOGS_VIEW_MODE', LOGS_VIEW_MODE = 'LOGS_VIEW_MODE',
LOGS_LINES_PER_ROW = 'LOGS_LINES_PER_ROW', LOGS_LINES_PER_ROW = 'LOGS_LINES_PER_ROW',
LIST_OPTIONS = 'LIST_OPTIONS',
} }

View File

@ -18,7 +18,7 @@ function FormatField({ config }: FormatFieldProps): JSX.Element | null {
value={config.value} value={config.value}
onChange={config.onChange} onChange={config.onChange}
> >
<RadioButton value="raw">{t('options_menu.row')}</RadioButton> <RadioButton value="raw">{t('options_menu.raw')}</RadioButton>
<RadioButton value="list">{t('options_menu.default')}</RadioButton> <RadioButton value="list">{t('options_menu.default')}</RadioButton>
<RadioButton value="table">{t('options_menu.column')}</RadioButton> <RadioButton value="table">{t('options_menu.column')}</RadioButton>
</RadioGroup> </RadioGroup>

View File

@ -1,5 +1,8 @@
import { RadioChangeEvent } from 'antd'; import { RadioChangeEvent } from 'antd';
import getFromLocalstorage from 'api/browser/localstorage/get';
import setToLocalstorage from 'api/browser/localstorage/set';
import { getAggregateKeys } from 'api/queryBuilder/getAttributeKeys'; import { getAggregateKeys } from 'api/queryBuilder/getAttributeKeys';
import { LOCALSTORAGE } from 'constants/localStorage';
import { QueryBuilderKeys } from 'constants/queryBuilder'; import { QueryBuilderKeys } from 'constants/queryBuilder';
import useUrlQueryData from 'hooks/useUrlQueryData'; import useUrlQueryData from 'hooks/useUrlQueryData';
import { useCallback, useEffect, useMemo } from 'react'; import { useCallback, useEffect, useMemo } from 'react';
@ -28,6 +31,10 @@ const useOptionsMenu = ({
aggregateOperator, aggregateOperator,
initialOptions = {}, initialOptions = {},
}: UseOptionsMenuProps): UseOptionsMenu => { }: UseOptionsMenuProps): UseOptionsMenu => {
const localStorageOptionsQuery = getFromLocalstorage(
LOCALSTORAGE.LIST_OPTIONS,
);
const { const {
query: optionsQuery, query: optionsQuery,
queryData: optionsQueryData, queryData: optionsQueryData,
@ -66,9 +73,21 @@ const useOptionsMenu = ({
[optionsQueryData], [optionsQueryData],
); );
const addColumnOptions = useMemo( const addColumnOptions = useMemo(() => {
() => getOptionsFromKeys(attributeKeys, selectedColumnKeys), const filteredAttributeKeys = attributeKeys.filter(
[attributeKeys, selectedColumnKeys], (item) => item.key !== 'body',
);
return getOptionsFromKeys(filteredAttributeKeys, selectedColumnKeys);
}, [attributeKeys, selectedColumnKeys]);
const handleRedirectWithOptionsData = useCallback(
(newQueryData: OptionsQuery) => {
redirectWithOptionsData(newQueryData);
setToLocalstorage(LOCALSTORAGE.LIST_OPTIONS, JSON.stringify(newQueryData));
},
[redirectWithOptionsData],
); );
const handleSelectedColumnsChange = useCallback( const handleSelectedColumnsChange = useCallback(
@ -83,15 +102,17 @@ const useOptionsMenu = ({
return [...acc, column]; return [...acc, column];
}, [] as BaseAutocompleteData[]); }, [] as BaseAutocompleteData[]);
redirectWithOptionsData({ const optionsData: OptionsQuery = {
...optionsQueryData, ...optionsQueryData,
selectColumns: newSelectedColumns, selectColumns: newSelectedColumns,
}); };
handleRedirectWithOptionsData(optionsData);
}, },
[ [
selectedColumnKeys, selectedColumnKeys,
redirectWithOptionsData,
optionsQueryData, optionsQueryData,
handleRedirectWithOptionsData,
attributeKeys, attributeKeys,
], ],
); );
@ -102,48 +123,54 @@ const useOptionsMenu = ({
({ id }) => id !== columnKey, ({ id }) => id !== columnKey,
); );
redirectWithOptionsData({ const optionsData: OptionsQuery = {
...defaultOptionsQuery, ...optionsQueryData,
selectColumns: newSelectedColumns, selectColumns: newSelectedColumns,
}); };
handleRedirectWithOptionsData(optionsData);
}, },
[optionsQueryData, redirectWithOptionsData], [optionsQueryData, handleRedirectWithOptionsData],
); );
const handleFormatChange = useCallback( const handleFormatChange = useCallback(
(event: RadioChangeEvent) => { (event: RadioChangeEvent) => {
redirectWithOptionsData({ const optionsData: OptionsQuery = {
...optionsQueryData, ...optionsQueryData,
format: event.target.value, format: event.target.value,
}); };
handleRedirectWithOptionsData(optionsData);
}, },
[optionsQueryData, redirectWithOptionsData], [handleRedirectWithOptionsData, optionsQueryData],
); );
const handleMaxLinesChange = useCallback( const handleMaxLinesChange = useCallback(
(value: string | number | null) => { (value: string | number | null) => {
redirectWithOptionsData({ const optionsData: OptionsQuery = {
...optionsQueryData, ...optionsQueryData,
maxLines: value as number, maxLines: value as number,
}); };
handleRedirectWithOptionsData(optionsData);
}, },
[optionsQueryData, redirectWithOptionsData], [handleRedirectWithOptionsData, optionsQueryData],
); );
const optionsMenuConfig: Required<OptionsMenuConfig> = useMemo( const optionsMenuConfig: Required<OptionsMenuConfig> = useMemo(
() => ({ () => ({
addColumn: { addColumn: {
value: optionsQueryData?.selectColumns || defaultOptionsQuery.selectColumns, value: optionsQueryData.selectColumns || defaultOptionsQuery.selectColumns,
options: addColumnOptions || [], options: addColumnOptions || [],
onChange: handleSelectedColumnsChange, onChange: handleSelectedColumnsChange,
onRemove: handleRemoveSelectedColumn, onRemove: handleRemoveSelectedColumn,
}, },
format: { format: {
value: optionsQueryData?.format || defaultOptionsQuery.format, value: optionsQueryData.format || defaultOptionsQuery.format,
onChange: handleFormatChange, onChange: handleFormatChange,
}, },
maxLines: { maxLines: {
value: optionsQueryData?.maxLines || defaultOptionsQuery.maxLines, value: optionsQueryData.maxLines || defaultOptionsQuery.maxLines,
onChange: handleMaxLinesChange, onChange: handleMaxLinesChange,
}, },
}), }),
@ -162,8 +189,18 @@ const useOptionsMenu = ({
useEffect(() => { useEffect(() => {
if (optionsQuery || !isFetched) return; if (optionsQuery || !isFetched) return;
redirectWithOptionsData(initialOptionsQuery); const nextOptionsQuery = localStorageOptionsQuery
}, [isFetched, optionsQuery, initialOptionsQuery, redirectWithOptionsData]); ? JSON.parse(localStorageOptionsQuery)
: initialOptionsQuery;
redirectWithOptionsData(nextOptionsQuery);
}, [
isFetched,
optionsQuery,
initialOptionsQuery,
redirectWithOptionsData,
localStorageOptionsQuery,
]);
return { return {
isLoading, isLoading,