File size: 6,484 Bytes
c72f0a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { IF_CONDITION_OPS, IF_MAX_ROWS, ifDefinitionIssue } from "./types";
import type { IfCondition, IfDefinition, IfResult, RollupConditionOp } from "./types";

export const IF_OP_LABELS: Record<RollupConditionOp, string> = {
  eq: "is", neq: "is not", contains: "contains", not_contains: "does not contain",
  is_empty: "is empty", is_not_empty: "is not empty", gt: "is greater than",
  gte: "is at least", lt: "is less than", lte: "is at most",
};

type SourceField = { key: string; label: string };
export const emptyIfDefinition = (): IfDefinition => ({ rows: [], default: { value: "" } });

function FieldOptions({ fields, current }: { fields: readonly SourceField[]; current: string }) {
  return <>
    {!fields.some((field) => field.key === current) && <option value={current}>
      {current ? "Unavailable field" : "Choose a field"}
    </option>}
    {fields.map((field) => <option key={field.key} value={field.key}>{field.label}</option>)}
  </>;
}

export function IfResultEditor({ label, result, fields, onChange }: {
  label: string; result: IfResult; fields: readonly SourceField[]; onChange: (result: IfResult) => void;
}) {
  const kind = "field" in result ? "field" : result.value === null ? "null"
    : typeof result.value === "number" ? "number" : typeof result.value === "boolean" ? "boolean" : "text";
  return <>
    <select className="cg-input" aria-label={`${label} kind`} value={kind} onChange={(event) => {
      const next = event.target.value;
      onChange(next === "field" ? { field: fields[0]?.key ?? "" }
        : { value: next === "null" ? null : next === "number" ? 0 : next === "boolean" ? false : "" });
    }}>
      <option value="text">Text</option><option value="number">Number</option>
      <option value="boolean">Yes or no</option><option value="null">Empty</option>
      <option value="field">Field</option>
    </select>
    {kind === "field" ? <select className="cg-input" aria-label={`${label} field`}
      value={result.field} onChange={(event) => onChange({ field: event.target.value })}>
      <FieldOptions fields={fields} current={result.field ?? ""} />
    </select> : kind === "boolean" ? <select className="cg-input" aria-label={`${label} value`}
      value={String(result.value)} onChange={(event) => onChange({ value: event.target.value === "true" })}>
      <option value="false">No</option><option value="true">Yes</option>
    </select> : kind === "number" ? <input className="cg-input" type="number" step="any"
      aria-label={`${label} value`} key={`${label}:${kind}:${String(result.value)}`}
      defaultValue={String(result.value)} onBlur={(event) => {
        const parsed = Number(event.target.value);
        const value = event.target.value.trim() && Number.isFinite(parsed) ? parsed : 0;
        event.target.value = String(value);
        onChange({ value });
      }} /> : kind === "text" ? <input className="cg-input" aria-label={`${label} value`}
      value={String(result.value ?? "")} onChange={(event) => onChange({ value: event.target.value })} /> : null}
  </>;
}

/** Reuse the flat rollup row anatomy, without conjunctions or statistic thresholds. */
export function IfStatementEditor({ definition, fields, onChange }: {
  definition: IfDefinition; fields: readonly SourceField[]; onChange: (definition: IfDefinition) => void;
}) {
  const patch = (index: number, next: IfCondition) => onChange({ ...definition,
    rows: definition.rows.map((row, i) => i === index ? next : row) });
  const move = (index: number, direction: number) => {
    const to = index + direction;
    if (to < 0 || to >= definition.rows.length) return;
    const rows = [...definition.rows];
    [rows[index], rows[to]] = [rows[to], rows[index]];
    onChange({ ...definition, rows });
  };
  const issue = ifDefinitionIssue(definition, fields);
  return <div className="cg-rollup-conditions cg-if-editor">
    <div className="cg-rollup-conditions__head"><span>Conditions</span>
      <button type="button" className="cg-btn" disabled={!fields.length || definition.rows.length >= IF_MAX_ROWS}
        onClick={() => onChange({ ...definition, rows: [...definition.rows,
          { field: fields[0]?.key ?? "", op: "eq", value: "", then: { value: "" } }] })}>
        Add condition
      </button>
    </div>
    {definition.rows.map((row, index) => <div className="cg-rollup-condition" key={index}>
      <select className="cg-input" aria-label={`Condition ${index + 1} field`} value={row.field}
        onChange={(event) => patch(index, { ...row, field: event.target.value })}>
        <FieldOptions fields={fields} current={row.field} />
      </select>
      <select className="cg-input" aria-label={`Condition ${index + 1} test`} value={row.op}
        onChange={(event) => patch(index, { ...row, op: event.target.value as RollupConditionOp,
          value: ["is_empty", "is_not_empty"].includes(event.target.value) ? "" : row.value })}>
        {IF_CONDITION_OPS.map((op) => <option key={op} value={op}>{IF_OP_LABELS[op]}</option>)}
      </select>
      <input className="cg-input" aria-label={`Condition ${index + 1} value`}
        disabled={row.op === "is_empty" || row.op === "is_not_empty"} value={String(row.value ?? "")}
        onChange={(event) => patch(index, { ...row, value: event.target.value })} />
      <button type="button" className="cg-icon-btn" aria-label={`Remove condition ${index + 1}`}
        onClick={() => onChange({ ...definition, rows: definition.rows.filter((_row, i) => i !== index) })}>×</button>
      <div style={{ gridColumn: "1 / -1" }}>
        <span className="cg-type-title">Result</span>
        <IfResultEditor label={`Condition ${index + 1} result`} result={row.then} fields={fields}
          onChange={(then) => patch(index, { ...row, then })} />
        <button type="button" className="cg-btn" aria-label={`Move condition ${index + 1} up`}
          disabled={index === 0} onClick={() => move(index, -1)}>Move up</button>
        <button type="button" className="cg-btn" aria-label={`Move condition ${index + 1} down`}
          disabled={index === definition.rows.length - 1} onClick={() => move(index, 1)}>Move down</button>
      </div>
    </div>)}
    <span className="cg-type-title">Default</span>
    <IfResultEditor label="Default result" result={definition.default} fields={fields}
      onChange={(fallback) => onChange({ ...definition, default: fallback })} />
    {issue && <div className="cg-field-hint" role="status">{issue}</div>}
  </div>;
}