0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-09 05:02:42 +00:00

Merge pull request #2327 from G-Ambatte/addSortType&DirToLocalStorage-#2326

[UserPage] Add sort type & dir to local storage
This commit is contained in:
Trevor Buckner
2022-09-03 23:11:24 -04:00
committed by GitHub

View File

@@ -7,7 +7,10 @@ const moment = require('moment');
const BrewItem = require('./brewItem/brewItem.jsx'); const BrewItem = require('./brewItem/brewItem.jsx');
const USERPAGE_KEY_PREFIX = 'HOMEBREWERY-LISTPAGE-VISIBILITY'; const USERPAGE_KEY_PREFIX = 'HOMEBREWERY-LISTPAGE';
const DEFAULT_SORT_TYPE = 'alpha';
const DEFAULT_SORT_DIR = 'asc';
const ListPage = createClass({ const ListPage = createClass({
displayName : 'ListPage', displayName : 'ListPage',
@@ -32,8 +35,8 @@ const ListPage = createClass({
return { return {
filterString : this.props.query?.filter || '', filterString : this.props.query?.filter || '',
sortType : this.props.query?.sort || 'alpha', sortType : this.props.query?.sort || null,
sortDir : this.props.query?.dir || 'asc', sortDir : this.props.query?.dir || null,
query : this.props.query, query : this.props.query,
brewCollection : brewCollection brewCollection : brewCollection
}; };
@@ -45,12 +48,19 @@ const ListPage = createClass({
// LOAD FROM LOCAL STORAGE // LOAD FROM LOCAL STORAGE
if(typeof window !== 'undefined') { if(typeof window !== 'undefined') {
const newSortType = (this.state.sortType ?? (localStorage.getItem(`${USERPAGE_KEY_PREFIX}-SORTTYPE`) || DEFAULT_SORT_TYPE));
const newSortDir = (this.state.sortDir ?? (localStorage.getItem(`${USERPAGE_KEY_PREFIX}-SORTDIR`) || DEFAULT_SORT_DIR));
this.updateUrl(this.state.filterString, newSortType, newSortDir);
const brewCollection = this.props.brewCollection.map((brewGroup)=>{ const brewCollection = this.props.brewCollection.map((brewGroup)=>{
brewGroup.visible = (localStorage.getItem(`${USERPAGE_KEY_PREFIX}-${brewGroup.class}`) ?? 'true')=='true'; brewGroup.visible = (localStorage.getItem(`${USERPAGE_KEY_PREFIX}-VISIBILITY-${brewGroup.class}`) ?? 'true')=='true';
return brewGroup; return brewGroup;
}); });
this.setState({ this.setState({
brewCollection : brewCollection brewCollection : brewCollection,
sortType : newSortType,
sortDir : newSortDir
}); });
}; };
}, },
@@ -61,8 +71,10 @@ const ListPage = createClass({
saveToLocalStorage : function() { saveToLocalStorage : function() {
this.state.brewCollection.map((brewGroup)=>{ this.state.brewCollection.map((brewGroup)=>{
localStorage.setItem(`${USERPAGE_KEY_PREFIX}-${brewGroup.class}`, `${brewGroup.visible}`); localStorage.setItem(`${USERPAGE_KEY_PREFIX}-VISIBILITY-${brewGroup.class}`, `${brewGroup.visible}`);
}); });
localStorage.setItem(`${USERPAGE_KEY_PREFIX}-SORTTYPE`, this.state.sortType);
localStorage.setItem(`${USERPAGE_KEY_PREFIX}-SORTDIR`, this.state.sortDir);
}, },
renderBrews : function(brews){ renderBrews : function(brews){