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

DEV: Use Category's async method in param-input #310

Merged
merged 7 commits into from
Aug 21, 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
109 changes: 74 additions & 35 deletions assets/javascripts/discourse/components/param-input-form.gjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Component from "@glimmer/component";
import { action } from "@ember/object";
import EmberObject, { action } from "@ember/object";
import { inject as service } from "@ember/service";
import { dasherize } from "@ember/string";
import { isEmpty } from "@ember/utils";
import ConditionalLoadingSpinner from "discourse/components/conditional-loading-spinner";
import Form from "discourse/components/form";
import Category from "discourse/models/category";
import I18n from "I18n";
Expand Down Expand Up @@ -49,22 +50,20 @@ const ERRORS = {
function digitalizeCategoryId(value) {
value = String(value || "");
const isPositiveInt = /^\d+$/.test(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;
}
if (!isPositiveInt && value.trim()) {
return Category.asyncFindBySlugPath(dasherize(value))
.then((res) => res.id)
.catch((err) => {
if (err.jqXHR?.status === 404) {
throw new ParamValidationError(
`${ERRORS.NO_SUCH_CATEGORY}: ${value}`
);
} else {
throw new Error(err.errorThrow || err.message);
}
});
}
return value?.toString();
return value;
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah I like this better. Throw if invalid and the value stays the same 👍

}

function normalizeValue(info, value) {
Expand Down Expand Up @@ -158,36 +157,72 @@ export default class ParamInputForm extends Component {
infoOf = {};
form = null;

promiseNormalizations = [];

constructor() {
super(...arguments);
this.initializeParams();

const initialValues = this.args.initialValues;
for (const info of this.args.paramInfo) {
this.args.onRegisterApi?.({
submit: this.submit,
allNormalized: Promise.allSettled(this.promiseNormalizations),
});
}

initializeParams() {
this.args.paramInfo.forEach((info) => {
const identifier = info.identifier;
const pinfo = this.createParamInfo(info);

this.paramInfo.push(pinfo);
this.infoOf[identifier] = info;

const normalized = this.getNormalizedValue(info);

// access parsed params if present to update values to previously ran values
let initialValue;
if (initialValues && identifier in initialValues) {
initialValue = initialValues[identifier];
if (normalized instanceof Promise) {
this.handlePromiseNormalization(normalized, pinfo);
} else {
// if no parsed params then get and set default values
initialValue = info.default;
this.data[identifier] = normalized;
}
this.data[identifier] = normalizeValue(info, initialValue);
this.paramInfo.push({
...info,
validation: validationOf(info),
validate: this.validatorOf(info),
component: componentOf(info),
});
this.infoOf[identifier] = info;
}
});
}

this.args.onRegisterApi?.({
submit: this.submit,
createParamInfo(info) {
return EmberObject.create({
...info,
validation: validationOf(info),
validate: this.validatorOf(info),
component: componentOf(info),
});
}

getNormalizedValue(info) {
const initialValues = this.args.initialValues;
const identifier = info.identifier;
return normalizeValue(
info,
initialValues && identifier in initialValues
? initialValues[identifier]
: info.default
);
}

handlePromiseNormalization(promise, pinfo) {
this.promiseNormalizations.push(promise);
pinfo.set("loading", true);
this.data[pinfo.identifier] = null;

promise
.then((res) => this.form.set(pinfo.identifier, res))
.catch((err) =>
this.form.addError(pinfo.identifier, {
title: pinfo.identifier,
message: err.message,
})
)
.finally(() => pinfo.set("loading", false));
}

getErrorFn(info) {
const isPositiveInt = (value) => /^\d+$/.test(value);
const VALIDATORS = {
Expand Down Expand Up @@ -321,6 +356,10 @@ export default class ParamInputForm extends Component {
as |field|
>
<info.component @field={{field}} @info={{info}} />
<ConditionalLoadingSpinner
@condition={{info.loading}}
@size="small"
/>
</form.Field>
</div>
{{/each}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import { action } from "@ember/object";
import CategoryChooser from "select-kit/components/category-chooser";

export default class GroupListInput extends Component {
@tracked value;
constructor() {
super(...arguments);
this.value = this.args.field.value;
}

@action
update(id) {
this.value = id;
this.args.field.set(id);
export default class CategoryIdInput extends Component {
// CategoryChooser will try to modify the value of value,
// triggering a setting-on-hash error. So we have to do the dirty work.
get data() {
return {
value: this.args.field.value,
};
}

<template>
<@field.Custom id={{@field.id}}>
<CategoryChooser
@value={{this.value}}
@onChange={{this.update}}
@value={{this.data.value}}
@onChange={{@field.set}}
name={{@info.identifier}}
/>
</@field.Custom>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,6 @@ export default class PluginsExplorerController extends Controller {
this.form = form;
}

@action
updateParams(identifier, value) {
this.selectedItem.set(`params.${identifier}`, value);
}

@action
updateSearch(value) {
this.search = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,6 @@ export default class GroupReportsShowController extends Controller {
});
}

// This is necessary with glimmer's one way data stream to get the child's
// changes of 'params' to bubble up.
@action
updateParams(identifier, value) {
this.set(`model.params.${identifier}`, value);
}

@action
onRegisterApi(form) {
this.form = form;
Expand Down
37 changes: 35 additions & 2 deletions test/javascripts/components/param-input-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const InputTestCases = [
tests: [
{
input: null,
data_null: undefined,
data_null: "",
error: ERRORS.REQUIRED,
},
{
Expand Down Expand Up @@ -111,8 +111,9 @@ module("Data Explorer Plugin | Component | param-input", function (hooks) {
initialValues: config.initial
? { [testcase.type]: config.initial }
: {},
onRegisterApi: ({ submit }) => {
onRegisterApi: ({ submit, allNormalized }) => {
this.submit = submit;
this.allNormalized = allNormalized;
},
});

Expand All @@ -124,6 +125,8 @@ module("Data Explorer Plugin | Component | param-input", function (hooks) {
@onRegisterApi={{this.onRegisterApi}}
/>`);

await this.allNormalized;

if (config.initial || config.default) {
const data = await this.submit();
const val = config.initial || config.default;
Expand Down Expand Up @@ -201,4 +204,34 @@ module("Data Explorer Plugin | Component | param-input", function (hooks) {
await fillIn(`[name="string"]`, "");
assert.rejects(this.submit());
});

test("async normalizion", async function (assert) {
this.setProperties({
param_info: [
{
identifier: "category_id",
type: "category_id",
default: "support",
nullable: false,
},
],
initialValues: {},
onRegisterApi: (paramInputApi) => {
this.paramInputApi = paramInputApi;
},
});

await render(hbs`
<ParamInputForm
@initialValues={{this.initialValues}}
@paramInfo={{this.param_info}}
@onRegisterApi={{this.onRegisterApi}}
/>`);

await this.paramInputApi.allNormalized;

this.paramInputApi.submit().then((res) => {
assert.strictEqual(res.category_id, "1003");
});
});
});