Skip to content

Commit

Permalink
ADG-7473 get table page size from local storage
Browse files Browse the repository at this point in the history
  • Loading branch information
IldarKamalov committed Oct 9, 2023
1 parent 7201d00 commit b6bf48a
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 34 deletions.
4 changes: 3 additions & 1 deletion client/src/components/Dashboard/Clients.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { BLOCK_ACTIONS, STATUS_COLORS } from '../../helpers/constants';
import { toggleClientBlock } from '../../actions/access';
import { renderFormattedClientCell } from '../../helpers/renderFormattedClientCell';
import { getStats } from '../../actions/stats';
import { LocalStorageHelper, LOCAL_STORAGE_KEYS } from '../../helpers/localStorageHelper';

const getClientsPercentColor = (percent) => {
if (percent > 50) {
Expand Down Expand Up @@ -135,7 +136,8 @@ const Clients = ({
showPagination={false}
noDataText={t('no_clients_found')}
minRows={6}
defaultPageSize={100}
defaultPageSize={LocalStorageHelper.getItem(LOCAL_STORAGE_KEYS.CLIENTS_PAGE_SIZE) || 100}
onPageSizeChange={(size) => LocalStorageHelper.setItem(LOCAL_STORAGE_KEYS.CLIENTS_PAGE_SIZE, size)}
className="-highlight card-table-overflow--limited clients__table"
getTrProps={(_state, rowInfo) => {
if (!rowInfo) {
Expand Down
4 changes: 3 additions & 1 deletion client/src/components/Filters/Rewrites/Table.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ReactTable from 'react-table';
import { withTranslation } from 'react-i18next';
import { sortIp } from '../../../helpers/helpers';
import { MODAL_TYPE } from '../../../helpers/constants';
import { LocalStorageHelper, LOCAL_STORAGE_KEYS } from '../../../helpers/localStorageHelper';

class Table extends Component {
cellWrap = ({ value }) => (
Expand Down Expand Up @@ -85,7 +86,8 @@ class Table extends Component {
loading={processing || processingAdd || processingDelete}
className="-striped -highlight card-table-overflow"
showPagination
defaultPageSize={10}
defaultPageSize={LocalStorageHelper.getItem(LOCAL_STORAGE_KEYS.REWRITES_PAGE_SIZE) || 10}
onPageSizeChange={(size) => LocalStorageHelper.setItem(LOCAL_STORAGE_KEYS.REWRITES_PAGE_SIZE, size)}
minRows={5}
ofText="/"
previousText={t('previous_btn')}
Expand Down
8 changes: 7 additions & 1 deletion client/src/components/Filters/Table.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import CellWrap from '../ui/CellWrap';
import { MODAL_TYPE } from '../../helpers/constants';
import { formatDetailedDateTime } from '../../helpers/helpers';
import { isValidAbsolutePath } from '../../helpers/form';
import { LOCAL_STORAGE_KEYS, LocalStorageHelper } from '../../helpers/localStorageHelper';

class Table extends Component {
getDateCell = (row) => CellWrap(row, formatDetailedDateTime);
Expand Down Expand Up @@ -126,12 +127,17 @@ class Table extends Component {
loading, filters, t, whitelist,
} = this.props;

const localStorageKey = whitelist
? LOCAL_STORAGE_KEYS.ALLOWLIST_PAGE_SIZE
: LOCAL_STORAGE_KEYS.BLOCKLIST_PAGE_SIZE;

return (
<ReactTable
data={filters}
columns={this.columns}
showPagination
defaultPageSize={10}
defaultPageSize={LocalStorageHelper.getItem(localStorageKey) || 10}
onPageSizeChange={(size) => LocalStorageHelper.setItem(localStorageKey, size)}
loading={loading}
minRows={6}
ofText="/"
Expand Down
6 changes: 5 additions & 1 deletion client/src/components/Settings/Clients/AutoClients.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import CellWrap from '../../ui/CellWrap';
import whoisCell from './whoisCell';
import LogsSearchLink from '../../ui/LogsSearchLink';
import { sortIp } from '../../../helpers/helpers';
import { LocalStorageHelper, LOCAL_STORAGE_KEYS } from '../../../helpers/localStorageHelper';

const COLUMN_MIN_WIDTH = 200;

Expand Down Expand Up @@ -85,7 +86,10 @@ class AutoClients extends Component {
]}
className="-striped -highlight card-table-overflow"
showPagination
defaultPageSize={10}
defaultPageSize={LocalStorageHelper.getItem(LOCAL_STORAGE_KEYS.AUTO_CLIENTS_PAGE_SIZE) || 10}
onPageSizeChange={(size) => (
LocalStorageHelper.setItem(LOCAL_STORAGE_KEYS.AUTO_CLIENTS_PAGE_SIZE, size)
)}
minRows={5}
ofText="/"
previousText={t('previous_btn')}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import Card from '../../../ui/Card';
import CellWrap from '../../../ui/CellWrap';
import LogsSearchLink from '../../../ui/LogsSearchLink';
import Modal from '../Modal';
import { LocalStorageHelper, LOCAL_STORAGE_KEYS } from '../../../../helpers/localStorageHelper';

const ClientsTable = ({
clients,
Expand Down Expand Up @@ -342,7 +343,10 @@ const ClientsTable = ({
]}
className="-striped -highlight card-table-overflow"
showPagination
defaultPageSize={10}
defaultPageSize={LocalStorageHelper.getItem(LOCAL_STORAGE_KEYS.CLIENTS_PAGE_SIZE) || 10}
onPageSizeChange={(size) => (
LocalStorageHelper.setItem(LOCAL_STORAGE_KEYS.CLIENTS_PAGE_SIZE, size)
)}
minRows={5}
ofText="/"
previousText={t('previous_btn')}
Expand Down
2 changes: 0 additions & 2 deletions client/src/helpers/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,4 @@ export const DISABLE_PROTECTION_TIMINGS = {
TOMORROW: 24 * 60 * 60 * 1000,
};

export const LOCAL_STORAGE_THEME_KEY = 'account_theme';

export const LOCAL_TIMEZONE_VALUE = 'Local';
30 changes: 3 additions & 27 deletions client/src/helpers/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import {
STANDARD_WEB_PORT,
SPECIAL_FILTER_ID,
THEMES,
LOCAL_STORAGE_THEME_KEY,
} from './constants';
import { LOCAL_STORAGE_KEYS, LocalStorageHelper } from './localStorageHelper';

/**
* @param time {string} The time to format
Expand Down Expand Up @@ -680,37 +680,13 @@ export const setHtmlLangAttr = (language) => {
window.document.documentElement.lang = language;
};

/**
* Set local storage field
*
* @param {string} key
* @param {string} value
*/

export const setStorageItem = (key, value) => {
if (window.localStorage) {
window.localStorage.setItem(key, value);
}
};

/**
* Get local storage field
*
* @param {string} key
*/

export const getStorageItem = (key) => (window.localStorage
? window.localStorage.getItem(key)
: null);

/**
* Set local storage theme field
*
* @param {string} theme
*/

export const setTheme = (theme) => {
setStorageItem(LOCAL_STORAGE_THEME_KEY, theme);
LocalStorageHelper.setItem(LOCAL_STORAGE_KEYS.THEME, theme);
};

/**
Expand All @@ -719,7 +695,7 @@ export const setTheme = (theme) => {
* @returns {string}
*/

export const getTheme = () => getStorageItem(LOCAL_STORAGE_THEME_KEY) || THEMES.light;
export const getTheme = () => LocalStorageHelper.getItem(LOCAL_STORAGE_KEYS.THEME) || THEMES.light;

/**
* Sets UI theme.
Expand Down
44 changes: 44 additions & 0 deletions client/src/helpers/localStorageHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
export const LOCAL_STORAGE_KEYS = {
THEME: 'account_theme',
BLOCKLIST_PAGE_SIZE: 'blocklist_page_size',
ALLOWLIST_PAGE_SIZE: 'allowlist_page_size',
CLIENTS_PAGE_SIZE: 'clients_page_size',
REWRITES_PAGE_SIZE: 'rewrites_page_size',
AUTO_CLIENTS_PAGE_SIZE: 'auto_clients_page_size',
};

export const LocalStorageHelper = {
setItem(key, value) {
try {
localStorage.setItem(key, JSON.stringify(value));
} catch (error) {
console.error(`Error setting ${key} in local storage: ${error.message}`);
}
},

getItem(key) {
try {
const item = localStorage.getItem(key);
return item ? JSON.parse(item) : null;
} catch (error) {
console.error(`Error getting ${key} from local storage: ${error.message}`);
return null;
}
},

removeItem(key) {
try {
localStorage.removeItem(key);
} catch (error) {
console.error(`Error removing ${key} from local storage: ${error.message}`);
}
},

clear() {
try {
localStorage.clear();
} catch (error) {
console.error(`Error clearing local storage: ${error.message}`);
}
},
};

0 comments on commit b6bf48a

Please sign in to comment.