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 { ajax } from "discourse/lib/ajax";
import { userPath } from "discourse/lib/url";
import { NotificationLevels } from "discourse/lib/notification-levels";
const flatten = array => [].concat.apply([], array);
@ -178,15 +179,33 @@ export default createWidget("hamburger-menu", {
listCategories() {
const maxCategoriesToDisplay = this.siteSettings
.hamburger_menu_categories_count;
const categoriesList = this.site.get("categoriesByCount");
let categories = categoriesList.slice();
const categoriesByCount = this.site.get("categoriesByCount");
let categories = categoriesByCount.slice();
if (this.currentUser) {
let categoryIds = this.currentUser.get("top_category_ids") || [];
let unreadCategoryIds = [];
let topCategoryIds = this.currentUser.get("top_category_ids") || [];
let i = 0;
const mutedCategoryIds = this.currentUser.get("muted_category_ids") || [];
categories = categories.filter(c => !mutedCategoryIds.includes(c.id));
categoryIds.forEach(id => {
categoriesByCount
.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);
if (category) {
categories = categories.filter(c => c.id !== id);

View File

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