recent_topics: Add persistence for filters via localstorage.

Previously the filter would be reset every time the page was
refreshed. This commit adds persistence via localstorage, the tests
follow the pattern used in tests for drafts.

Fixes: #15676.
This commit is contained in:
YashRE42
2020-09-20 23:43:18 +00:00
committed by Tim Abbott
parent 7fc28eaa16
commit 2cd234f1b7
5 changed files with 42 additions and 4 deletions

View File

@@ -14,17 +14,19 @@ set_global("location", {
set_global("to_$", () => window_stub); set_global("to_$", () => window_stub);
const people = zrequire("people"); const people = zrequire("people");
zrequire("localstorage");
const hash_util = zrequire("hash_util"); const hash_util = zrequire("hash_util");
const hashchange = zrequire("hashchange"); const hashchange = zrequire("hashchange");
const stream_data = zrequire("stream_data"); const stream_data = zrequire("stream_data");
zrequire("navigate"); zrequire("navigate");
zrequire("recent_topics");
const recent_topics = zrequire("recent_topics");
recent_topics.show = () => {}; recent_topics.show = () => {};
recent_topics.is_visible = () => false;
set_global("search", { set_global("search", {
update_button_visibility: () => {}, update_button_visibility: () => {},
}); });
set_global((recent_topics.is_visible = () => false));
set_global("document", "document-stub"); set_global("document", "document-stub");
const history = set_global("history", {}); const history = set_global("history", {});

View File

@@ -9,6 +9,7 @@ const $ = require("../zjsunit/zjquery");
zrequire("message_util"); zrequire("message_util");
zrequire("narrow_state"); zrequire("narrow_state");
zrequire("localstorage");
const noop = () => {}; const noop = () => {};
set_global("top_left_corner", { set_global("top_left_corner", {
@@ -87,6 +88,22 @@ set_global("drafts", {
update_draft: noop, update_draft: noop,
}); });
const ls_container = new Map();
set_global("localStorage", {
getItem(key) {
return ls_container.get(key);
},
setItem(key, val) {
ls_container.set(key, val);
},
removeItem(key) {
ls_container.delete(key);
},
clear() {
ls_container.clear();
},
});
// Custom Data // Custom Data
// New stream // New stream

View File

@@ -113,9 +113,10 @@ const messages = {
// This is an example of a deep unit test, where our dependencies // This is an example of a deep unit test, where our dependencies
// are easy to test. Start by requiring the dependencies: // are easy to test. Start by requiring the dependencies:
zrequire("recent_senders"); zrequire("recent_senders");
zrequire("localstorage");
const unread = zrequire("unread"); const unread = zrequire("unread");
const stream_topic_history = zrequire("stream_topic_history"); const stream_topic_history = zrequire("stream_topic_history");
zrequire("recent_topics"); const recent_topics = zrequire("recent_topics");
// And finally require the module that we will test directly: // And finally require the module that we will test directly:
const message_store = zrequire("message_store"); const message_store = zrequire("message_store");

View File

@@ -105,6 +105,8 @@ rewiremock.proxy(() => zrequire("notifications"), {
"../../static/js/favicon": {}, "../../static/js/favicon": {},
}); });
zrequire("pm_list"); zrequire("pm_list");
zrequire("list_cursor");
zrequire("localstorage");
zrequire("keydown_util"); zrequire("keydown_util");
zrequire("stream_list"); zrequire("stream_list");
zrequire("topic_list"); zrequire("topic_list");

View File

@@ -4,6 +4,7 @@ const render_recent_topic_row = require("../templates/recent_topic_row.hbs");
const render_recent_topics_filters = require("../templates/recent_topics_filters.hbs"); const render_recent_topics_filters = require("../templates/recent_topics_filters.hbs");
const render_recent_topics_body = require("../templates/recent_topics_table.hbs"); const render_recent_topics_body = require("../templates/recent_topics_table.hbs");
const {localstorage} = require("./localstorage");
const people = require("./people"); const people = require("./people");
const topics = new Map(); // Key is stream-id:topic. const topics = new Map(); // Key is stream-id:topic.
@@ -11,7 +12,6 @@ let topics_widget;
// Sets the number of avatars to display. // Sets the number of avatars to display.
// Rest of the avatars, if present, are displayed as {+x} // Rest of the avatars, if present, are displayed as {+x}
const MAX_AVATAR = 4; const MAX_AVATAR = 4;
let filters = new Set();
// Use this to set the focused element. // Use this to set the focused element.
// //
@@ -35,6 +35,19 @@ let col_focus = 1;
// actions that only appear in some rows. // actions that only appear in some rows.
const MAX_SELECTABLE_COLS = 4; const MAX_SELECTABLE_COLS = 4;
// we use localstorage to persist the recent topic filters
const ls_key = "recent_topic_filters";
const ls = localstorage();
let filters = new Set();
exports.save_filters = function () {
ls.set(ls_key, Array.from(filters));
};
exports.load_filters = function () {
filters = new Set(ls.get(ls_key));
};
function set_default_focus() { function set_default_focus() {
// If at any point we are confused about the currently // If at any point we are confused about the currently
// focused element, we switch focus to search. // focused element, we switch focus to search.
@@ -338,11 +351,14 @@ exports.set_filter = function (filter) {
} else { } else {
filters.add(filter); filters.add(filter);
} }
exports.save_filters();
}; };
function show_selected_filters() { function show_selected_filters() {
// Add `btn-selected-filter` to the buttons to show // Add `btn-selected-filter` to the buttons to show
// which filters are applied. // which filters are applied.
exports.load_filters();
if (filters.size === 0) { if (filters.size === 0) {
$("#recent_topics_filter_buttons") $("#recent_topics_filter_buttons")
.find('[data-filter="all"]') .find('[data-filter="all"]')