mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-11-25 16:17:34 +00:00
listPage - Reorder and clean up functions
This commit is contained in:
1 parent
a7a368edcc
commit
da0a0c2766
1 file changed
+30
-42
@@ -38,23 +38,41 @@ const ListPage = ({ brewCollection = [{ title: '', class: '', brews: [] }], navI
|
||||
|
||||
const newSortType = sortType ?? (localStorage.getItem(USERPAGE_SORT_TYPE) || DEFAULT_SORT_TYPE);
|
||||
const newSortDir = sortDir ?? (localStorage.getItem(USERPAGE_SORT_DIR) || DEFAULT_SORT_DIR);
|
||||
updateUrl(filterString, newSortType, newSortDir, filterTags);
|
||||
const newLayoutMode = localStorage.getItem(USERPAGE_LAYOUT_MODE) || DEFAULT_LAYOUT_MODE;
|
||||
const newThemeMode = localStorage.getItem(USERPAGE_THEME_MODE) || DEFAULT_THEME_MODE;
|
||||
|
||||
const namedBrewCollection = brewCollection.reduce((visibility, brewGroup)=>{
|
||||
const initialGroupVisibility = brewCollection.reduce((visibility, brewGroup)=>{
|
||||
visibility[brewGroup.class] = (localStorage.getItem(`${USERPAGE_GROUP_VISIBILITY_PREFIX}_${brewGroup.class}`) ?? 'true') == 'true';
|
||||
return visibility;
|
||||
}, {});
|
||||
|
||||
const newLayoutMode = localStorage.getItem(USERPAGE_LAYOUT_MODE) || DEFAULT_LAYOUT_MODE;
|
||||
const newThemeMode = localStorage.getItem(USERPAGE_THEME_MODE) || DEFAULT_THEME_MODE;
|
||||
|
||||
setGroupVisibility(namedBrewCollection);
|
||||
setGroupVisibility(initialGroupVisibility);
|
||||
setSortType(newSortType);
|
||||
setSortDir(newSortDir);
|
||||
setLayoutMode(newLayoutMode);
|
||||
setThemeMode(newThemeMode);
|
||||
|
||||
updateUrl(filterString, newSortType, newSortDir, filterTags);
|
||||
}, []);
|
||||
|
||||
const updateUrl = (filterTerm, sortType, sortDir, filterTags)=>{
|
||||
const url = new URL(window.location.href);
|
||||
const urlParams = new URLSearchParams(url.search);
|
||||
|
||||
urlParams.set('sort', sortType);
|
||||
urlParams.set('dir', sortDir);
|
||||
|
||||
// Add tags to URL in alphabetical order
|
||||
urlParams.delete('tag');
|
||||
filterTags.forEach((tag)=>urlParams.append('tag', tag));
|
||||
|
||||
if(!filterTerm) urlParams.delete('filter');
|
||||
else urlParams.set('filter', filterTerm);
|
||||
|
||||
url.search = urlParams;
|
||||
window.history.replaceState(null, null, url);
|
||||
};
|
||||
|
||||
const renderBrews = (brews)=>{
|
||||
if(!brews || !brews.length) return <div className='noBrews'>No Brews.</div>;
|
||||
|
||||
@@ -116,24 +134,6 @@ const ListPage = ({ brewCollection = [{ title: '', class: '', brews: [] }], navI
|
||||
updateUrl(filterString, sortType, sortDir, newFilterTags);
|
||||
};
|
||||
|
||||
const updateUrl = (filterTerm, sortType, sortDir, filterTags)=>{
|
||||
const url = new URL(window.location.href);
|
||||
const urlParams = new URLSearchParams(url.search);
|
||||
|
||||
urlParams.set('sort', sortType);
|
||||
urlParams.set('dir', sortDir);
|
||||
|
||||
// Add tags to URL in alphabetical order
|
||||
urlParams.delete('tag');
|
||||
filterTags.forEach((tag)=>urlParams.append('tag', tag));
|
||||
|
||||
if(!filterTerm) urlParams.delete('filter');
|
||||
else urlParams.set('filter', filterTerm);
|
||||
|
||||
url.search = urlParams;
|
||||
window.history.replaceState(null, null, url);
|
||||
};
|
||||
|
||||
const renderFilterOption = ()=>(
|
||||
<div className='filter-option'>
|
||||
<label>
|
||||
@@ -163,8 +163,7 @@ const ListPage = ({ brewCollection = [{ title: '', class: '', brews: [] }], navI
|
||||
);
|
||||
};
|
||||
|
||||
const renderSortOptions = ()=>{
|
||||
return (
|
||||
const renderSortOptions = ()=>(
|
||||
<div className='radio-toggle sort-container'>
|
||||
<h6>Sort by :</h6>
|
||||
{renderSortOption('Title', 'alpha')}
|
||||
@@ -173,7 +172,6 @@ const ListPage = ({ brewCollection = [{ title: '', class: '', brews: [] }], navI
|
||||
{renderSortOption('Views', 'views')}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const getSortedBrews = (brews)=>{
|
||||
const testString = _.deburr(filterString).toLowerCase();
|
||||
@@ -200,13 +198,9 @@ const ListPage = ({ brewCollection = [{ title: '', class: '', brews: [] }], navI
|
||||
}, [brewCollection, filterString, filterTags, sortType, sortDir]);
|
||||
|
||||
const toggleBrewCollectionState = (brewGroupClass)=>{
|
||||
setGroupVisibility((prevVisibility)=>{
|
||||
const visible = !prevVisibility[brewGroupClass];
|
||||
|
||||
const visible = !groupVisibility[brewGroupClass];
|
||||
localStorage.setItem(`${USERPAGE_GROUP_VISIBILITY_PREFIX}_${brewGroupClass}`, visible);
|
||||
|
||||
return {...prevVisibility, [brewGroupClass]: visible};
|
||||
});
|
||||
setGroupVisibility({...groupVisibility, [brewGroupClass]: visible});
|
||||
};
|
||||
|
||||
const renderBrewCollection = ()=>{
|
||||
@@ -234,8 +228,7 @@ const ListPage = ({ brewCollection = [{ title: '', class: '', brews: [] }], navI
|
||||
localStorage.setItem(USERPAGE_LAYOUT_MODE, mode);
|
||||
};
|
||||
|
||||
const renderLayoutModeOptions = ()=>{
|
||||
return (
|
||||
const renderLayoutModeOptions = ()=>(
|
||||
<div className='radio-toggle layout-container'>
|
||||
<h6>View:</h6>
|
||||
<label className='option' title='grid'>
|
||||
@@ -252,15 +245,13 @@ const ListPage = ({ brewCollection = [{ title: '', class: '', brews: [] }], navI
|
||||
</label>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const handleThemeChange = (mode)=>{
|
||||
setThemeMode(mode);
|
||||
localStorage.setItem(USERPAGE_THEME_MODE, mode);
|
||||
};
|
||||
|
||||
const renderThemePicker = ()=>{
|
||||
return (
|
||||
const renderThemePicker = ()=>(
|
||||
<div className='radio-toggle theme-container'>
|
||||
<h6>Theme:</h6>
|
||||
<label className='option'>
|
||||
@@ -273,10 +264,8 @@ const ListPage = ({ brewCollection = [{ title: '', class: '', brews: [] }], navI
|
||||
</label>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const renderNav = ()=>{
|
||||
return (
|
||||
const renderNav = ()=>(
|
||||
<div className='listPageNav'>
|
||||
{renderLayoutModeOptions()}
|
||||
{renderThemePicker()}
|
||||
@@ -285,7 +274,6 @@ const ListPage = ({ brewCollection = [{ title: '', class: '', brews: [] }], navI
|
||||
{renderTagsOptions()}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className='sitePage listPage'>
|
||||
|
||||
Reference in new issue
Block a user