Files
old-ribica/front-ui/app/components/browsing/byCategory.js

146 lines
5.2 KiB
JavaScript
Raw Normal View History

2015-02-04 06:51:49 +01:00
var React = require('react'),
Router = require('react-router'),
Category = require('../../models/category'),
2015-03-29 23:23:21 +02:00
SubCategory = require('../../models/subCategory'),
Section = require('../../models/section'),
2015-02-04 06:51:49 +01:00
ItemCollection = require('../../models/itemCollection'),
ItemActions = require('../../actions/itemActions.js'),
CategoryActions = require('../../actions/categoryActions.js'),
CategoryStore = require('../../stores/categoryStore'),
2015-02-04 06:51:49 +01:00
ItemStore = require('../../stores/itemStore'),
NavigationStore = require('../../stores/navigationStore'),
ItemList = require('../items/itemList'),
2015-02-15 14:50:31 +01:00
NavigationActions = require('../../actions/navigationActions'),
2015-03-29 23:23:21 +02:00
LinkBanner = require('../linkBanner/linkBanner'),
2015-02-15 14:50:31 +01:00
Globals = require('../../globals');
2015-01-25 13:38:25 +01:00
2015-03-29 16:34:42 +02:00
var FilterCriteriaSelector = require('./filterCriteriaSelector');
var AppliedFiltersList = require('./appliedFiltersList');
2015-01-25 13:38:25 +01:00
var ByCategory = React.createClass({
2015-03-29 16:34:42 +02:00
mixins: [Router.State],
2015-02-15 14:50:31 +01:00
getInitialState: function() {
var category = new Category();
var items = new ItemCollection();
return {
category: category,
items: items,
filter :{},
pagination: {}
};
},
2015-02-12 18:59:49 +01:00
filter: {},
onFCClick: function(fc, fcv) {
var q = {};
q[fc.field_name] = fcv.filter_value;
2015-02-07 18:16:21 +01:00
var section = new Section(this.state.category.get('section'));
var category = this.state.category;
2015-02-15 14:50:31 +01:00
2015-02-12 18:59:49 +01:00
this.filter[fc.field_name] = fcv.filter_value;
NavigationActions.goToCategory(category, section, this.filter, 0, this.state.pagination.limit);
},
2015-02-11 08:28:41 +01:00
removeAppliedFilter: function(name) {
2015-02-12 18:59:49 +01:00
delete this.filter[name];
var section = new Section(this.state.category.get('section'));
var category = this.state.category;
NavigationActions.goToCategory(category, section, this.filter, 0, this.state.pagination.limit);
},
2015-03-29 23:23:21 +02:00
onSCClick: function(sc) {
var subCategory = new SubCategory(sc);
NavigationActions.goToSubCategory(subCategory, 0, Globals.defaultLimit);
event.preventDefault();
},
changePage: function(page) {
var section = new Section(this.state.category.get('section'));
var category = this.state.category;
NavigationActions.goToCategory(category, section, this.filter, parseInt(page) * this.state.pagination.limit, this.state.pagination.limit);
2015-02-11 08:28:41 +01:00
},
2015-02-04 06:51:49 +01:00
render: function() {
var self = this;
2015-02-04 06:51:49 +01:00
return (
2015-02-15 14:50:31 +01:00
<div>
<div className='col-md-2'>
<div className='h4'>{this.state.category.get('name')}</div>
<div>Podkategorije</div>
<ul>
{(this.state.category.get('sub_categories') || []).map(function(sc) {
2015-03-29 23:23:21 +02:00
return (<li> <a onClick={self.onSCClick.bind(self, sc)}>{sc.name}</a></li>)
2015-02-15 14:50:31 +01:00
})}
</ul>
2015-03-29 16:34:42 +02:00
<FilterCriteriaSelector filterCriterias={this.state.category.get('filter_criterias')} onFCClick={this.onFCClick} />
2015-02-15 14:50:31 +01:00
</div>
2015-02-04 06:51:49 +01:00
<div className='col-md-10'>
2015-02-15 14:50:31 +01:00
2015-03-29 12:46:51 +02:00
<LinkBanner locationName="category" locationId={Number(this.getParams().id)} />
<h3> Kategorija - {this.state.category.get('name')}</h3>
2015-02-04 06:51:49 +01:00
Number of items in this category: {this.state.items.length}
2015-02-15 14:50:31 +01:00
<div>
2015-03-29 16:34:42 +02:00
<AppliedFiltersList filters={this.appliedCategoryFiltersArray()} onRemove={self.removeAppliedFilter} />
2015-02-15 14:50:31 +01:00
</div>
2015-02-11 08:28:41 +01:00
2015-02-15 14:50:31 +01:00
<div> total count is : {this.state.items.totalCount}</div>
<ItemList items={this.state.items} paginationEnabled={true} total={this.state.items.totalCount} limit={this.state.pagination.limit} onPageChange={this.changePage} currentOffset={this.state.pagination.offset} />
2015-02-04 06:51:49 +01:00
</div>
</div>
);
},
2015-02-11 08:28:41 +01:00
appliedCategoryFiltersArray: function() {
var filters = [];
2015-02-12 18:59:49 +01:00
for(var key in this.state.filter) {
if(this.state.filter.hasOwnProperty(key) && key !== 'limit' && key !== 'offset') {
2015-02-12 18:59:49 +01:00
filters.push({name: key, value: this.state.filter[key]});
2015-02-11 08:28:41 +01:00
}
}
return filters;
},
2015-02-15 14:50:31 +01:00
update: function() {
2015-02-04 06:51:49 +01:00
var categoryId = this.getParams().id;
2015-02-12 18:59:49 +01:00
2015-02-13 23:44:53 +01:00
this.filter = this.getQuery();
var offset = this.filter.offset || 0;
2015-02-15 14:50:31 +01:00
var limit = this.filter.limit || Globals.DefaultPageSize;
2015-02-12 18:59:49 +01:00
this.setState({
filter: this.filter,
pagination: {
offset: offset,
limit: limit
}
2015-02-12 18:59:49 +01:00
});
ItemActions.loadByCategory(categoryId, offset, limit, this.filter);
CategoryActions.loadCategoryDetails(categoryId);
2015-02-04 06:51:49 +01:00
},
2015-02-15 14:50:31 +01:00
componentWillReceiveProps: function() {
this.update();
},
2015-02-04 06:51:49 +01:00
componentDidMount: function() {
2015-02-15 14:50:31 +01:00
this.update();
2015-02-04 06:51:49 +01:00
ItemStore.addChangeListener(this._onChange);
CategoryStore.addChangeListener(this._onChange);
2015-02-04 06:51:49 +01:00
},
componentWillUnmount: function() {
ItemStore.removeChangeListener(this._onChange);
CategoryStore.removeChangeListener(this._onChange);
2015-02-04 06:51:49 +01:00
},
_onChange: function() {
if(this.isMounted()) {
this.setState({
items: ItemStore.getItemsForCategory(),
category: CategoryStore.getCategoryDetails()
2015-02-04 06:51:49 +01:00
});
}
}
2015-01-25 13:38:25 +01:00
});
module.exports = ByCategory;