cache filters in session storage

This commit is contained in:
b1ek 2023-05-11 23:20:10 +10:00
parent c91eba0a62
commit eb89bb3526
Signed by: blek
GPG Key ID: 14546221E3595D0C
1 changed files with 11 additions and 5 deletions

View File

@ -174,7 +174,8 @@ const FiltersForm = (props) => {
{ value: 1, label: '1 комната' }, { value: 1, label: '1 комната' },
{ value: 2, label: '2 комнаты' }, { value: 2, label: '2 комнаты' },
{ value: 3, label: '3 комнаты' }, { value: 3, label: '3 комнаты' },
{ value: 4, label: '4 комнаты' } { value: 4, label: '4 комнаты' },
{ value: -1, label: 'Выберите' }
]; ];
const def_form = { const def_form = {
@ -194,10 +195,15 @@ const FiltersForm = (props) => {
form_err: false form_err: false
}; };
let cached_filters = window.sessionStorage.getItem('pairent_filters');
if (cached_filters) cached_filters = JSON.parse(cached_filters);
const [ state, setState_ ] = useState(def_form); const [ state, setState_ ] = useState(cached_filters || def_form);
const setState = (new_state) => { const setState = (new_state) => {
window.sessionStorage.setItem('pairent_filters', JSON.stringify({ ...state, ...new_state }));
return setState_({...state, ...new_state}); return setState_({...state, ...new_state});
} }
@ -358,10 +364,10 @@ export default class IndexPage extends React.Component {
} }
render() { render() {
const { page, pageSize } = this.state; let { page, pageSize } = this.state;
const pages = this.state.apartments.length / pageSize; let pages = this.state.apartments.length / pageSize;
const current_data = this.state.apartments.slice((page * pageSize), (page * pageSize) + pageSize); let current_data = this.state.apartments.slice((page * pageSize), (page * pageSize) + pageSize);
return ( return (