Skip to content
This repository was archived by the owner on May 16, 2019. It is now read-only.

Commit ba7ab58

Browse files
authored
Merge pull request #1813 from OpenBazaar/fixStoreCategories
Fixes issues with store categories.
2 parents 5d2abe9 + 6de6b12 commit ba7ab58

File tree

2 files changed

+45
-40
lines changed

2 files changed

+45
-40
lines changed

js/views/itemListVw.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,16 @@ module.exports = baseVw.extend({
3838

3939
render: function(){
4040
var self = this;
41-
//clear the list
41+
// clear the list
4242
this.$el.empty();
43-
if (this.itemsShort.models.length > 0) {
44-
this.renderItemSet(this.nextToShow, this.showPerScroll);
43+
// create list based on category each time this is rendered
44+
if(this.category !== 'all') {
45+
this.categorizedItems = this.itemsShort.where({category: this.category})
46+
} else {
47+
this.categorizedItems = this.itemsShort.models;
48+
}
49+
if (this.categorizedItems.length > 0) {
50+
this.renderItemSet(0, this.showPerScroll);
4551
} else {
4652
self.renderNoneFound();
4753
}
@@ -51,16 +57,16 @@ module.exports = baseVw.extend({
5157
renderItemSet: function(start, end){
5258
let renderSet = [];
5359

54-
if (start >= this.itemsShort.models.length) return;
60+
if (start >= this.categorizedItems.length) return;
5561

56-
renderSet = __.filter(this.itemsShort.models, function(value, index){
62+
renderSet = __.filter(this.categorizedItems, function(value, index){
5763
return (index >= start) && (index < end);
5864
});
5965

6066
__.each(renderSet, (item) => {
6167
if (item.toJSON().category == this.category || this.category == "all") {
6268
this.renderContract(item);
63-
}
69+
}
6470
}, this);
6571

6672
this.nextToShow = end;

js/views/userPageVw.js

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ UserPageVw = pageVw.extend({
395395
self.setCustomStyles();
396396
self.setState(self.state, self.currentItemHash, { replaceHistory: true });
397397
self.$backToTop = self.$('.backToTop');
398+
self.$categorySelect = self.$('.js-categories');
398399

399400
//check if user is blocked
400401
if (!self.options.ownPage && isBlocked) {
@@ -598,16 +599,24 @@ UserPageVw = pageVw.extend({
598599
}
599600
},
600601

601-
setCategory: function(category) {
602-
var $select;
602+
setCategorySelect: function(category) {
603+
if (category && this.$categorySelect.val() !== category &&
604+
this.$categorySelect.find('option[value="' + category + '"]').length) {
605+
this.$categorySelect.val(category);
606+
}
607+
this.showCategory(category);
608+
},
603609

604-
if (category) {
605-
$select = this.$el.find('.js-categories');
610+
categoryChanged: function() {
611+
this.showCategory(this.$categorySelect.val());
612+
},
606613

607-
if ($select.val() !== category && $select.find('option[value="' + category + '"]').length) {
608-
$select.val(category);
609-
this.categoryChanged();
610-
}
614+
showCategory: function(category) {
615+
if (this.itemList) {
616+
this.itemList.category = category;
617+
this.itemList.render();
618+
} else {
619+
this.renderItems(this.cachedListings);
611620
}
612621
},
613622

@@ -627,10 +636,6 @@ UserPageVw = pageVw.extend({
627636
}
628637
},
629638

630-
categoryChanged: function() {
631-
this.renderItems(this.listings.get('listings'));
632-
},
633-
634639
toggleFollowButtons: function(followed) {
635640
var followBtn = this.$('.js-follow'),
636641
unfollowBtn = this.$('.js-unfollow');
@@ -847,7 +852,6 @@ UserPageVw = pageVw.extend({
847852

848853
renderItems: function (model, skipNSFWmodal) {
849854
var self = this,
850-
select = this.$el.find('.js-categories'),
851855
selectOptions = [],
852856
addressCountries = self.options.userModel.get('shipping_addresses').map(function(address){
853857
return address.country;
@@ -857,38 +861,38 @@ UserPageVw = pageVw.extend({
857861
addressCountries.push(userCountry);
858862
skipNSFWmodal = skipNSFWmodal || this.skipNSFWmodal;
859863
model = model || [];
860-
__.each(model, function (arrayItem) {
864+
__.each(model, (arrayItem) => {
861865

862-
if (!self.showNSFWContent && !self.showNSFW &&!skipNSFWmodal && arrayItem.nsfw){
866+
if (!this.showNSFWContent && !this.showNSFW &&!skipNSFWmodal && arrayItem.nsfw){
863867
arrayItem.cloak = true;
864868
} else {
865869
arrayItem.cloak = false;
866870
}
867-
arrayItem.userCurrencyCode = self.options.userModel.get('currency_code');
868-
arrayItem.serverUrl = self.options.userModel.get('serverUrl');
871+
arrayItem.userCurrencyCode = this.options.userModel.get('currency_code');
872+
arrayItem.serverUrl = this.options.userModel.get('serverUrl');
869873
arrayItem.showAvatar = false;
870-
arrayItem.avatar_hash = self.model.get('page').profile.avatar_hash;
871-
arrayItem.handle = self.model.get('page').profile.handle;
872-
arrayItem.userID = self.pageID;
873-
arrayItem.ownPage = self.options.ownPage;
874+
arrayItem.avatar_hash = this.model.get('page').profile.avatar_hash;
875+
arrayItem.handle = this.model.get('page').profile.handle;
876+
arrayItem.userID = this.pageID;
877+
arrayItem.ownPage = this.options.ownPage;
874878
arrayItem.onUserPage = true;
875879
arrayItem.userCountries = addressCountries;
876880
arrayItem.skipNSFWmodal = skipNSFWmodal;
877-
if (arrayItem.category != "" && self.$el.find('.js-categories option[value="' + arrayItem.category + '"]').length == 0){
881+
if (arrayItem.category != "" && this.$el.find('.js-categories option[value="' + arrayItem.category + '"]').length == 0){
878882
selectOptions[arrayItem.category] = true;
879883
}
880-
if (self.options.ownPage === true){
881-
arrayItem.imageURL = self.options.userModel.get('serverUrl')+"get_image?hash="+arrayItem.thumbnail_hash;
884+
if (this.options.ownPage === true){
885+
arrayItem.imageURL = this.options.userModel.get('serverUrl')+"get_image?hash="+arrayItem.thumbnail_hash;
882886
} else {
883-
arrayItem.imageURL = self.options.userModel.get('serverUrl')+"get_image?hash="+arrayItem.thumbnail_hash+"&guid="+self.pageID;
887+
arrayItem.imageURL = this.options.userModel.get('serverUrl')+"get_image?hash="+arrayItem.thumbnail_hash+"&guid="+this.pageID;
884888
}
885889
});
886890

887-
Object.keys(selectOptions).sort().forEach(function(selectOption) {
891+
Object.keys(selectOptions).sort().forEach((selectOption) => {
888892
var opt = document.createElement('option');
889893
opt.value = selectOption;
890894
opt.innerHTML = selectOption;
891-
select.append(opt);
895+
this.$categorySelect.append(opt);
892896
});
893897

894898
this.itemList = new itemListView({
@@ -897,7 +901,7 @@ UserPageVw = pageVw.extend({
897901
title: window.polyglot.t('NoListings'),
898902
message: "",
899903
userModel: this.options.userModel,
900-
category: this.$el.find('.js-categories').val()
904+
category: this.$categorySelect.val(),
901905
});
902906

903907
this.registerChild(this.itemList);
@@ -1148,16 +1152,11 @@ UserPageVw = pageVw.extend({
11481152
},
11491153

11501154
storeTabClick: function(e) {
1151-
if (this.$el.find('.js-categories').val() != "all"){
1152-
$(".js-categories option[value='all']").attr("selected", "selected");
1153-
this.categoryChanged();
1154-
}
1155-
11561155
this.storeClick(e);
11571156
},
11581157

11591158
storeCatClick: function(e) {
1160-
this.setCategory($(e.target).text());
1159+
this.setCategorySelect($(e.target).text());
11611160
this.storeClick(e);
11621161
},
11631162

0 commit comments

Comments
 (0)