Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: Add bulk action to bookmark #26856

Merged
merged 6 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,43 @@
<thead class="topic-list-header">
{{#if this.site.desktopView}}
<PluginOutlet @name="bookmark-list-table-header">
<th class="topic-list-data">{{i18n "topic.title"}}</th>
pmusaraj marked this conversation as resolved.
Show resolved Hide resolved

{{#if this.bulkSelectEnabled}}
<th class="bulk-select topic-list-data">
{{raw
"flat-button"
class="bulk-select"
icon="list"
title="bookmarks.bulk.toggle"
}}
</th>
{{/if}}
<th class="topic-list-data">
{{#if this.bulkSelectEnabled}}
<span class="bulk-select-topics">
{{~#if this.canDoBulkActions}}
{{raw
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you try to use the Glimmer component directly here? For context, raw handlebars components are legacy elements, we are rtying to move away from them. (There's no way for you to have known this before...)

I suspect this may also help with the dialog test issue.

"bookmark-bulk-select-dropdown"
bulkSelectHelper=this.bulkSelectHelper
}}
{{/if~}}
<button class="btn btn-default bulk-select-all">{{i18n
pmusaraj marked this conversation as resolved.
Show resolved Hide resolved
"bookmarks.bulk.select_all"
}}</button>
<button class="btn btn-default bulk-clear-all">{{i18n
"bookmarks.bulk.clear_all"
}}</button>
</span>
{{else}}
{{raw
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here, het's try to avoid any raw components.

"flat-button"
class="bulk-select"
icon="tasks"
title="bookmarks.bulk.toggle"
}}
{{i18n "topic.title"}}
{{/if~}}
</th>
<th class="topic-list-data">&nbsp;</th>
<th class="post-metadata topic-list-data">{{i18n
"post.bookmarks.updated"
Expand All @@ -20,6 +56,18 @@
<tbody class="topic-list-body">
{{#each this.content as |bookmark|}}
<tr class="topic-list-item bookmark-list-item">
{{#if this.bulkSelectEnabled}}
pmusaraj marked this conversation as resolved.
Show resolved Hide resolved
<td class="bulk-select bookmark-list-data">
<label for="bulk-select-{{bookmark.id}}">
<input
type="checkbox"
class="bulk-select"
id="bulk-select-{{bookmark.id}}"
data-id={{bookmark.id}}
/>
</label>
</td>
{{/if}}
<th scope="row" class="main-link topic-list-data">
<span class="link-top-line">
<div class="bookmark-metadata">
Expand Down
81 changes: 81 additions & 0 deletions app/assets/javascripts/discourse/app/components/bookmark-list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Component from "@ember/component";
import { action } from "@ember/object";
import { dependentKeyCompat } from "@ember/object/compat";
import { service } from "@ember/service";
import { Promise } from "rsvp";
import BookmarkModal from "discourse/components/modal/bookmark";
Expand All @@ -16,6 +17,14 @@ export default Component.extend({
modal: service(),
classNames: ["bookmark-list-wrapper"],

get canDoBulkActions() {
return this.bulkSelectHelper?.selected.length;
},

get selected() {
return this.bulkSelectHelper?.selected;
},

@action
removeBookmark(bookmark) {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -90,7 +99,79 @@ export default Component.extend({
bookmark.togglePin().then(this.reload);
},

@dependentKeyCompat // for the classNameBindings
get bulkSelectEnabled() {
return this.bulkSelectHelper?.bulkSelectEnabled;
},

_removeBookmarkFromList(bookmark) {
this.content.removeObject(bookmark);
},

_toggleBookmark(target, bookmark, isSelectingRange) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should be named _toggleSelection, because it doesn't change the bookmark status, it just changes the selected checkboxes.

const selected = this.selected;

if (target.checked) {
selected.addObject(bookmark);

if (isSelectingRange) {
const bulkSelects = Array.from(
document.querySelectorAll("input.bulk-select")
),
from = bulkSelects.indexOf(target),
to = bulkSelects.findIndex((el) => el.id === this.lastChecked.id),
start = Math.min(from, to),
end = Math.max(from, to);

bulkSelects
.slice(start, end)
.filter((el) => el.checked !== true)
.forEach((checkbox) => {
checkbox.click();
});
}
this.set("lastChecked", target);
} else {
selected.removeObject(bookmark);
this.set("lastChecked", null);
}
},

click(e) {
const onClick = (sel, callback) => {
let target = e.target.closest(sel);

if (target) {
callback(target);
}
};

onClick("button.bulk-select", () => {
this.bulkSelectHelper?.toggleBulkSelect();
this.rerender();
});

onClick("input.bulk-select", () => {
const target = e.target;
const bookmarkId = target.dataset.id;
const bookmark = this.content.find(
(item) => item.id.toString() === bookmarkId
);
this._toggleBookmark(target, bookmark, this.lastChecked && e.shiftKey);
});

onClick("button.bulk-select-all", () => {
this.bulkSelectHelper.autoAddBookmarksToBulkSelect = true;
document
.querySelectorAll("input.bulk-select:not(:checked)")
.forEach((el) => el.click());
});

onClick("button.bulk-clear-all", () => {
this.bulkSelectHelper.autoAddBookmarksToBulkSelect = false;
document
.querySelectorAll("input.bulk-select:checked")
.forEach((el) => el.click());
});
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { service } from "@ember/service";
import { htmlSafe } from "@ember/template";
import { Promise } from "rsvp";
import { ajax } from "discourse/lib/ajax";
import BulkSelectHelper from "discourse/lib/bulk-select-helper";
import Bookmark from "discourse/models/bookmark";
import { iconHTML } from "discourse-common/lib/icon-library";
import discourseComputed from "discourse-common/utils/decorators";
Expand All @@ -23,6 +24,13 @@ export default Controller.extend({
inSearchMode: notEmpty("q"),
noContent: equal("model.bookmarks.length", 0),

bulkSelectHelper: null,

init() {
this._super(...arguments);
this.bulkSelectHelper = new BulkSelectHelper(this);
pmusaraj marked this conversation as resolved.
Show resolved Hide resolved
},

searchTerm: computed("q", {
get() {
return this.q;
Expand Down Expand Up @@ -77,6 +85,11 @@ export default Controller.extend({
.finally(() => this.set("loadingMore", false));
},

@action
updateAutoAddBookmarksToBulkSelect(value) {
this.bulkSelectHelper.autoAddBookmarksToBulkSelect = value;
},

_loadMoreBookmarks(searchQuery) {
if (!this.model.loadMoreUrl) {
return Promise.resolve();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default class BulkSelectHelper {

@tracked bulkSelectEnabled = false;
@tracked autoAddTopicsToBulkSelect = false;
@tracked autoAddBookmarksToBulkSelect = false;

selected = new TrackedArray();

Expand Down
12 changes: 12 additions & 0 deletions app/assets/javascripts/discourse/app/models/bookmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ export default class Bookmark extends RestModel {
});
}

static bulkOperation(bookmarks, operation) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a method to support bulk operations on bookmarks.

const data = {
bookmark_ids: bookmarks.mapBy("id"),
operation,
};

return ajax("/bookmarks/bulk", {
type: "PUT",
data,
});
}

static async applyTransformations(bookmarks) {
await applyModelTransformations("bookmark", bookmarks);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{{view.html}}}
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be dropped as well?

Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<button class='btn-transparent {{class}}' title='{{i18n "topics.bulk.toggle"}}'>
<button class='btn-transparent {{class}}' title='{{i18n title}}'>
{{d-icon icon}}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was just a minor refactoring to get the title dynamically instead of always getting the hardcoded for topics.

Copy link
Contributor

Choose a reason for hiding this comment

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

Probably don't need to include this change either now that bookmarks don't use the raw button.

</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import EmberObject from "@ember/object";
import rawRenderGlimmer from "discourse/lib/raw-render-glimmer";
import i18n from "discourse-common/helpers/i18n";
import BulkSelectBookmarksDropdown from "select-kit/components/bulk-select-bookmarks-dropdown";

export default class extends EmberObject {
pmusaraj marked this conversation as resolved.
Show resolved Hide resolved
get selectedCount() {
return this.bulkSelectHelper.selected.length;
}

get html() {
return rawRenderGlimmer(
this,
"div.bulk-select-bookmarks-dropdown",
<template>
<span class="bulk-select-bookmark-dropdown__count">
{{i18n "bookmarks.bulk.selected_count" count=@data.selectedCount}}
</span>
<BulkSelectBookmarksDropdown
@bulkSelectHelper={{@data.bulkSelectHelper}}
/>
</template>,
{
bulkSelectHelper: this.bulkSelectHelper,
selectedCount: this.selectedCount,
}
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<div class="alert alert-info">{{i18n "user.no_bookmarks_search"}}</div>
{{else}}
<BookmarkList
@bulkSelectHelper={{this.bulkSelectHelper}}
@loadMore={{action "loadMore"}}
@reload={{action "reload"}}
@loadingMore={{this.loadingMore}}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { click, visit } from "@ember/test-helpers";
import { test } from "qunit";
import {
acceptance,
exists,
queryAll,
updateCurrentUser,
} from "discourse/tests/helpers/qunit-helpers";
import selectKit from "discourse/tests/helpers/select-kit-helper";
import I18n from "discourse-i18n";

acceptance("Bookmark - Bulk Actions", function (needs) {
needs.user();
needs.pretender((server, helper) => {
server.put("/bookmarks/bulk", () => {
return helper.response({
bookmark_ids: [],
});
});
});

test("bulk select - options", async function (assert) {
updateCurrentUser({ moderator: true });
Copy link
Contributor

Choose a reason for hiding this comment

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

Very minor, but you likely don't ned to set moderator to true here. Regular users should be able to update their own bookmarks.

await visit("/u/eviltrout/activity/bookmarks");
assert.ok(exists("button.bulk-select"));

await click("button.bulk-select");

await click(queryAll("input.bulk-select")[0]);
await click(queryAll("input.bulk-select")[1]);

const dropdown = selectKit(".select-kit.bulk-select-bookmarks-dropdown");
await dropdown.expand();

const options = dropdown.displayedContent();

assert.strictEqual(
options[0].name,
I18n.t("js.bookmark_bulk_actions.clear_reminders.name"),
"it shows an option to clear reminders"
);

assert.strictEqual(
options[1].name,
I18n.t("js.bookmark_bulk_actions.delete_bookmarks.name"),
"it shows an option to delete bookmarks"
);
pmusaraj marked this conversation as resolved.
Show resolved Hide resolved
});

test("bulk select - clear reminders", async function (assert) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the test I mentioned before; it is currently flakey because of the dialog service.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, this is tricky. I am not sure it ahs to do with the dialog service, that service should be available at all times. I suspect it has to do with raw handlebars and the fact that here two tests are loading the same endpoint.

Once we move away from handlebars, this should clear. If it doesn't, we can sidestep tihs issue by having just one test that covers both the deleting and the reminder clearing checks.

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've updated the templates, but the test was still flakey, so I combined all the tests into one, as you suggested.

updateCurrentUser({ moderator: true });

await visit("/u/eviltrout/activity/bookmarks");
assert.ok(exists("button.bulk-select"));

await click("button.bulk-select");

await click(queryAll("input.bulk-select")[0]);
await click(queryAll("input.bulk-select")[1]);

const dropdown = selectKit(".select-kit.bulk-select-bookmarks-dropdown");
await dropdown.expand();

await dropdown.selectRowByValue("clear-reminders");

assert.ok(exists(".dialog-container"), "it should show the modal");

assert.dom(".dialog-container .dialog-body").includesText(
I18n.t("js.bookmark_bulk_actions.clear_reminders.description", {
count: 2,
}).replaceAll(/\<.*?>/g, "")
);
});
});