0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-08 03:12:40 +00:00

Initial pass at visibility functionality

This commit is contained in:
G.Ambatte
2022-08-28 23:46:19 +12:00
parent 0811da79c4
commit 4bb5e96c42
3 changed files with 82 additions and 16 deletions

View File

@@ -6,28 +6,58 @@ const moment = require('moment');
const BrewItem = require('./brewItem/brewItem.jsx'); const BrewItem = require('./brewItem/brewItem.jsx');
const USERPAGE_KEY_PREFIX = 'HOMEBREWERY-LISTPAGE-VISIBILITY';
const ListPage = createClass({ const ListPage = createClass({
displayName : 'ListPage', displayName : 'ListPage',
getDefaultProps : function() { getDefaultProps : function() {
return { return {
brewCollection : [ brewCollection : [
{ {
title : '', title : '',
class : '', class : '',
brews : [] brews : [],
visible : true
} }
], ],
navItems : <></> navItems : <></>
}; };
}, },
getInitialState : function() { getInitialState : function() {
let brewCollection = [];
if(typeof window !== 'undefined') {
brewCollection = this.props.brewCollection.map((brewGroup)=>{
const localVisibility = localStorage.getItem(`${USERPAGE_KEY_PREFIX}-${brewGroup.class}`) ?? 'true';
if(brewGroup.visible != (localVisibility=='true')) {
brewGroup.visible = (localVisibility=='true');
};
return brewGroup;
});
}
return { return {
sortType : 'alpha', sortType : 'alpha',
sortDir : 'asc', sortDir : 'asc',
filterString : this.props.query?.filter || '', filterString : this.props.query?.filter || '',
query : this.props.query query : this.props.query,
brewCollection : brewCollection
}; };
}, },
componentDidMount : function() {
// SAVE TO LOCAL STORAGE WHEN LEAVING PAGE
window.onbeforeunload = this.saveToLocalStorage;
},
componentWillUnmount : function() {
window.onbeforeunload = function(){};
},
saveToLocalStorage : function() {
this.state.brewCollection.map((brewGroup)=>{
localStorage.setItem(`${USERPAGE_KEY_PREFIX}-${brewGroup.class}`, `${brewGroup.visible}`);
});
},
renderBrews : function(brews){ renderBrews : function(brews){
if(!brews || !brews.length) return <div className='noBrews'>No Brews.</div>; if(!brews || !brews.length) return <div className='noBrews'>No Brews.</div>;
@@ -150,11 +180,22 @@ const ListPage = createClass({
return _.orderBy(brews, (brew)=>{ return this.sortBrewOrder(brew); }, this.state.sortDir); return _.orderBy(brews, (brew)=>{ return this.sortBrewOrder(brew); }, this.state.sortDir);
}, },
toggleBrewCollectionState : function(brewGroupClass) {
this.setState((prevState)=>({
brewCollection : prevState.brewCollection.map(
(brewGroup)=>brewGroup.class === brewGroupClass ? { ...brewGroup, visible: !brewGroup.visible } : brewGroup
)
}));
},
renderBrewCollection : function(brewCollection){ renderBrewCollection : function(brewCollection){
if(brewCollection == []) return <div className='brewCollection'>
<h1>No Brews</h1>
</div>;
return _.map(brewCollection, (brewGroup, idx)=>{ return _.map(brewCollection, (brewGroup, idx)=>{
return <div key={idx} className={`brewCollection ${brewGroup.class ?? ''}`}> return <div key={idx} className={`brewCollection ${brewGroup.class ?? ''}`}>
<h1>{brewGroup.title || 'No Title'}</h1> <h1 className={brewGroup.visible ? 'active' : 'inactive'} onClick={()=>{this.toggleBrewCollectionState(brewGroup.class);}}>{brewGroup.title || 'No Title'}</h1>
{this.renderBrews(this.getSortedBrews(brewGroup.brews))} {brewGroup.visible ? this.renderBrews(this.getSortedBrews(brewGroup.brews)) : <></>}
</div>; </div>;
}); });
}, },
@@ -167,7 +208,7 @@ const ListPage = createClass({
<div className='content V3'> <div className='content V3'>
<div className='phb'> <div className='phb'>
{this.renderSortOptions()} {this.renderSortOptions()}
{this.renderBrewCollection(this.props.brewCollection)} {this.renderBrewCollection(this.state.brewCollection)}
</div> </div>
</div> </div>
</div>; </div>;

View File

@@ -74,4 +74,27 @@
} }
} }
} }
h1 {
cursor: pointer;
&.active {
color: #58180D;
}
&.active::before {
content: '\f107';
font-family: 'Font Awesome 5 Free';
font-weight: 900;
font-size: 0.6cm;
padding-right: 0.5em;
}
&.inactive {
color: #707070;
}
&.inactive::before {
content: '\f105';
font-family: 'Font Awesome 5 Free';
font-weight: 900;
font-size: 0.6cm;
padding-right: 0.5em;
}
}
} }

View File

@@ -31,17 +31,19 @@ const UserPage = createClass({
const brewCollection = [ const brewCollection = [
{ {
title : `${usernameWithS} published brews`, title : `${usernameWithS} published brews`,
class : 'published', class : 'published',
brews : brews.published brews : brews.published,
visible : true
} }
]; ];
if(this.props.username == global.account?.username){ if(this.props.username == global.account?.username){
brewCollection.push( brewCollection.push(
{ {
title : `${usernameWithS} unpublished brews`, title : `${usernameWithS} unpublished brews`,
class : 'unpublished', class : 'unpublished',
brews : brews.private brews : brews.private,
visible : true
} }
); );
} }