diff --git a/assets/javascripts/discourse/components/param-input.hbs b/assets/javascripts/discourse/components/param-input.hbs index 6b1553e..5e9448f 100644 --- a/assets/javascripts/discourse/components/param-input.hbs +++ b/assets/javascripts/discourse/components/param-input.hbs @@ -64,6 +64,14 @@ /> {{@info.identifier}} + {{else if (eq this.type "category_id")}} + + {{@info.identifier}} + {{else}} c.id === intVal); - } else if (/\//.test(value)) { - const match = /(.*)\/(.*)/.exec(value); - if (!match) { - return false; - } - const result = Category.findBySlug( - dasherize(match[2]), - dasherize(match[1]) - ); - return !!result; } else { - return !!Category.findBySlug(dasherize(value)); + return false; } case "group_id": const groups = this.site.get("groups"); @@ -163,13 +156,25 @@ export default class ParamInput extends Component { return this.site.get("groups"); } - dasherizeCategoryId(value) { + digitalizeCategoryId(value) { value = String(value || ""); const isPositiveInt = /^\d+$/.test(value); - if (!isPositiveInt && value !== dasherize(value)) { - return dasherize(value); + if (!isPositiveInt) { + if (/\//.test(value)) { + const match = /(.*)\/(.*)/.exec(value); + if (!match) { + value = null; + } else { + value = Category.findBySlug( + dasherize(match[2]), + dasherize(match[1]) + )?.id; + } + } else { + value = Category.findBySlug(dasherize(value))?.id; + } } - return value; + return value?.toString(); } @action @@ -177,12 +182,9 @@ export default class ParamInput extends Component { // handle selectKit inputs as well as traditional inputs const value = input.target ? input.target.value : input; if (value.length) { - this.value = - this.args.info.type === "category_id" - ? this.dasherizeCategoryId(value.toString()) - : value.toString(); + this.value = this.normalizeValue(value.toString()); } else { - this.value = value; + this.value = this.normalizeValue(value); } this.args.updateParams(this.args.info.identifier, this.value); diff --git a/spec/system/param_input_spec.rb b/spec/system/param_input_spec.rb index d93def2..8902aa6 100644 --- a/spec/system/param_input_spec.rb +++ b/spec/system/param_input_spec.rb @@ -63,6 +63,12 @@ RSpec.describe "Param input", type: :system, js: true do ::DiscourseDataExplorer::Parameter .create_from_sql(ALL_PARAMS_SQL) - .each { |param| expect(page).to have_css(".query-params [name=\"#{param.identifier}\"]") } + .each do |param| + if !param.nullable && param.type != :boolean && param.default.nil? + expect(page).to have_css(".query-params .param.invalid [name=\"#{param.identifier}\"]") + else + expect(page).to have_css(".query-params .param.valid [name=\"#{param.identifier}\"]") + end + end end end diff --git a/test/javascripts/components/param-input-test.js b/test/javascripts/components/param-input-test.js new file mode 100644 index 0000000..7203388 --- /dev/null +++ b/test/javascripts/components/param-input-test.js @@ -0,0 +1,42 @@ +import { render } from "@ember/test-helpers"; +import hbs from "htmlbars-inline-precompile"; +import { module, test } from "qunit"; +import { setupRenderingTest } from "discourse/tests/helpers/component-test"; +import selectKit from "discourse/tests/helpers/select-kit-helper"; + +const values = {}; +function updateParams(identifier, value) { + values[identifier] = value; +} + +module("Data Explorer Plugin | Component | param-input", function (hooks) { + setupRenderingTest(hooks); + + test("Renders the categroy_id type correctly", async function (assert) { + this.setProperties({ + info: { + identifier: "category_id", + type: "category_id", + default: null, + nullable: false, + }, + initialValues: {}, + params: {}, + updateParams, + }); + + await render(hbs``); + + const categoryChooser = selectKit(".category-chooser"); + + await categoryChooser.expand(); + await categoryChooser.selectRowByValue(2); + + assert.strictEqual(values.category_id, "2"); + }); +});