require('./listPage.less');
const React = require('react');
const createClass = require('create-react-class');
const _ = require('lodash');
const moment = require('moment');
const BrewItem = require('./brewItem/brewItem.jsx');
const ListPage = createClass({
displayName : 'ListPage',
getDefaultProps : function() {
return {
brewCollection : [
{
title : '',
class : '',
brews : []
}
],
navItems : <>>
};
},
getInitialState : function() {
return {
sortType : 'alpha',
sortDir : 'asc',
filterString : ''
};
},
renderBrews : function(brews){
if(!brews || !brews.length) return
No Brews.
;
return _.map(brews, (brew, idx)=>{
return ;
});
},
sortBrewOrder : function(brew){
if(!brew.title){brew.title = 'No Title';}
const mapping = {
'alpha' : _.deburr(brew.title.toLowerCase()),
'created' : moment(brew.createdAt).format(),
'updated' : moment(brew.updatedAt).format(),
'views' : brew.views,
'latest' : moment(brew.lastViewed).format()
};
return mapping[this.state.sortType];
},
handleSortOptionChange : function(event){
this.setState({
sortType : event.target.value
});
},
handleSortDirChange : function(event){
this.setState({
sortDir : `${(this.state.sortDir == 'asc' ? 'desc' : 'asc')}`
});
},
renderSortOption : function(sortTitle, sortValue){
return
| ;
},
handleFilterTextChange : function(e){
this.setState({
filterString : e.target.value
});
return;
},
renderFilterOption : function(){
return
| ;
},
renderSortOptions : function(){
return
Sort by :
|
{this.renderSortOption('Title', 'alpha')}
{this.renderSortOption('Created Date', 'created')}
{this.renderSortOption('Updated Date', 'updated')}
{this.renderSortOption('Views', 'views')}
{/* {this.renderSortOption('Latest', 'latest')} */}
Direction :
|
|
{this.renderFilterOption()}
;
},
getSortedBrews : function(brews){
const testString = _.deburr(this.state.filterString).toLowerCase();
brews = _.filter(brews, (brew)=>{
return (_.deburr(brew.title).toLowerCase().includes(testString)) ||
(_.deburr(brew.description).toLowerCase().includes(testString));
});
return _.orderBy(brews, (brew)=>{ return this.sortBrewOrder(brew); }, this.state.sortDir);
},
renderBrewCollection : function(brewCollection){
return _.map(brewCollection, (brewGroup, idx)=>{
return
{brewGroup.title || 'No Title'}
{this.renderBrews(this.getSortedBrews(brewGroup.brews))}
;
});
},
render : function(){
return
{this.props.navItems}
{this.renderSortOptions()}
{this.renderBrewCollection(this.props.brewCollection)}
;
}
});
module.exports = ListPage;