Skip to content

A platform for community discussion. Free, open, simple.

UX: Let data table node columns be explicitly added and removed

Régis Hanol16ba84988393
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/boolean-control.gjs b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/boolean-control.gjs
index 8c349b1bd7c..02abcb869e5 100644
--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/boolean-control.gjs
+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/boolean-control.gjs
@@ -59,6 +59,7 @@ export default class BooleanControl extends Component {
@title={{this.label}}
@tooltip={{this.tooltip}}
@type="custom"
+ @validate={{@validate}}
@validation={{this.validation}}
as |field|
>
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/collection-add-menu.gjs b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/collection-add-menu.gjs
new file mode 100644
index 00000000000..ca8fca61de2
--- /dev/null
+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/collection-add-menu.gjs
@@ -0,0 +1,47 @@
+import { fn } from "@ember/helper";
+import DMenu from "discourse/float-kit/components/d-menu";
+import DButton from "discourse/ui-kit/d-button";
+import DDropdownMenu from "discourse/ui-kit/d-dropdown-menu";
+import dIcon from "discourse/ui-kit/helpers/d-icon";
+
+const CollectionAddMenu = <template>
+ <DMenu
+ class="btn btn-default workflows-property-engine__add-attrs-btn"
+ @inline={{true}}
+ @modalForwardRecipient={{true}}
+ >
+ <:trigger>
+ {{@label}}
+ {{dIcon "chevron-down"}}
+ </:trigger>
+ <:content as |menu|>
+ <DDropdownMenu as |dropdown|>
+ {{#if @addAllLabel}}
+ <dropdown.item>
+ <DButton
+ class="btn-transparent workflows-property-engine__add-all"
+ @action={{fn @onAddAll menu.close}}
+ @translatedLabel={{@addAllLabel}}
+ />
+ </dropdown.item>
+ <dropdown.divider />
+ {{/if}}
+ {{#each @options key="name" as |option|}}
+ <dropdown.item>
+ <DButton
+ class="btn-transparent"
+ @action={{fn @onAdd option menu.close}}
+ @translatedLabel={{if
+ @optionLabel
+ (@optionLabel option)
+ option.name
+ }}
+ />
+ </dropdown.item>
+ {{/each}}
+ </DDropdownMenu>
+ </:content>
+ </DMenu>
+</template>;
+
+export default CollectionAddMenu;
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/collection-option-row.gjs b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/collection-option-row.gjs
new file mode 100644
index 00000000000..92d22020750
--- /dev/null
+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/collection-option-row.gjs
@@ -0,0 +1,26 @@
+import DButton from "discourse/ui-kit/d-button";
+import dConcatClass from "discourse/ui-kit/helpers/d-concat-class";
+
+const CollectionOptionRow = <template>
+ <div
+ class={{dConcatClass
+ "workflows-property-engine__collection-row"
+ (if @inlineControl "--inline-control")
+ }}
+ ...attributes
+ >
+ <DButton
+ class="workflows-property-engine__collection-delete"
+ @action={{@onRemove}}
+ @icon="xmark"
+ @translatedAriaLabel={{@removeLabel}}
+ @translatedTitle={{@removeLabel}}
+ />
+
+ <div class="workflows-property-engine__collection-fields">
+ {{yield}}
+ </div>
+ </div>
+</template>;
+
+export default CollectionOptionRow;
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/collection.gjs b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/collection.gjs
index 4f137f7b1a6..ed0c16f660c 100644
--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/collection.gjs
+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/collection.gjs
@@ -1,21 +1,23 @@
import Component from "@glimmer/component";
import { fn, hash } from "@ember/helper";
import { action } from "@ember/object";
-import DMenu from "discourse/float-kit/components/d-menu";
import DButton from "discourse/ui-kit/d-button";
-import DDropdownMenu from "discourse/ui-kit/d-dropdown-menu";
-import dIcon from "discourse/ui-kit/helpers/d-icon";
import { i18n } from "discourse-i18n";
import {
- fieldControl,
+ keyedValue,
+ withoutKey,
+} from "../../../lib/workflows/keyed-collection";
+import {
fieldValue,
findNodeType,
- isExpression,
normalizePropertyOptions,
propertyDescription,
propertyLabel,
+ usesInlineBooleanControl,
} from "../../../lib/workflows/property-engine";
import WorkflowsEmptyState from "../empty-state";
+import CollectionAddMenu from "./collection-add-menu";
+import CollectionOptionRow from "./collection-option-row";
import Field from "./field";
export default class Collection extends Component {
@@ -64,7 +66,7 @@ export default class Collection extends Component {
}
get currentValue() {
- return this.args.formApi?.get(this.args.fieldName) || {};
+ return keyedValue(this.args.formApi?.get(this.args.fieldName));
}
get options() {
@@ -87,21 +89,9 @@ export default class Collection extends Component {
return this.availableOptions.length > 0;
}
- usesInlineControl(option) {
- return (
- fieldControl(option) === "boolean" &&
- !isExpression(this.currentValue[option.name])
- );
- }
-
@action
- collectionRowClass(option) {
- return [
- "workflows-property-engine__collection-row",
- this.usesInlineControl(option) ? "--inline-control" : null,
- ]
- .filter(Boolean)
- .join(" ");
+ usesInlineControl(option) {
+ return usesInlineBooleanControl(option, this.currentValue[option.name]);
}
@action
@@ -124,9 +114,10 @@ export default class Collection extends Component {
@action
removeOption(option) {
- const nextValue = { ...this.currentValue };
- delete nextValue[option.name];
- this.args.formApi.set(this.args.fieldName, nextValue);
+ this.args.formApi.set(
+ this.args.fieldName,
+ withoutKey(this.currentValue, option.name)
+ );
}
@action
@@ -188,71 +179,43 @@ export default class Collection extends Component {
{{else}}
<@form.Object @name={{@fieldName}} as |object item|>
{{#each this.selectedOptions key="name" as |option|}}
- <div class={{this.collectionRowClass option}}>
-
- <DButton
- class="workflows-property-engine__collection-delete"
- @action={{fn this.removeOption option}}
- @icon="xmark"
- @translatedAriaLabel={{i18n
- "discourse_workflows.property_engine.remove_assignment"
- name=(this.optionLabel option)
- }}
- @translatedTitle={{i18n
- "discourse_workflows.property_engine.remove_assignment"
- name=(this.optionLabel option)
- }}
+ <CollectionOptionRow
+ @inlineControl={{this.usesInlineControl option}}
+ @onRemove={{fn this.removeOption option}}
+ @removeLabel={{i18n
+ "discourse_workflows.property_engine.remove_assignment"
+ name=(this.optionLabel option)
+ }}
+ >
+ <Field
+ @configuration={{item}}
+ @connections={{@connections}}
+ @credentials={{@credentials}}
+ @fieldName={{option.name}}
+ @form={{object}}
+ @formApi={{@formApi}}
+ @label={{this.optionLabel option}}
+ @node={{@node}}
+ @nodeDefinition={{this.nodeDefinition}}
+ @nodeParameters={{@nodeParameters}}
+ @nodes={{@nodes}}
+ @nodeType={{@nodeType}}
+ @nodeTypes={{@nodeTypes}}
+ @schema={{option}}
+ @session={{@session}}
+ @showOptional={{false}}
/>
-
- <div class="workflows-property-engine__collection-fields">
- <Field
- @configuration={{item}}
- @connections={{@connections}}
- @credentials={{@credentials}}
- @fieldName={{option.name}}
- @form={{object}}
- @formApi={{@formApi}}
- @label={{this.optionLabel option}}
- @node={{@node}}
- @nodeDefinition={{this.nodeDefinition}}
- @nodeParameters={{@nodeParameters}}
- @nodes={{@nodes}}
- @nodeType={{@nodeType}}
- @nodeTypes={{@nodeTypes}}
- @schema={{option}}
- @session={{@session}}
- @showOptional={{false}}
- />
- </div>
- </div>
-
+ </CollectionOptionRow>
{{/each}}
</@form.Object>
{{#if this.hasAvailableOptions}}
- <DMenu
- class="btn btn-default workflows-property-engine__add-attrs-btn"
- @inline={{true}}
- @modalForwardRecipient={{true}}
- >
- <:trigger>
- {{this.addOptionLabel}}
- {{dIcon "chevron-down"}}
- </:trigger>
- <:content as |menu|>
- <DDropdownMenu as |dropdown|>
- {{#each this.availableOptions key="name" as |option|}}
- <dropdown.item>
- <DButton
- class="btn-transparent"
- @action={{fn this.addOption option menu.close}}
- @translatedLabel={{this.optionLabel option}}
- />
- </dropdown.item>
- {{/each}}
- </DDropdownMenu>
- </:content>
- </DMenu>
+ <CollectionAddMenu
+ @label={{this.addOptionLabel}}
+ @onAdd={{this.addOption}}
+ @optionLabel={{this.optionLabel}}
+ @options={{this.availableOptions}}
+ />
{{/if}}
{{/if}}
</@form.Section>
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/data-table-columns.gjs b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/data-table-columns.gjs
index f4a18a7d390..a07641b5c38 100644
--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/data-table-columns.gjs
+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/data-table-columns.gjs
@@ -1,79 +1,257 @@
import Component from "@glimmer/component";
-import { tracked } from "@glimmer/tracking";
-import { concat } from "@ember/helper";
+import { cached, tracked } from "@glimmer/tracking";
+import { fn } from "@ember/helper";
+import { action } from "@ember/object";
import { service } from "@ember/service";
+import dConcatClass from "discourse/ui-kit/helpers/d-concat-class";
+import { i18n } from "discourse-i18n";
+import {
+ isKeyedValue,
+ keyedValue,
+ withoutKey,
+} from "../../../lib/workflows/keyed-collection";
+import {
+ isExpression,
+ usesInlineBooleanControl,
+} from "../../../lib/workflows/property-engine";
+import CollectionAddMenu from "./collection-add-menu";
+import CollectionOptionRow from "./collection-option-row";
import Field from "./field";
+const COLUMN_FIELD_TYPES = { number: "integer", boolean: "boolean" };
+
function schemaForColumn(column) {
- switch (column.type) {
- case "number":
- return { type: "integer", name: column.name, ui: { expression: true } };
- case "boolean":
- return { type: "boolean", name: column.name, ui: { expression: true } };
- default:
- return { type: "string", name: column.name, ui: { expression: true } };
- }
+ return {
+ type: COLUMN_FIELD_TYPES[column.type] ?? "string",
+ name: column.name,
+ ui: { expression: true },
+ validate: "non_empty_expression",
+ };
}
export default class DataTableColumns extends Component {
@service workflowsNodeTypes;
+ // Keeps each column's schema stable across edits so its Field isn't rebuilt
+ // on every keystroke.
+ #schemas = new Map();
@tracked _loadedDataTables = null;
+ @tracked _loading = false;
+
constructor(owner, args) {
super(owner, args);
const identifier =
args.nodeDefinition?.name || args.nodeDefinition?.identifier;
- if (identifier) {
+ if (identifier && !args.metadata?.data_tables) {
+ const context = {
+ path: "data_table_id",
+ currentNodeParameters: args.configuration || {},
+ };
+ this._loading = true;
this.workflowsNodeTypes
.loadNodeParameterOptions(
identifier,
"data_tables",
args.nodeDefinition?.version,
- args.session?.nodeParameterOptionsContext({
- path: "data_table_id",
- currentNodeParameters: args.configuration || {},
- }) || {
- path: "data_table_id",
- currentNodeParameters: args.configuration || {},
- }
+ args.session?.nodeParameterOptionsContext(context) || context
)
.then((dataTables) => {
this._loadedDataTables = dataTables;
+ })
+ .finally(() => {
+ this._loading = false;
});
}
}
- get columns() {
+ @cached
+ get dataTable() {
const id = parseInt(this.args.configuration?.data_table_id, 10);
const dataTables =
this.args.metadata?.data_tables || this._loadedDataTables || [];
- const dataTable = dataTables.find((dt) => dt.id === id) || null;
- return (dataTable?.columns ?? []).filter((c) => !c.reserved);
+ return dataTables.find((dt) => dt.id === id) || null;
+ }
+
+ @cached
+ get columns() {
+ return (this.dataTable?.columns ?? []).filter((c) => !c.reserved);
}
- get columnsConfiguration() {
- return this.args.configuration?.[this.args.fieldName] || {};
+ get rawValue() {
+ return this.args.formApi?.get(this.args.fieldName);
+ }
+
+ get currentValue() {
+ return keyedValue(this.rawValue);
+ }
+
+ // Keys stay visible while the table is unresolved so they can still be
+ // edited or removed. A whole-object expression is left untouched.
+ get showColumns() {
+ if (this._loading) {
+ return false;
+ }
+ if (this.rawValue != null && !isKeyedValue(this.rawValue)) {
+ return false;
+ }
+ return Boolean(this.dataTable) || this.rows.length > 0;
+ }
+
+ @cached
+ get rows() {
+ const names = new Set(this.columns.map((column) => column.name));
+ const known = this.columns
+ .filter((column) => Object.hasOwn(this.currentValue, column.name))
+ .map((column) => ({
+ name: column.name,
+ schema: this.#schemaFor(column),
+ }));
+ // Only a resolved table can prove a column is gone; an expression id or an
+ // unloaded table can't, so those keys stay editable as plain columns.
+ const unknown = Object.keys(this.currentValue)
+ .filter((name) => !names.has(name))
+ .map((name) =>
+ this.dataTable
+ ? { name, missing: true }
+ : { name, schema: this.#schemaFor({ name }) }
+ );
+
+ return [...known, ...unknown];
+ }
+
+ @cached
+ get availableColumns() {
+ return this.columns.filter(
+ (column) => !Object.hasOwn(this.currentValue, column.name)
+ );
+ }
+
+ @action
+ usesInlineControl(row) {
+ return (
+ !row.missing &&
+ usesInlineBooleanControl(row.schema, this.currentValue[row.name])
+ );
+ }
+
+ // An empty expression is an error, not NULL, so the hint only fits Plain
+ // mode. "" rather than undefined stops Field falling back to a shared one.
+ @action
+ placeholderFor(name) {
+ if (isExpression(this.currentValue[name])) {
+ return "";
+ }
+ return i18n("discourse_workflows.data_table_node.empty_column_placeholder");
+ }
+
+ // Switching an empty expression back to Plain yields "", which looks like the
+ // NULL placeholder but would write an empty string.
+ @action
+ setColumnValue(value, { set, name }) {
+ set(name, value === "" ? null : value);
+ }
+
+ @action
+ addColumn(column, close) {
+ this.#addColumns([column]);
+ close?.();
+ }
+
+ @action
+ addAllColumns(close) {
+ this.#addColumns(this.availableColumns);
+ close?.();
+ }
+
+ @action
+ removeColumn(name) {
+ this.args.formApi.set(
+ this.args.fieldName,
+ withoutKey(this.currentValue, name)
+ );
+ }
+
+ #schemaFor(column) {
+ const key = `${column.type}:${column.name}`;
+ if (!this.#schemas.has(key)) {
+ this.#schemas.set(key, schemaForColumn(column));
+ }
+ return this.#schemas.get(key);
+ }
+
+ // Booleans start as false because the toggle can't show NULL.
+ #addColumns(columns) {
+ const added = Object.fromEntries(
+ columns.map((column) => [
+ column.name,
+ column.type === "boolean" ? false : null,
+ ])
+ );
+ this.args.formApi.set(this.args.fieldName, {
+ ...this.currentValue,
+ ...added,
+ });
}
<template>
- {{#if this.columns.length}}
- <@form.Section @title={{@label}}>
- <@form.Object @name={{@fieldName}} as |object|>
- {{#each this.columns key="name" as |column|}}
- <Field
- @configuration={{this.columnsConfiguration}}
- @fieldName={{column.name}}
- @form={{object}}
- @formApi={{@formApi}}
- @formApiPath={{concat @fieldName "." column.name}}
- @label={{column.name}}
- @schema={{schemaForColumn column}}
- @session={{@session}}
- />
+ {{#if this.showColumns}}
+ <@form.Section class="workflows-data-table-columns" @title={{@label}}>
+ <@form.Object @name={{@fieldName}} as |object item|>
+ {{#each this.rows key="name" as |row|}}
+ <CollectionOptionRow
+ class={{dConcatClass
+ "workflows-data-table-columns__column"
+ (if row.missing "--missing")
+ }}
+ data-column-name={{row.name}}
+ @inlineControl={{this.usesInlineControl row}}
+ @onRemove={{fn this.removeColumn row.name}}
+ @removeLabel={{i18n
+ "discourse_workflows.property_engine.remove_assignment"
+ name=row.name
+ }}
+ >
+ {{#if row.missing}}
+ <@form.Alert
+ class="workflows-data-table-columns__missing"
+ @type="error"
+ >
+ {{i18n
+ "discourse_workflows.data_table_node.missing_column"
+ name=row.name
+ }}
+ </@form.Alert>
+ {{else}}
+ <Field
+ @configuration={{item}}
+ @fieldName={{row.name}}
+ @form={{object}}
+ @formApi={{@formApi}}
+ @label={{row.name}}
+ @onSet={{this.setColumnValue}}
+ @placeholder={{this.placeholderFor row.name}}
+ @schema={{row.schema}}
+ @session={{@session}}
+ @showOptional={{false}}
+ />
+ {{/if}}
+ </CollectionOptionRow>
{{/each}}
</@form.Object>
+
+ {{#if this.availableColumns.length}}
+ <CollectionAddMenu
+ @addAllLabel={{i18n
+ "discourse_workflows.data_table_node.add_all_columns"
+ }}
+ @label={{i18n "discourse_workflows.data_table_node.add_column"}}
+ @onAdd={{this.addColumn}}
+ @onAddAll={{this.addAllColumns}}
+ @options={{this.availableColumns}}
+ />
+ {{/if}}
</@form.Section>
{{/if}}
</template>
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/field.gjs b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/field.gjs
index 8eab233bdcb..592dab879e2 100644
--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/field.gjs
+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/field.gjs
@@ -2,8 +2,8 @@ import Component from "@glimmer/component";
import { trustHTML } from "@ember/template";
import { applyValueTransformer } from "discourse/lib/transformer";
import { eq } from "discourse/truth-helpers";
-import { i18n } from "discourse-i18n";
import FIELD_CONTROL_REGISTRY from "../../../lib/workflows/field-control-registry";
+import { fieldValidator } from "../../../lib/workflows/field-validators";
import {
fieldControl,
fieldFormat,
@@ -13,7 +13,6 @@ import {
fieldSupportsExpression,
fieldType,
findNodeType,
- isExpression,
propertyDescription,
propertyDynamicValueHint,
propertyLabel,
@@ -21,28 +20,6 @@ import {
propertyTooltip,
} from "../../../lib/workflows/property-engine";
-const CRON_FIELD_PATTERN =
- /^(\*|\d+(-\d+)?(\/\d+)?|\*\/\d+)(,(\*|\d+(-\d+)?(\/\d+)?|\*\/\d+))*$/;
-
-function isValidCron(value) {
- if (!value || typeof value !== "string") {
- return false;
- }
- const fields = value.trim().split(/\s+/);
- return fields.length === 5 && fields.every((f) => CRON_FIELD_PATTERN.test(f));
-}
-
-const FIELD_VALIDATORS = {
- cron: (name, value, { addError }) => {
- if (value && !isExpression(value) && !isValidCron(value)) {
- addError(name, {
- title: i18n("discourse_workflows.schedule.cron"),
- message: i18n("discourse_workflows.schedule.cron_invalid"),
- });
- }
- },
-};
-
export default class Field extends Component {
get control() {
return fieldControl(this.args.schema);
@@ -103,7 +80,10 @@ export default class Field extends Component {
}
get placeholder() {
- return propertyPlaceholder(this.nodeDefinition, this.args.fieldName);
+ return (
+ this.args.placeholder ??
+ propertyPlaceholder(this.nodeDefinition, this.args.fieldName)
+ );
}
get showLabel() {
@@ -140,7 +120,7 @@ export default class Field extends Component {
}
get customValidation() {
- return FIELD_VALIDATORS[this.args.schema?.validate];
+ return fieldValidator(this.args.schema, this.fieldTitle);
}
get fieldDescription() {
@@ -201,6 +181,7 @@ export default class Field extends Component {
@schema={{@schema}}
@session={{@session}}
@showOptional={{this.showOptional}}
+ @validate={{this.customValidation}}
/>
{{else}}
<@form.Field
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/field-validators.js b/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/field-validators.js
new file mode 100644
index 00000000000..5c09cac4151
--- /dev/null
+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/field-validators.js
@@ -0,0 +1,52 @@
+import { i18n } from "discourse-i18n";
+import { isEmptyExpression, isExpression } from "./property-engine";
+
+const CRON_FIELD_PATTERN =
+ /^(\*|\d+(-\d+)?(\/\d+)?|\*\/\d+)(,(\*|\d+(-\d+)?(\/\d+)?|\*\/\d+))*$/;
+
+function isValidCron(value) {
+ if (!value || typeof value !== "string") {
+ return false;
+ }
+ const fields = value.trim().split(/\s+/);
+ return fields.length === 5 && fields.every((f) => CRON_FIELD_PATTERN.test(f));
+}
+
+const FIELD_VALIDATORS = {
+ cron: (name, value, { addError }) => {
+ if (value && !isExpression(value) && !isValidCron(value)) {
+ addError(name, {
+ title: i18n("discourse_workflows.schedule.cron"),
+ message: i18n("discourse_workflows.schedule.cron_invalid"),
+ });
+ }
+ },
+ // A blank expression runs as an empty string, which is never what was meant.
+ non_empty_expression: (name, value, { addError, title }) => {
+ if (isEmptyExpression(value)) {
+ addError(name, {
+ title,
+ message: i18n("discourse_workflows.parameter_field.empty_expression", {
+ plain: i18n("discourse_workflows.parameter_field.plain"),
+ }),
+ });
+ }
+ },
+};
+
+/**
+ * Returns the FormKit `@validate` callback named by `schema.validate`.
+ *
+ * @param {object} schema - The field schema.
+ * @param {string} title - The field title shown with any error.
+ * @returns {Function|undefined}
+ */
+export function fieldValidator(schema, title) {
+ const validator = FIELD_VALIDATORS[schema?.validate];
+ if (!validator) {
+ return undefined;
+ }
+
+ return (name, value, helpers) =>
+ validator(name, value, { ...helpers, title });
+}
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/keyed-collection.js b/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/keyed-collection.js
new file mode 100644
index 00000000000..3c7c3700d60
--- /dev/null
+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/keyed-collection.js
@@ -0,0 +1,35 @@
+// Helpers for object-valued parameters where a present key means "set" and an
+// absent key means "leave alone", so removing an entry must delete its key.
+
+/**
+ * Whether the value is a plain object of keyed entries.
+ *
+ * @param {any} value
+ * @returns {boolean}
+ */
+export function isKeyedValue(value) {
+ return value !== null && typeof value === "object" && !Array.isArray(value);
+}
+
+/**
+ * Returns the value when it is a plain object, otherwise an empty object.
+ *
+ * @param {any} value
+ * @returns {object}
+ */
+export function keyedValue(value) {
+ return isKeyedValue(value) ? value : {};
+}
+
+/**
+ * Returns a copy of the keyed value without the `name` entry.
+ *
+ * @param {object} value
+ * @param {string} name
+ * @returns {object}
+ */
+export function withoutKey(value, name) {
+ const nextValue = { ...value };
+ delete nextValue[name];
+ return nextValue;
+}
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/property-engine.js b/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/property-engine.js
index 438dc055667..8c1c262d5d8 100644
--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/property-engine.js
+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/property-engine.js
@@ -450,6 +450,26 @@ export function wholeExpressionBody(value) {
: null;
}
+/**
+ * Whether the value is an expression with nothing to evaluate, e.g. `=` or
+ * `={{ }}`. Mirrors ExpressionResolver.empty_expression? on the server.
+ */
+export function isEmptyExpression(value) {
+ return isExpression(value) && /^=\s*(\{\{\s*\}\})?\s*$/.test(value);
+}
+
+/**
+ * Whether a field renders its boolean toggle inline, which only applies
+ * outside expression mode.
+ *
+ * @param {object} schema - The field schema.
+ * @param {any} value - The field's current value.
+ * @returns {boolean}
+ */
+export function usesInlineBooleanControl(schema, value) {
+ return fieldControl(schema) === "boolean" && !isExpression(value);
+}
+
/** Mirrors ExpressionResolver.find_matching_close on the server. */
function findMatchingClose(template, start) {
let depth = 1;
diff --git a/plugins/discourse-workflows/config/locales/client.en.yml b/plugins/discourse-workflows/config/locales/client.en.yml
index b992746c3e3..9eb31f57b9b 100644
--- a/plugins/discourse-workflows/config/locales/client.en.yml
+++ b/plugins/discourse-workflows/config/locales/client.en.yml
@@ -799,6 +799,7 @@ en:
fixed: "Fixed"
plain: "Plain"
dynamic: "Dynamic"
+ empty_expression: "Enter an expression or switch back to %{plain}"
topic_control:
unavailable_topic: "Topic #%{id}"
expression_preview:
@@ -1427,6 +1428,10 @@ en:
mapping_mode_auto: "Map automatically"
columns_case_sensitive_hint_description: "Column mappings must match the data table column names exactly (case sensitive)"
columns: "Columns"
+ add_column: "Add column"
+ add_all_columns: "All columns"
+ empty_column_placeholder: "Empty (NULL)"
+ missing_column: "Column %{name} can’t be written to this data table. Remove it, or the node fails when it runs."
filter_combinator: "Match mode"
filter_combinator_and: "All conditions must match"
filter_combinator_or: "Any condition can match"
diff --git a/plugins/discourse-workflows/spec/system/data_table_node_spec.rb b/plugins/discourse-workflows/spec/system/data_table_node_spec.rb
new file mode 100644
index 00000000000..e39714089c9
--- /dev/null
+++ b/plugins/discourse-workflows/spec/system/data_table_node_spec.rb
@@ -0,0 +1,84 @@
+# frozen_string_literal: true
+
+RSpec.describe "Discourse Workflows | data table node" do
+ fab!(:admin)
+ fab!(:data_table) do
+ Fabricate(
+ :discourse_workflows_data_table,
+ columns: [
+ { "name" => "key", "type" => "string" },
+ { "name" => "val", "type" => "string" },
+ { "name" => "val2", "type" => "string" },
+ ],
+ )
+ end
+
+ let(:editor_page) { PageObjects::Pages::DiscourseWorkflows::WorkflowEditor.new }
+
+ before { sign_in(admin) }
+
+ def workflow_with_update_node(columns)
+ workflow = Fabricate(:discourse_workflows_workflow, created_by: admin)
+ graph =
+ build_workflow_graph do |g|
+ g.node "trigger-1", "trigger:manual", name: "Manual trigger"
+ g.node "update-1",
+ "action:data_table",
+ name: "Update row",
+ configuration: {
+ "operation" => "update",
+ "data_table_id" => data_table.id.to_s,
+ "filter" => [
+ {
+ "columnName" => "key",
+ "operator" => {
+ "type" => "string",
+ "operation" => "equals",
+ "singleValue" => false,
+ },
+ "value" => "1",
+ },
+ ],
+ "columns" => columns,
+ }
+ g.chain "trigger-1", "update-1"
+ end
+ workflow.update!(**graph)
+ workflow
+ end
+
+ def saved_columns(workflow)
+ workflow.reload.find_node("update-1")["parameters"]["columns"]
+ end
+
+ it "does not write a removed column and flags a cleared expression" do
+ workflow = workflow_with_update_node({ "val2" => "={{ 1 }}" })
+
+ editor_page.visit_node(workflow, "update-1")
+ expect(editor_page).to have_data_table_column("val2")
+
+ editor_page.add_data_table_column("val")
+ editor_page.fill_data_table_column("val", with: "changed")
+ try_until_success do
+ expect(saved_columns(workflow)).to eq("val2" => "={{ 1 }}", "val" => "changed")
+ end
+
+ editor_page.remove_data_table_column("val")
+ try_until_success { expect(saved_columns(workflow)).to eq("val2" => "={{ 1 }}") }
+
+ page.refresh
+ expect(editor_page).to have_node_configurator
+ expect(editor_page).to have_no_data_table_column("val")
+
+ editor_page.clear_data_table_column_expression("val2")
+
+ expect(editor_page).to have_data_table_column_error(
+ "val2",
+ I18n.t(
+ "js.discourse_workflows.parameter_field.empty_expression",
+ plain: I18n.t("js.discourse_workflows.parameter_field.plain"),
+ ),
+ )
+ try_until_success { expect(saved_columns(workflow)).to eq("val2" => "=") }
+ end
+end
diff --git a/plugins/discourse-workflows/spec/system/page_objects/pages/workflow_editor.rb b/plugins/discourse-workflows/spec/system/page_objects/pages/workflow_editor.rb
index daea77ae71b..def1a32a853 100644
--- a/plugins/discourse-workflows/spec/system/page_objects/pages/workflow_editor.rb
+++ b/plugins/discourse-workflows/spec/system/page_objects/pages/workflow_editor.rb
@@ -118,6 +118,50 @@ module PageObjects
page.has_css?(".workflows-configurator-modal__save-status--saved")
end
+ def add_data_table_column(name)
+ PageObjects::Components::DMenu.new(
+ ".workflows-data-table-columns .workflows-property-engine__add-attrs-btn",
+ ).expand
+ find(".dropdown-menu__item .btn-transparent", exact_text: name).click
+ self
+ end
+
+ def fill_data_table_column(name, with:)
+ # FormKitField#fill_in is ambiguous here: the Plain/Dynamic toggle adds radio inputs.
+ find("#{data_table_column_selector(name)} input.form-kit__control-input").fill_in(with:)
+ self
+ end
+
+ def remove_data_table_column(name)
+ find(
+ "#{data_table_column_selector(name)} .workflows-property-engine__collection-delete",
+ ).click
+ self
+ end
+
+ def has_data_table_column?(name)
+ page.has_css?(data_table_column_selector(name))
+ end
+
+ def clear_data_table_column_expression(name)
+ within(data_table_column_selector(name)) do
+ expression_editor = ExpressionEditor.new
+ length = expression_editor.editor_text.length
+ expression_editor.type_in_editor("").move_cursor_and_delete(length, length)
+ end
+ find(".workflows-data-table-columns .form-kit__section-title").click
+ self
+ end
+
+ def has_data_table_column_error?(name, message)
+ data_table_column_field(name).has_errors?(message)
+ end
+
+ def has_no_data_table_column?(name)
+ page.has_css?(".workflows-data-table-columns") &&
+ page.has_no_css?(data_table_column_selector(name))
+ end
+
def add_status_filter(status)
selector = PageObjects::Components::SelectKit.new("[data-name='statuses'] .multi-select")
selector.expand
@@ -320,6 +364,16 @@ module PageObjects
private
+ def data_table_column_selector(name)
+ ".workflows-data-table-columns__column[data-column-name='#{name}']"
+ end
+
+ def data_table_column_field(name)
+ PageObjects::Components::FormKitField.new(
+ find("#{data_table_column_selector(name)} .form-kit__field"),
+ )
+ end
+
def workflow_path(workflow)
"#{WORKFLOWS_PATH}/#{workflow.id}"
end
diff --git a/plugins/discourse-workflows/test/javascripts/integration/components/workflows/data-table-columns-test.gjs b/plugins/discourse-workflows/test/javascripts/integration/components/workflows/data-table-columns-test.gjs
new file mode 100644
index 00000000000..1b676a7f207
--- /dev/null
+++ b/plugins/discourse-workflows/test/javascripts/integration/components/workflows/data-table-columns-test.gjs
@@ -0,0 +1,313 @@
+import { click, findAll, render, settled, waitFor } from "@ember/test-helpers";
+import { module, test } from "qunit";
+import Form from "discourse/components/form";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import pretender, { response } from "discourse/tests/helpers/create-pretender";
+import formKit from "discourse/tests/helpers/form-kit-helper";
+import { i18n } from "discourse-i18n";
+import DataTableColumns from "discourse/plugins/discourse-workflows/admin/components/workflows/configurators/data-table-columns";
+
+const COLUMN = ".workflows-data-table-columns__column";
+const ADD_MENU = ".workflows-property-engine__add-attrs-btn";
+
+const METADATA = {
+ data_tables: [
+ {
+ id: 1,
+ name: "Contacts",
+ columns: [
+ { name: "id", type: "number", reserved: true },
+ { name: "title", type: "string" },
+ { name: "score", type: "number" },
+ { name: "active", type: "boolean" },
+ ],
+ },
+ ],
+};
+
+const NODE_DEFINITION = { identifier: "action:data_table", version: "1.0" };
+
+const EMPTY_PLACEHOLDER = i18n(
+ "discourse_workflows.data_table_node.empty_column_placeholder"
+);
+const EMPTY_EXPRESSION_ERROR = i18n(
+ "discourse_workflows.parameter_field.empty_expression",
+ { plain: i18n("discourse_workflows.parameter_field.plain") }
+);
+
+function column(name) {
+ return `${COLUMN}[data-column-name="${name}"]`;
+}
+
+function renderColumns(
+ context,
+ columns,
+ { metadata = METADATA, nodeDefinition } = {}
+) {
+ context.setProperties({
+ configuration: { data_table_id: 1, columns },
+ formApi: null,
+ metadata,
+ nodeDefinition,
+ registerApi: (api) => context.set("formApi", api),
+ });
+
+ return render(
+ <template>
+ <Form
+ @data={{context.configuration}}
+ @onRegisterApi={{context.registerApi}}
+ as |form transientData|
+ >
+ <DataTableColumns
+ @configuration={{transientData}}
+ @fieldName="columns"
+ @form={{form}}
+ @formApi={{context.formApi}}
+ @label="Columns"
+ @metadata={{context.metadata}}
+ @nodeDefinition={{context.nodeDefinition}}
+ />
+ </Form>
+ </template>
+ );
+}
+
+function menuItems() {
+ return findAll(".dropdown-menu__item .btn-transparent");
+}
+
+function menuText(element) {
+ return element.textContent.trim();
+}
+
+async function openAddMenu() {
+ await click(ADD_MENU);
+ await waitFor(".dropdown-menu__item .btn-transparent");
+}
+
+async function addColumn(name) {
+ await openAddMenu();
+ await click(menuItems().find((element) => menuText(element) === name));
+}
+
+module("Integration | Component | DataTableColumns", function (hooks) {
+ setupRenderingTest(hooks);
+
+ hooks.beforeEach(function () {
+ pretender.get("/admin/plugins/discourse-workflows/variables.json", () =>
+ response(200, { variables: [] })
+ );
+ });
+
+ test("renders only the columns present in the configuration", async function (assert) {
+ await renderColumns(this, { title: "Hello", score: null });
+
+ assert
+ .dom(COLUMN)
+ .exists({ count: 2 }, "only present columns are rendered");
+ assert.form().field("columns.title").hasValue("Hello");
+ assert
+ .dom(`${column("score")} input`)
+ .hasAttribute("placeholder", EMPTY_PLACEHOLDER, "NULL shows as empty");
+ assert.dom(column("active")).doesNotExist("absent columns are hidden");
+ });
+
+ test("adds a single column from the menu", async function (assert) {
+ await renderColumns(this, {});
+
+ assert.dom(COLUMN).doesNotExist("starts with no columns");
+
+ await addColumn("score");
+
+ assert.dom(column("score")).exists("the added column is rendered");
+ assert.deepEqual(this.formApi.get("columns"), { score: null });
+
+ await openAddMenu();
+
+ assert.deepEqual(
+ menuItems().map(menuText),
+ [
+ i18n("discourse_workflows.data_table_node.add_all_columns"),
+ "title",
+ "active",
+ ],
+ "the menu lists only the columns not added yet"
+ );
+ });
+
+ test("adds all remaining columns at once", async function (assert) {
+ await renderColumns(this, { title: "Hello" });
+
+ await openAddMenu();
+ await click(".workflows-property-engine__add-all");
+
+ assert.deepEqual(this.formApi.get("columns"), {
+ title: "Hello",
+ score: null,
+ active: false,
+ });
+ assert.dom(COLUMN).exists({ count: 3 });
+ assert.dom(ADD_MENU).doesNotExist("nothing left to add");
+ });
+
+ test("removing a column deletes its key", async function (assert) {
+ await renderColumns(this, { title: "Hello", score: 3 });
+
+ await click(
+ `${column("title")} .workflows-property-engine__collection-delete`
+ );
+
+ assert.deepEqual(
+ this.formApi.get("columns"),
+ { score: 3 },
+ "the key is removed rather than set to null"
+ );
+ assert.dom(column("title")).doesNotExist();
+ });
+
+ test("clearing a column keeps it as an explicit NULL", async function (assert) {
+ await renderColumns(this, { title: "Hello" });
+
+ await formKit().field("columns.title").fillIn("");
+
+ assert.true(
+ Object.hasOwn(this.formApi.get("columns"), "title"),
+ "the key stays present"
+ );
+ assert.strictEqual(this.formApi.get("columns.title"), null);
+ assert
+ .dom(`${column("title")} input`)
+ .hasAttribute("placeholder", EMPTY_PLACEHOLDER);
+ });
+
+ test("boolean columns can be unset by removing them", async function (assert) {
+ await renderColumns(this, { active: true });
+
+ assert
+ .dom(column("active"))
+ .hasClass("--inline-control", "booleans use the inline row");
+
+ await click(
+ `${column("active")} .workflows-property-engine__collection-delete`
+ );
+
+ assert.deepEqual(this.formApi.get("columns"), {});
+ });
+
+ test("shows removable notices for columns that can't be written", async function (assert) {
+ await renderColumns(this, { renamed: "x", id: 5, title: "Hello" });
+
+ assert.dom(column("id")).hasClass("--missing");
+ assert.dom(column("renamed")).hasClass("--missing");
+ assert
+ .dom(`${column("renamed")} .workflows-data-table-columns__missing`)
+ .includesText("renamed");
+ assert.dom(`${column("renamed")} input`).doesNotExist();
+
+ await click(
+ `${column("renamed")} .workflows-property-engine__collection-delete`
+ );
+
+ assert.deepEqual(this.formApi.get("columns"), { id: 5, title: "Hello" });
+ });
+
+ test("validates dynamic expressions on text and boolean columns", async function (assert) {
+ await renderColumns(this, {
+ title: "={{ }}",
+ active: "=",
+ score: "={{ $json.score }}",
+ });
+
+ await this.formApi.submit();
+
+ assert.form().field("columns.title").hasError(EMPTY_EXPRESSION_ERROR);
+ assert.form().field("columns.active").hasError(EMPTY_EXPRESSION_ERROR);
+ assert.form().field("columns.score").hasNoErrors();
+ assert
+ .dom(column("active"))
+ .doesNotHaveClass(
+ "--inline-control",
+ "dynamic booleans use the full row"
+ );
+ });
+
+ test("switching an empty expression back to plain keeps NULL", async function (assert) {
+ await renderColumns(this, { title: "=" });
+
+ assert
+ .dom(`${column("title")} [placeholder="${EMPTY_PLACEHOLDER}"]`)
+ .doesNotExist("no NULL placeholder in dynamic mode");
+
+ await click(
+ `${column("title")} .d-segmented-control__input[value='plain']`
+ );
+
+ assert.strictEqual(this.formApi.get("columns.title"), null);
+ assert
+ .dom(`${column("title")} input.form-kit__control-input`)
+ .hasAttribute("placeholder", EMPTY_PLACEHOLDER);
+ });
+
+ test("leaves a whole-object expression untouched", async function (assert) {
+ await renderColumns(this, "={{ $json }}");
+
+ assert.dom(".workflows-data-table-columns").doesNotExist();
+ assert.strictEqual(this.formApi.get("columns"), "={{ $json }}");
+ });
+
+ test("waits for the data tables before rendering the columns", async function (assert) {
+ let release;
+ const released = new Promise((resolve) => (release = resolve));
+
+ pretender.post(
+ "/admin/plugins/discourse-workflows/dynamic-node-parameters/options.json",
+ async () => {
+ await released;
+ return response(METADATA.data_tables);
+ }
+ );
+
+ const renderPromise = renderColumns(
+ this,
+ { title: "Hello" },
+ { metadata: null, nodeDefinition: NODE_DEFINITION }
+ );
+
+ await waitFor("form");
+
+ assert
+ .dom(".workflows-data-table-columns")
+ .doesNotExist("nothing renders while loading");
+ assert.dom(`${COLUMN}.--missing`).doesNotExist();
+
+ release();
+ await renderPromise;
+ await settled();
+
+ assert.dom(column("title")).exists();
+ assert.dom(column("title")).doesNotHaveClass("--missing");
+ });
+
+ test("keeps saved columns editable when the data table can't be resolved", async function (assert) {
+ pretender.post(
+ "/admin/plugins/discourse-workflows/dynamic-node-parameters/options.json",
+ () => response([])
+ );
+
+ await renderColumns(
+ this,
+ { title: "Hello" },
+ { metadata: null, nodeDefinition: NODE_DEFINITION }
+ );
+
+ assert.dom(column("title")).doesNotHaveClass("--missing");
+ assert.form().field("columns.title").hasValue("Hello");
+
+ await click(
+ `${column("title")} .workflows-property-engine__collection-delete`
+ );
+
+ assert.deepEqual(this.formApi.get("columns"), {});
+ });
+});
diff --git a/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-field-validators-test.js b/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-field-validators-test.js
new file mode 100644
index 00000000000..d94cb2341ac
--- /dev/null
+++ b/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-field-validators-test.js
@@ -0,0 +1,39 @@
+import { module, test } from "qunit";
+import { fieldValidator } from "discourse/plugins/discourse-workflows/admin/lib/workflows/field-validators";
+
+function errorsFor(schema, value) {
+ const errors = [];
+ fieldValidator(schema, "Title")("field", value, {
+ addError: (name, error) => errors.push({ name, ...error }),
+ });
+ return errors;
+}
+
+module("Unit | lib | discourse-workflows | field-validators", function () {
+ test("returns undefined for an unknown validator", function (assert) {
+ assert.strictEqual(
+ fieldValidator({ validate: "nope" }, "Title"),
+ undefined
+ );
+ assert.strictEqual(fieldValidator({}, "Title"), undefined);
+ assert.strictEqual(fieldValidator(undefined, "Title"), undefined);
+ });
+
+ test("cron flags invalid expressions only", function (assert) {
+ const cron = { validate: "cron" };
+
+ assert.strictEqual(errorsFor(cron, "bad").length, 1);
+ assert.strictEqual(errorsFor(cron, "*/5 * * * *").length, 0);
+ assert.strictEqual(errorsFor(cron, "={{ $json.cron }}").length, 0);
+ assert.strictEqual(errorsFor(cron, "").length, 0);
+ });
+
+ test("non_empty_expression flags blank expressions only", function (assert) {
+ const schema = { validate: "non_empty_expression" };
+
+ const [error] = errorsFor(schema, "={{ }}");
+ assert.strictEqual(error.name, "field");
+ assert.strictEqual(error.title, "Title", "uses the field title");
+ assert.strictEqual(errorsFor(schema, "={{ x }}").length, 0);
+ });
+});
diff --git a/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-property-engine-test.js b/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-property-engine-test.js
index 0fc40301559..cc6fb0df91b 100644
--- a/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-property-engine-test.js
+++ b/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-property-engine-test.js
@@ -12,6 +12,7 @@ import {
getPropertySchema,
i18nPrefix,
i18nScope,
+ isEmptyExpression,
localeKeyPart,
normalizeOptions,
propertyDescription,
@@ -564,4 +565,21 @@ module("Unit | Utility | workflows property engine", function () {
assert.strictEqual(wholeExpressionBody(input), expected, String(input));
});
});
+
+ test("isEmptyExpression", function (assert) {
+ [
+ ["=", true],
+ ["= ", true],
+ ["={{ }}", true],
+ ["={{ }}", true],
+ ["= {{ }} ", true],
+ ["={{ x }}", false],
+ ["={{ }}{{ x }}", false],
+ ["", false],
+ [null, false],
+ [false, false],
+ ].forEach(([input, expected]) => {
+ assert.strictEqual(isEmptyExpression(input), expected, String(input));
+ });
+ });
});