REFACTOR: Prioritize unread categories in hamburger menu

This commit is contained in:
Vinoth Kannan 2018-07-30 20:20:10 +05:30
parent 6566b2f11a
commit c54b5824d4
2 changed files with 44 additions and 12 deletions

View File

@ -3,6 +3,7 @@ import { h } from "virtual-dom";
import DiscourseURL from "discourse/lib/url"; import DiscourseURL from "discourse/lib/url";
import { ajax } from "discourse/lib/ajax"; import { ajax } from "discourse/lib/ajax";
import { userPath } from "discourse/lib/url"; import { userPath } from "discourse/lib/url";
import { NotificationLevels } from "discourse/lib/notification-levels";
const flatten = array => [].concat.apply([], array); const flatten = array => [].concat.apply([], array);
@ -178,15 +179,33 @@ export default createWidget("hamburger-menu", {
listCategories() { listCategories() {
const maxCategoriesToDisplay = this.siteSettings const maxCategoriesToDisplay = this.siteSettings
.hamburger_menu_categories_count; .hamburger_menu_categories_count;
const categoriesList = this.site.get("categoriesByCount"); const categoriesByCount = this.site.get("categoriesByCount");
let categories = categoriesList.slice(); let categories = categoriesByCount.slice();
if (this.currentUser) { if (this.currentUser) {
let categoryIds = this.currentUser.get("top_category_ids") || []; let unreadCategoryIds = [];
let topCategoryIds = this.currentUser.get("top_category_ids") || [];
let i = 0; let i = 0;
const mutedCategoryIds = this.currentUser.get("muted_category_ids") || [];
categories = categories.filter(c => !mutedCategoryIds.includes(c.id)); categoriesByCount
categoryIds.forEach(id => { .sort((a, b) => {
return (
b.get("newTopics") +
b.get("unreadTopics") -
(a.get("newTopics") + a.get("unreadTopics"))
);
})
.forEach(c => {
if (c.get("newTopics") > 0 || c.get("unreadTopics") > 0) {
unreadCategoryIds.push(c.id);
}
});
categories = categories.filter(
c => c.notification_level !== NotificationLevels.MUTED
);
[...unreadCategoryIds, ...topCategoryIds].uniq().forEach(id => {
const category = categories.find(c => c.id === id); const category = categories.find(c => c.id === id);
if (category) { if (category) {
categories = categories.filter(c => c.id !== id); categories = categories.filter(c => c.id !== id);

View File

@ -1,9 +1,11 @@
import { moduleForWidget, widgetTest } from "helpers/widget-test"; import { moduleForWidget, widgetTest } from "helpers/widget-test";
import { NotificationLevels } from "discourse/lib/notification-levels";
moduleForWidget("hamburger-menu"); moduleForWidget("hamburger-menu");
const topCategoryIds = [2, 3, 1]; const topCategoryIds = [2, 3, 1];
let mutedCategoryIds = []; let mutedCategoryIds = [];
let unreadCategoryIds = [];
let categoriesByCount = []; let categoriesByCount = [];
widgetTest("prioritize faq", { widgetTest("prioritize faq", {
@ -155,16 +157,24 @@ widgetTest("top categories", {
beforeEach() { beforeEach() {
this.siteSettings.hamburger_menu_categories_count = 8; this.siteSettings.hamburger_menu_categories_count = 8;
maxCategoriesToDisplay = this.siteSettings.hamburger_menu_categories_count; maxCategoriesToDisplay = this.siteSettings.hamburger_menu_categories_count;
categoriesByCount = this.site.get("categoriesByCount"); categoriesByCount = this.site.get("categoriesByCount").slice();
categoriesByCount.every(c => { categoriesByCount.every(c => {
if (!topCategoryIds.includes(c.id)) { if (!topCategoryIds.includes(c.id)) {
mutedCategoryIds.push(c.id); if (mutedCategoryIds.length === 0) {
return false; mutedCategoryIds.push(c.id);
c.set("notification_level", NotificationLevels.MUTED);
} else if (unreadCategoryIds.length === 0) {
unreadCategoryIds.push(c.id);
c.set("unreadTopics", 5);
} else {
unreadCategoryIds.splice(0, 0, c.id);
c.set("newTopics", 10);
return false;
}
} }
return true; return true;
}); });
this.currentUser.set("top_category_ids", topCategoryIds); this.currentUser.set("top_category_ids", topCategoryIds);
this.currentUser.set("muted_category_ids", mutedCategoryIds);
}, },
test(assert) { test(assert) {
@ -173,8 +183,11 @@ widgetTest("top categories", {
categoriesByCount = categoriesByCount.filter( categoriesByCount = categoriesByCount.filter(
c => !mutedCategoryIds.includes(c.id) c => !mutedCategoryIds.includes(c.id)
); );
let ids = topCategoryIds let ids = [
.concat(categoriesByCount.map(c => c.id)) ...unreadCategoryIds,
...topCategoryIds,
...categoriesByCount.map(c => c.id)
]
.uniq() .uniq()
.slice(0, maxCategoriesToDisplay); .slice(0, maxCategoriesToDisplay);