Skip to content
This repository was archived by the owner on Jul 22, 2020. It is now read-only.

Comments

Save filters usage history to local storage and provide a dropdown me…#158

Merged
prymitive merged 8 commits intomasterfrom
history
Aug 14, 2017
Merged

Save filters usage history to local storage and provide a dropdown me…#158
prymitive merged 8 commits intomasterfrom
history

Conversation

@prymitive
Copy link
Contributor

@prymitive prymitive commented Aug 6, 2017

…nu to access it

This allows to quickly select recently used filters from a dropdown. It also shows default (configured by unsee admin) filter and the saved one (saved by the user to a cookie)

screen shot 2017-08-13 at 10 37 46

.tt-cursor,
.tt-suggestion:hover,
.tt-suggestion:focus {
div.tt-cursor,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR seems to contain changes from other unapproved PRs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like I've based it on top of other branch, rather than the master

}

function renderHistory() {
const storage = window.localStorage;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to pass localStorage here, that way you can use your mock without having to override window.localStorage?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is what I would call "a good idea" sir

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, I could, but then I need to pass it through all the layers and mock it eventually anyway, so it doesn't really help much here IMHO, how strongly do you feel about it?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's an architectured dependency of this code, I'd really prefer if it could be provided, rather than assumed.


})();

Object.defineProperty(window, "localStorage", {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look like this mock is working correctly, it seems to be undefined in tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it needs to be required. Added some ugly tests around history


function appendFilterToHistory(text) {
// require non empty text and enabled appends
if (!text || !appendsEnabled) return false;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this makes it impossible to clear the text stored in the storage engine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no clear method

// final filter list we'll save to storage
var filterList = [ text ];

// get current history list from storage and append it to our final list

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should consider using a Set here then.

// truncate the history to up to 11 elements
filterList = filterList.slice(0, 11);

storage.setItem(historyKey, filterList.join("\n"));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll have to marshal it back here. You might be interested in wrapping the storage engine to deal with marshalling tasks.

$(selectors.historyMenu).on("click", "a.history-menu-item", function(event) {
var elem = $(event.target).parents("li.history-menu");
const historyArr = elem.find(".rawFilter").text().trim().split(",");
// we need to add filters one by one, this would reload alerts on every

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth creating some sort of "unsee.transaction" then?

$(selectors.historyMenu).on("click", "a.history-menu-item", unsee.transaction(function(event) {
  // append work here
}));


var elems = $("#historyMenu").find(".rawFilter");
// foo is active so should only get default
expect(elems.length).toBe(1);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be easier to use Jest's snapshot feature here (and again it the test below)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to use snapshots before, but didn't realized how easy it is, now I know

…nu to access it

This allows to quickly select recently used filters from a dropdown. It also shows default (configured by unsee admin) filter and the saved one (saved by the user to a cookie)
Instead of assuming that window.localStorage is always used pass it via arguments
We only want unique values, Set is better for this
Show search icon for search history elements and provide a separate entries for the default and saved filter.
Default & saved filter are always rendered on the bottom, if they are non-empty.
@prymitive prymitive merged commit 070a95d into master Aug 14, 2017
@prymitive prymitive deleted the history branch August 14, 2017 18:44
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants