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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,55 @@
<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">
<FlatButton
@action={{this.toggleBulkSelect}}
@class="bulk-select"
@icon="tasks"
@title="bookmarks.bulk.toggle"
/>
</th>
{{/if}}
<th class="topic-list-data">

{{#if this.bulkSelectEnabled}}
<span class="bulk-select-topics">
{{~#if this.canDoBulkActions}}
<div class="bulk-select-bookmarks-dropdown">
<span class="bulk-select-bookmark-dropdown__count">
{{i18n
"bookmarks.bulk.selected_count"
count=this.selectedCount
}}
</span>
<BulkSelectBookmarksDropdown
@bulkSelectHelper={{this.bulkSelectHelper}}
/>
</div>

{{/if~}}
<DButton
@action={{this.selectAll}}
class="btn btn-default bulk-select-all"
@label="bookmarks.bulk.select_all"
/>
<DButton
@action={{this.clearAll}}
class="btn btn-default bulk-clear-all"
@label="bookmarks.bulk.clear_all"
/>
</span>
{{else}}
<FlatButton
@action={{this.toggleBulkSelect}}
@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 +68,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
88 changes: 88 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,18 @@ export default Component.extend({
modal: service(),
classNames: ["bookmark-list-wrapper"],

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

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

get selectedCount() {
return this.selected?.length || 0;
},

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

@action
toggleBulkSelect() {
this.bulkSelectHelper?.toggleBulkSelect();
this.rerender();
},

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

@action
clearAll() {
this.bulkSelectHelper.autoAddBookmarksToBulkSelect = false;
document
.querySelectorAll("input.bulk-select:checked")
.forEach((el) => el.click());
},

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

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

_toggleSelection(target, bookmark, isSelectingRange) {
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("input.bulk-select", () => {
const target = e.target;
const bookmarkId = target.dataset.id;
const bookmark = this.content.find(
(item) => item.id.toString() === bookmarkId
);
this._toggleSelection(target, bookmark, this.lastChecked && e.shiftKey);
});
},
});
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
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,63 @@
import { click, visit } from "@ember/test-helpers";
import { test } from "qunit";
import {
acceptance,
count,
exists,
queryAll,
} 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();

test("bulk select - modal", async function (assert) {
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, "")
);

await click("button.bulk-clear-all");

assert.strictEqual(
count("input.bulk-select:checked"),
0,
"Clear all should clear all selection"
);

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

assert.strictEqual(
count("input.bulk-select:checked"),
2,
"Select all should select all topics"
);

await dropdown.expand();
await dropdown.selectRowByValue("delete-bookmarks");

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

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