DEV: Modernise and fix behavior when used with loading-slider (#243)

This commit is contained in:
David Taylor 2023-07-06 16:16:36 +01:00 committed by GitHub
parent 62fe282c75
commit 9087034a4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 37 deletions

View File

@ -1,10 +1,10 @@
{{#if siteSettings.solved_enabled}} {{#if this.siteSettings.solved_enabled}}
<ComboBox <ComboBox
@class="solved-status-filter" @class="solved-status-filter"
@content={{statuses}} @content={{this.statuses}}
@value={{status}} @value={{this.status}}
@valueProperty="value" @valueProperty="value"
@options={{hash caretDownIcon="caret-right" caretUpIcon="caret-down"}} @options={{hash caretDownIcon="caret-right" caretUpIcon="caret-down"}}
@onChange={{(action "changeStatus")}} @onChange={{this.changeStatus}}
/> />
{{/if}} {{/if}}

View File

@ -1,51 +1,58 @@
import I18n from "I18n"; import I18n from "I18n";
import { getOwner } from "discourse-common/lib/get-owner"; import { getOwner } from "discourse-common/lib/get-owner";
import Component from "@glimmer/component";
import { action } from "@ember/object";
import { inject as service } from "@ember/service";
export default { const QUERY_PARAM_VALUES = {
shouldRender(args, component) { solved: "yes",
const router = getOwner(this).lookup("router:main"); unsolved: "no",
all: null,
};
const UX_VALUES = {
yes: "solved",
no: "unsolved",
};
export default class SolvedStatusFilter extends Component {
static shouldRender(args, helper) {
const router = getOwner(this).lookup("service:router");
if ( if (
!component.siteSettings.show_filter_by_solved_status || !helper.siteSettings.show_filter_by_solved_status ||
router.currentPath === "discovery.categories" router.currentRouteName === "discovery.categories"
) { ) {
return false; return false;
} else if (component.siteSettings.allow_solved_on_all_topics) { } else if (helper.siteSettings.allow_solved_on_all_topics) {
return true; return true;
} else { } else {
const controller = getOwner(this).lookup( return args.currentCategory?.enable_accepted_answers;
"controller:navigation/category" }
);
return controller && controller.get("category.enable_accepted_answers");
} }
},
setupComponent(args, component) { @service router;
const statuses = ["all", "solved", "unsolved"].map((status) => { @service siteSettings;
get statuses() {
return ["all", "solved", "unsolved"].map((status) => {
return { return {
name: I18n.t(`solved.topic_status_filter.${status}`), name: I18n.t(`solved.topic_status_filter.${status}`),
value: status, value: status,
}; };
}); });
component.set("statuses", statuses);
const queryStrings = window.location.search;
if (queryStrings.match(/solved=yes/)) {
component.set("status", "solved");
} else if (queryStrings.match(/solved=no/)) {
component.set("status", "unsolved");
} else {
component.set("status", "all");
} }
},
actions: { get status() {
const queryParamValue =
this.router.currentRoute.attributes?.modelParams?.solved;
return UX_VALUES[queryParamValue] || "all";
}
@action
changeStatus(newStatus) { changeStatus(newStatus) {
const router = getOwner(this).lookup("router:main"); this.router.transitionTo({
if (newStatus && newStatus !== "all") { queryParams: { solved: QUERY_PARAM_VALUES[newStatus] },
newStatus = newStatus === "solved" ? "yes" : "no"; });
}
} }
router.transitionTo({ queryParams: { solved: newStatus } });
},
},
};