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: Support subcategory paths in custom route setting #60

Merged
merged 7 commits into from
Aug 12, 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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@ You can control some features for the provided blocks via parameters.
| title | title of the block | varies | string | tag-topics, category-list |
| excludedGroupNames | Excludes specified groups | | Group names | top-contributors |
| order | Orders the contributors | likes_received | String (likes_received or likes_given) | top-contributors |
| period | Time period for top contributors | yearly | all, yearly, quarterly, monthly, weekly, daily | top-contributors |
| period | Time period for top contributors | yearly | all, yearly, quarterly, monthly, weekly, daily | top-contributors |

### Blocks from other plugins

The Discourse Calendar plugin comes with a block called `upcoming-events-list` that you can use in conjunction with this component. You'll want to ensure the desired route is enabled via the `events calendar categories` setting in the Calendar plugin settings. The block params use this [syntax](https://momentjs.com/docs/#/displaying/format/), for example `MMMM D, YYYY`.
Copy link
Contributor

Choose a reason for hiding this comment

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

This is neat to have in the readme. Other plugins/themes contribute blocks that can be used here, I can think of just Gamification off the top of my head, but I'm sure there are a few more. Probably documented on the meta topic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oooh cool! Good to know.

2 changes: 1 addition & 1 deletion about.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license_url": "https://github.com/discourse/discourse-right-sidebar-blocks/blob/main/LICENSE",
"about_url": "https://meta.discourse.org/t/right-sidebar-blocks/231067",
"learn_more": "TODO",
"theme_version": "0.0.1",
"theme_version": "0.0.2",
"minimum_discourse_version": null,
"maximum_discourse_version": null,
"assets": {},
Expand Down
19 changes: 18 additions & 1 deletion javascripts/connectors/before-list-area/tc-right-sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,37 @@ export default {

@discourseComputed(
"router.currentRouteName",
"router.currentRoute.attributes.category",
"router.currentRoute.attributes.category.slug",
"router.currentRoute.attributes.tag.id"
)
showSidebar(currentRouteName, categorySlug, tagId) {
showSidebar(currentRouteName, category, categorySlug, tagId) {
if (this.site.mobileView) {
return false;
}

if (settings.show_in_routes !== "") {
const selectedRoutes = settings.show_in_routes.split("|");
let subcategory = null;
let parentCategory = null;

// check if current page is subcategory
// -- is category
// -- does not have children itself
// -- has a parent category
if (
!!this.category &&
!this.category.has_children &&
!!this.category.parent_category_id
) {
subcategory = categorySlug;
parentCategory = category.ancestors[0].slug;
}

if (
selectedRoutes.includes(currentRouteName) ||
selectedRoutes.includes(`c/${categorySlug}`) ||
selectedRoutes.includes(`c/${parentCategory}/${subcategory}`) ||
selectedRoutes.includes(`tag/${tagId}`)
) {
Comment on lines +25 to 43
Copy link
Contributor

Choose a reason for hiding this comment

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

I might be missing something but it feels like you could do: selectedRoutes.includes(this.category.path) and remove a lot of logic of this codeblock.

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'll give that a shot! No reason, I just wanted to make it as specific as possible just in case.

return true;
Expand Down
33 changes: 32 additions & 1 deletion test/acceptance/right-sidebar-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,42 @@ acceptance("Right Sidebar - custom routes", function (needs) {

needs.hooks.beforeEach(() => {
settings.show_in_routes =
"discovery.categories|discovery.top|c/bug|tag/important";
"discovery.categories|discovery.top|c/bug|c/bug/foo|tag/important";
});

needs.hooks.afterEach(() => {
settings.blocks = "[]";
});

needs.site({
categories: [
{
id: 1,
name: "bug",
slug: "bug",
},
{
id: 2,
name: "foo",
slug: "foo",
parent_category_id: 1,
},
],
});

needs.pretender((server, helper) => {
server.get(`/c/bug/1/l/latest.json`, () => {
return helper.response(
cloneJSON(discoveryFixture["/c/bug/1/l/latest.json"])
);
});

server.get(`/c/bug/foo/2/l/latest.json`, () => {
return helper.response(
cloneJSON(discoveryFixture["/c/bug/1/l/latest.json"])
);
});

server.get(`/tag/important/l/latest.json`, () => {
return helper.response(
cloneJSON(discoveryFixture["/tag/important/l/latest.json"])
Expand Down Expand Up @@ -81,6 +103,15 @@ acceptance("Right Sidebar - custom routes", function (needs) {
);
});

test("Viewing the foo subcategory", async function (assert) {
await visit("/c/bug/foo/2");

assert.ok(
visible(".tc-right-sidebar"),
"sidebar present under the foo subcategory"
);
});

test("Viewing the important tag", async function (assert) {
await visit("/tag/important");

Expand Down