@kivacore/ui-pro-shared 0.0.9 → 0.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +167 -1
- package/src/.kiva/config.json +27 -0
- package/src/components/avatar-group.tsx +222 -0
- package/src/components/kiva/bulk-update-dialog/bulk-update-dialog.tsx +240 -0
- package/src/components/kiva/bulk-update-dialog/index.tsx +6 -0
- package/src/components/kiva/bulk-update-dialog/types.ts +33 -0
- package/src/components/kiva/calendar/animations.ts +40 -0
- package/src/components/kiva/calendar/calendar-body.tsx +106 -0
- package/src/components/kiva/calendar/calendar-sidebar.tsx +103 -0
- package/src/components/kiva/calendar/calendar.tsx +243 -0
- package/src/components/kiva/calendar/constants.ts +12 -0
- package/src/components/kiva/calendar/contexts/calendar-context.tsx +696 -0
- package/src/components/kiva/calendar/contexts/dnd-context.tsx +173 -0
- package/src/components/kiva/calendar/dialogs/add-edit-event-dialog.tsx +287 -0
- package/src/components/kiva/calendar/dialogs/delete-event-dialog.tsx +73 -0
- package/src/components/kiva/calendar/dialogs/event-details-dialog.tsx +163 -0
- package/src/components/kiva/calendar/dialogs/events-list-dialog.tsx +122 -0
- package/src/components/kiva/calendar/dnd/draggable-event.tsx +66 -0
- package/src/components/kiva/calendar/dnd/droppable-area.tsx +53 -0
- package/src/components/kiva/calendar/dnd/resizable-event.tsx +190 -0
- package/src/components/kiva/calendar/header/calendar-header.tsx +84 -0
- package/src/components/kiva/calendar/header/date-navigator.tsx +188 -0
- package/src/components/kiva/calendar/header/filter.tsx +80 -0
- package/src/components/kiva/calendar/header/today-button.tsx +49 -0
- package/src/components/kiva/calendar/header/user-select.tsx +111 -0
- package/src/components/kiva/calendar/header/view-tabs.tsx +144 -0
- package/src/components/kiva/calendar/helpers.ts +617 -0
- package/src/components/kiva/calendar/hooks/use-disclosure.ts +19 -0
- package/src/components/kiva/calendar/hooks/use-local-storage.ts +40 -0
- package/src/components/kiva/calendar/hooks/use-media-query.ts +25 -0
- package/src/components/kiva/calendar/hooks.ts +5 -0
- package/src/components/kiva/calendar/index.tsx +27 -0
- package/src/components/kiva/calendar/interfaces.ts +35 -0
- package/src/components/kiva/calendar/schemas.ts +20 -0
- package/src/components/kiva/calendar/settings/settings.tsx +116 -0
- package/src/components/kiva/calendar/skeletons/calendar-header-skeleton.tsx +27 -0
- package/src/components/kiva/calendar/skeletons/calendar-skeleton.tsx +19 -0
- package/src/components/kiva/calendar/skeletons/day-view-skeleton.tsx +40 -0
- package/src/components/kiva/calendar/skeletons/month-view-skeleton.tsx +36 -0
- package/src/components/kiva/calendar/skeletons/week-view-skeleton.tsx +48 -0
- package/src/components/kiva/calendar/skeletons/year-view-skeleton.tsx +42 -0
- package/src/components/kiva/calendar/types.ts +25 -0
- package/src/components/kiva/calendar/views/agenda-view/agenda-events.tsx +150 -0
- package/src/components/kiva/calendar/views/month-view/calendar-month-view.tsx +82 -0
- package/src/components/kiva/calendar/views/month-view/day-cell.tsx +218 -0
- package/src/components/kiva/calendar/views/month-view/event-bullet.tsx +46 -0
- package/src/components/kiva/calendar/views/month-view/month-event-badge.tsx +154 -0
- package/src/components/kiva/calendar/views/timeline/calendar-timeline-view.tsx +100 -0
- package/src/components/kiva/calendar/views/timeline/lib/timeline-layout.ts +130 -0
- package/src/components/kiva/calendar/views/timeline/lib/timeline-model.ts +504 -0
- package/src/components/kiva/calendar/views/timeline/timeline-event-bar.tsx +68 -0
- package/src/components/kiva/calendar/views/timeline/timeline-grid-header.tsx +125 -0
- package/src/components/kiva/calendar/views/timeline/timeline-row.tsx +159 -0
- package/src/components/kiva/calendar/views/timeline/timeline-section.tsx +61 -0
- package/src/components/kiva/calendar/views/timeline/use-timeline-sections.ts +278 -0
- package/src/components/kiva/calendar/views/week-and-day-view/calendar-day-view.tsx +277 -0
- package/src/components/kiva/calendar/views/week-and-day-view/calendar-time-line.tsx +47 -0
- package/src/components/kiva/calendar/views/week-and-day-view/calendar-week-view.tsx +229 -0
- package/src/components/kiva/calendar/views/week-and-day-view/day-view-multi-day-events-row.tsx +79 -0
- package/src/components/kiva/calendar/views/week-and-day-view/event-block.tsx +116 -0
- package/src/components/kiva/calendar/views/week-and-day-view/render-grouped-events.tsx +54 -0
- package/src/components/kiva/calendar/views/week-and-day-view/week-view-multi-day-events-row.tsx +159 -0
- package/src/components/kiva/calendar/views/year-view/calendar-year-view.tsx +159 -0
- package/src/components/kiva/data-table-side-filters/components/side-filter-active-chips.tsx +137 -0
- package/src/components/kiva/data-table-side-filters/components/side-filter-clear.tsx +34 -0
- package/src/components/kiva/data-table-side-filters/components/side-filter-search.tsx +67 -0
- package/src/components/kiva/data-table-side-filters/components/side-filter-section.tsx +77 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-async-options.tsx +354 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-boolean.tsx +66 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-checkbox-list.tsx +137 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-date-range.tsx +204 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-numeric-range.tsx +162 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-options-dropdown.tsx +191 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-text.tsx +53 -0
- package/src/components/kiva/data-table-side-filters/core/query-translator.ts +367 -0
- package/src/components/kiva/data-table-side-filters/core/types.ts +81 -0
- package/src/components/kiva/data-table-side-filters/data-table-side-filters.tsx +483 -0
- package/src/components/kiva/data-table-side-filters/hooks/use-data-table-side-filters.tsx +160 -0
- package/src/components/kiva/data-table-side-filters/hooks/use-show-more.tsx +62 -0
- package/src/components/kiva/data-table-side-filters/index.tsx +44 -0
- package/src/components/kiva/data-table-side-filters/lib/render-mode.ts +62 -0
- package/src/components/kiva/date-time-picker.tsx +163 -0
- package/src/components/kiva/editable-record-detail.tsx +809 -0
- package/src/components/kiva/enum-option-editor/enum-option-editor.tsx +891 -0
- package/src/components/kiva/enum-option-editor/index.tsx +26 -0
- package/src/components/kiva/enum-option-editor/kiva-enum-option-editor.tsx +320 -0
- package/src/components/kiva/enum-option-editor/types.ts +90 -0
- package/src/components/kiva/kanban.tsx +1376 -0
- package/src/components/kiva/kiva-data-export-menu.tsx +543 -0
- package/src/components/kiva/map.tsx +1631 -0
- package/src/components/kiva/pivot-calendar/components/group-sidebar.tsx +95 -0
- package/src/components/kiva/pivot-calendar/components/pivot-calendar-toolbar.tsx +113 -0
- package/src/components/kiva/pivot-calendar/components/stats-cell.tsx +126 -0
- package/src/components/kiva/pivot-calendar/helpers.ts +374 -0
- package/src/components/kiva/pivot-calendar/hooks/use-pivot-calendar.ts +222 -0
- package/src/components/kiva/pivot-calendar/index.tsx +32 -0
- package/src/components/kiva/pivot-calendar/pivot-calendar.tsx +107 -0
- package/src/components/kiva/pivot-calendar/types.ts +154 -0
- package/src/components/kiva/pivot-calendar/views/days-of-month-view.tsx +37 -0
- package/src/components/kiva/pivot-calendar/views/days-of-week-view.tsx +37 -0
- package/src/components/kiva/pivot-calendar/views/month-calendar-view.tsx +154 -0
- package/src/components/kiva/pivot-calendar/views/months-of-year-view.tsx +40 -0
- package/src/components/kiva/pivot-calendar/views/pivot-grid-view.tsx +251 -0
- package/src/components/kiva/pivot-filter/index.tsx +9 -0
- package/src/components/kiva/pivot-filter/pivot-filter-item.tsx +159 -0
- package/src/components/kiva/pivot-filter/pivot-filter-settings.tsx +161 -0
- package/src/components/kiva/pivot-filter/pivot-filter.tsx +187 -0
- package/src/components/kiva/pivot-filter/types.ts +82 -0
- package/src/components/kiva/pivot-grid/hooks/use-pivot-grid.ts +683 -0
- package/src/components/kiva/pivot-grid/index.tsx +23 -0
- package/src/components/kiva/pivot-grid/lib/pivot-grid-color-rules.ts +98 -0
- package/src/components/kiva/pivot-grid/lib/pivot-grid-export.ts +265 -0
- package/src/components/kiva/pivot-grid/lib/pivot-grid-model.ts +1110 -0
- package/src/components/kiva/pivot-grid/pivot-grid-demo-data.ts +200 -0
- package/src/components/kiva/pivot-grid/pivot-grid-export-menu.tsx +66 -0
- package/src/components/kiva/pivot-grid/pivot-grid-toolbar.tsx +85 -0
- package/src/components/kiva/pivot-grid/pivot-grid.tsx +601 -0
- package/src/components/kiva/pivot-grid/types.ts +195 -0
- package/src/components/kiva/place-autocomplete.tsx +484 -0
- package/src/components/kiva/record-delete-confirm-dialog/index.tsx +10 -0
- package/src/components/kiva/record-delete-confirm-dialog/record-delete-confirm-dialog.tsx +266 -0
- package/src/components/kiva/record-delete-confirm-dialog/types.ts +25 -0
- package/src/hooks/kiva/_internal/autonumber-setting.ts +112 -0
- package/src/hooks/kiva/_internal/form-layout-to-view.ts +665 -0
- package/src/hooks/kiva/_internal/kiva-hook-utils.ts +143 -0
- package/src/hooks/kiva/_internal/use-computed-field-evaluator.tsx +310 -0
- package/src/hooks/kiva/_internal/use-field-actions-evaluator.tsx +398 -0
- package/src/hooks/kiva/_internal/use-form-actions-evaluator.tsx +363 -0
- package/src/hooks/kiva/_internal/use-form-view-engine.tsx +2200 -0
- package/src/hooks/kiva/_internal/use-kiva-form-view-fields.tsx +190 -0
- package/src/hooks/kiva/_internal/use-kiva-kanban-cards.tsx +311 -0
- package/src/hooks/kiva/use-data-export.tsx +265 -0
- package/src/hooks/kiva/use-kiva-calendar.tsx +992 -0
- package/src/hooks/kiva/use-kiva-data-export.tsx +173 -0
- package/src/hooks/kiva/use-kiva-data-grid.tsx +4750 -0
- package/src/hooks/kiva/use-kiva-data-view-select.tsx +1273 -0
- package/src/hooks/kiva/use-kiva-form-view.tsx +1581 -0
- package/src/hooks/kiva/use-kiva-inventory.tsx +184 -0
- package/src/hooks/kiva/use-kiva-kanban.tsx +1750 -0
- package/src/hooks/kiva/use-kiva-map-view.tsx +2255 -0
- package/src/hooks/kiva/use-kiva-pivot-calendar.tsx +759 -0
- package/src/hooks/kiva/use-kiva-pivot-filter.tsx +1523 -0
- package/src/hooks/kiva/use-kiva-pivot-grid.tsx +543 -0
- package/src/hooks/kiva/use-kiva-tenant.tsx +313 -0
- package/src/hooks/kiva/use-parameter-persistence.ts +191 -0
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
// @ts-nocheck
|
|
4
|
+
/* eslint-disable */
|
|
5
|
+
/*
|
|
6
|
+
* Lives in `_internal/` so the registry scanner does not surface it as a
|
|
7
|
+
* standalone hook. Bundled as a transitive file under use-kiva-form-view.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
useCallback, useEffect, useRef, useState
|
|
12
|
+
} from 'react';
|
|
13
|
+
|
|
14
|
+
import { type RuleGroupTypeAny } from 'react-querybuilder';
|
|
15
|
+
|
|
16
|
+
import { evaluateJsonata } from '@kivacore/ui-pro-shared/components/kiva/jsonata-editor/lib/evaluate';
|
|
17
|
+
import {
|
|
18
|
+
computeDateBindings,
|
|
19
|
+
convertQueryToJsonata,
|
|
20
|
+
createHelperBindings
|
|
21
|
+
} from '@kivacore/ui-pro-shared/components/kiva/query-builder/lib/query-to-jsonata';
|
|
22
|
+
import {
|
|
23
|
+
type FieldActionBlockItem,
|
|
24
|
+
type FieldActionCondition,
|
|
25
|
+
type FieldActionPropertyOverrides,
|
|
26
|
+
type FieldActionStep,
|
|
27
|
+
type FormAction,
|
|
28
|
+
type FormActionTriggerType,
|
|
29
|
+
type FormCustomValidationRule
|
|
30
|
+
} from '@kivacore/ui-pro-shared/components/kiva/form-fields/types';
|
|
31
|
+
|
|
32
|
+
/*
|
|
33
|
+
* ---------------------------------------------------------------------------
|
|
34
|
+
* Public interface
|
|
35
|
+
* ---------------------------------------------------------------------------
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
export interface UseFormActionsOptions {
|
|
39
|
+
/** All form-level actions (across all trigger types) */
|
|
40
|
+
formActions?: FormAction[] | null;
|
|
41
|
+
/** Form-level custom validation rules evaluated on submit */
|
|
42
|
+
formCustomValidations?: FormCustomValidationRule[] | null;
|
|
43
|
+
/**
|
|
44
|
+
* Called when an action step writes a new value to a field.
|
|
45
|
+
* Wired to setValueExternal in use-kiva-form-view.
|
|
46
|
+
*/
|
|
47
|
+
onSetValue: (slug: string, value: unknown) => void;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface UseFormActionsResult {
|
|
51
|
+
/** Fire when the form finishes loading initial data. */
|
|
52
|
+
triggerFormLoad: (formValues: Record<string, unknown>) => void;
|
|
53
|
+
/**
|
|
54
|
+
* Fire just before the submit API call.
|
|
55
|
+
* Runs onFormBeforeSubmit blocks — field value mutations are applied via onSetValue.
|
|
56
|
+
* Awaitable: resolves when all blocks have run.
|
|
57
|
+
*/
|
|
58
|
+
triggerBeforeSubmit: (formValues: Record<string, unknown>) => Promise<void>;
|
|
59
|
+
/**
|
|
60
|
+
* Fire after a successful submit API call.
|
|
61
|
+
* `result` is the raw API response — available as `$result` in expressions.
|
|
62
|
+
*/
|
|
63
|
+
triggerAfterSubmit: (result: unknown, formValues: Record<string, unknown>) => void;
|
|
64
|
+
/**
|
|
65
|
+
* Evaluate form-level custom validation rules.
|
|
66
|
+
* Returns an array of error messages (empty = valid).
|
|
67
|
+
*/
|
|
68
|
+
validateFormLevel: (formValues: Record<string, unknown>) => Promise<string[]>;
|
|
69
|
+
/**
|
|
70
|
+
* Accumulated property overrides from form-level action runs.
|
|
71
|
+
* Merges into resolvedFields at lower priority than computed formulas.
|
|
72
|
+
*/
|
|
73
|
+
propertyOverrides: Map<string, FieldActionPropertyOverrides>;
|
|
74
|
+
/** Clear all accumulated property overrides. */
|
|
75
|
+
resetPropertyOverrides: () => void;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/*
|
|
79
|
+
* ---------------------------------------------------------------------------
|
|
80
|
+
* Hook
|
|
81
|
+
* ---------------------------------------------------------------------------
|
|
82
|
+
*/
|
|
83
|
+
|
|
84
|
+
export function useFormActionsEvaluator({
|
|
85
|
+
formActions,
|
|
86
|
+
formCustomValidations,
|
|
87
|
+
onSetValue
|
|
88
|
+
}: UseFormActionsOptions): UseFormActionsResult {
|
|
89
|
+
const helperBindingsRef = useRef<Record<string, unknown>>(
|
|
90
|
+
createHelperBindings() as Record<string, unknown>
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
const propertyOverridesRef = useRef<Map<string, FieldActionPropertyOverrides>>(new Map());
|
|
94
|
+
const [propertyOverrides, setPropertyOverrides] = useState<Map<string, FieldActionPropertyOverrides>>(
|
|
95
|
+
() => new Map()
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
const onSetValueRef = useRef(onSetValue);
|
|
99
|
+
const formActionsRef = useRef(formActions);
|
|
100
|
+
const formCustomValidationsRef = useRef(formCustomValidations);
|
|
101
|
+
|
|
102
|
+
useEffect(() => {
|
|
103
|
+
onSetValueRef.current = onSetValue;
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
useEffect(() => {
|
|
107
|
+
formActionsRef.current = formActions;
|
|
108
|
+
}, [formActions]);
|
|
109
|
+
|
|
110
|
+
useEffect(() => {
|
|
111
|
+
formCustomValidationsRef.current = formCustomValidations;
|
|
112
|
+
}, [formCustomValidations]);
|
|
113
|
+
|
|
114
|
+
/*
|
|
115
|
+
* Core: run all FormAction[] for a given triggerType
|
|
116
|
+
*/
|
|
117
|
+
const runTrigger = useCallback(async (
|
|
118
|
+
triggerType: FormActionTriggerType,
|
|
119
|
+
context: Record<string, unknown>
|
|
120
|
+
): Promise<void> => {
|
|
121
|
+
const actions = formActionsRef.current;
|
|
122
|
+
|
|
123
|
+
if (!actions || actions.length === 0) return;
|
|
124
|
+
|
|
125
|
+
const matching = actions.filter(a => a.triggerType === triggerType);
|
|
126
|
+
|
|
127
|
+
if (matching.length === 0) return;
|
|
128
|
+
|
|
129
|
+
const nowMs = Date.now();
|
|
130
|
+
const helperBindings = helperBindingsRef.current;
|
|
131
|
+
const nextOverrides = new Map(propertyOverridesRef.current);
|
|
132
|
+
const valueChanges: Array<{ slug: string; value: unknown }> = [];
|
|
133
|
+
|
|
134
|
+
for (const action of matching) {
|
|
135
|
+
const sortedBlocks = [...action.blocks].sort((a, b) => a.sortOrder - b.sortOrder);
|
|
136
|
+
|
|
137
|
+
for (const block of sortedBlocks) {
|
|
138
|
+
let matchedSteps: FieldActionStep[] | null = null;
|
|
139
|
+
|
|
140
|
+
for (const item of block.conditionalItems) {
|
|
141
|
+
const met = await evaluateCondition(item.condition, context, nowMs, helperBindings);
|
|
142
|
+
|
|
143
|
+
if (met) {
|
|
144
|
+
matchedSteps = item.actions;
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (matchedSteps === null) {
|
|
150
|
+
matchedSteps = block.elseActions;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
for (const step of matchedSteps) {
|
|
154
|
+
applyStep(step, nextOverrides, valueChanges);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
for (const step of block.unconditionalActions) {
|
|
158
|
+
applyStep(step, nextOverrides, valueChanges);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
for (const { slug, value } of valueChanges) {
|
|
164
|
+
onSetValueRef.current(slug, value);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (!mapsShallowEqual(propertyOverridesRef.current, nextOverrides)) {
|
|
168
|
+
propertyOverridesRef.current = nextOverrides;
|
|
169
|
+
setPropertyOverrides(new Map(nextOverrides));
|
|
170
|
+
}
|
|
171
|
+
}, []);
|
|
172
|
+
|
|
173
|
+
const triggerFormLoad = useCallback((formValues: Record<string, unknown>) => {
|
|
174
|
+
void runTrigger('onFormLoad', { ...formValues });
|
|
175
|
+
}, [runTrigger]);
|
|
176
|
+
|
|
177
|
+
const triggerBeforeSubmit = useCallback(async (formValues: Record<string, unknown>) => {
|
|
178
|
+
await runTrigger('onFormBeforeSubmit', { ...formValues });
|
|
179
|
+
}, [runTrigger]);
|
|
180
|
+
|
|
181
|
+
const triggerAfterSubmit = useCallback((result: unknown, formValues: Record<string, unknown>) => {
|
|
182
|
+
void runTrigger('onFormAfterSubmit', { ...formValues, $result: result });
|
|
183
|
+
}, [runTrigger]);
|
|
184
|
+
|
|
185
|
+
const validateFormLevel = useCallback(async (
|
|
186
|
+
formValues: Record<string, unknown>
|
|
187
|
+
): Promise<string[]> => {
|
|
188
|
+
const rules = formCustomValidationsRef.current;
|
|
189
|
+
|
|
190
|
+
if (!rules || rules.length === 0) return [];
|
|
191
|
+
|
|
192
|
+
const errors: string[] = [];
|
|
193
|
+
const nowMs = Date.now();
|
|
194
|
+
const helperBindings = helperBindingsRef.current;
|
|
195
|
+
const context = { values: formValues, ...formValues };
|
|
196
|
+
|
|
197
|
+
for (const rule of rules) {
|
|
198
|
+
if (!rule.expression?.trim()) continue;
|
|
199
|
+
|
|
200
|
+
const dateBindings = computeDateBindings([], nowMs);
|
|
201
|
+
const result = await evaluateJsonata(rule.expression, context, {
|
|
202
|
+
bindings: { ...helperBindings, ...dateBindings }
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
if (result.status === 'success' && result.result !== true) {
|
|
206
|
+
errors.push(rule.message || 'Form validation failed');
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return errors;
|
|
211
|
+
}, []);
|
|
212
|
+
|
|
213
|
+
const resetPropertyOverrides = useCallback(() => {
|
|
214
|
+
propertyOverridesRef.current = new Map();
|
|
215
|
+
setPropertyOverrides(new Map());
|
|
216
|
+
}, []);
|
|
217
|
+
|
|
218
|
+
return {
|
|
219
|
+
triggerFormLoad,
|
|
220
|
+
triggerBeforeSubmit,
|
|
221
|
+
triggerAfterSubmit,
|
|
222
|
+
validateFormLevel,
|
|
223
|
+
propertyOverrides,
|
|
224
|
+
resetPropertyOverrides
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/*
|
|
229
|
+
* ---------------------------------------------------------------------------
|
|
230
|
+
* Condition evaluation (shared with use-field-actions-evaluator pattern)
|
|
231
|
+
* ---------------------------------------------------------------------------
|
|
232
|
+
*/
|
|
233
|
+
|
|
234
|
+
async function evaluateCondition(
|
|
235
|
+
condition: FieldActionCondition,
|
|
236
|
+
context: Record<string, unknown>,
|
|
237
|
+
nowMs: number,
|
|
238
|
+
helperBindings: Record<string, unknown>
|
|
239
|
+
): Promise<boolean> {
|
|
240
|
+
if (!condition) return true;
|
|
241
|
+
|
|
242
|
+
let expression: string;
|
|
243
|
+
let dateBindings: ReturnType<typeof computeDateBindings> = {};
|
|
244
|
+
|
|
245
|
+
if (typeof condition === 'string') {
|
|
246
|
+
const trimmed = condition.trim();
|
|
247
|
+
|
|
248
|
+
if (!trimmed) return true;
|
|
249
|
+
expression = trimmed;
|
|
250
|
+
} else if (
|
|
251
|
+
typeof condition === 'object'
|
|
252
|
+
&& 'combinator' in condition
|
|
253
|
+
&& 'rules' in condition
|
|
254
|
+
&& Array.isArray(condition.rules)
|
|
255
|
+
&& condition.rules.length > 0
|
|
256
|
+
) {
|
|
257
|
+
const converted = convertQueryToJsonata(condition as RuleGroupTypeAny);
|
|
258
|
+
|
|
259
|
+
({ expression } = converted);
|
|
260
|
+
dateBindings = computeDateBindings(converted.dateBindings, nowMs);
|
|
261
|
+
} else {
|
|
262
|
+
return true;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const result = await evaluateJsonata(expression, context, {
|
|
266
|
+
bindings: { ...helperBindings, ...dateBindings }
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
return result.status === 'success' ? Boolean(result.result) : false;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/*
|
|
273
|
+
* ---------------------------------------------------------------------------
|
|
274
|
+
* Step dispatcher
|
|
275
|
+
* ---------------------------------------------------------------------------
|
|
276
|
+
*/
|
|
277
|
+
|
|
278
|
+
function applyStep(
|
|
279
|
+
step: FieldActionStep,
|
|
280
|
+
overrides: Map<string, FieldActionPropertyOverrides>,
|
|
281
|
+
valueChanges: Array<{ slug: string; value: unknown }>
|
|
282
|
+
): void {
|
|
283
|
+
switch (step.method) {
|
|
284
|
+
case 'setFieldValue':
|
|
285
|
+
valueChanges.push({ slug: step.fieldSlug, value: step.value });
|
|
286
|
+
break;
|
|
287
|
+
|
|
288
|
+
case 'setFieldValues':
|
|
289
|
+
for (const { fieldSlug, value } of step.fields) {
|
|
290
|
+
valueChanges.push({ slug: fieldSlug, value });
|
|
291
|
+
}
|
|
292
|
+
break;
|
|
293
|
+
|
|
294
|
+
case 'clearFieldValue':
|
|
295
|
+
valueChanges.push({ slug: step.fieldSlug, value: null });
|
|
296
|
+
break;
|
|
297
|
+
|
|
298
|
+
case 'showField':
|
|
299
|
+
mergeOverride(overrides, step.fieldSlug, { hidden: false });
|
|
300
|
+
break;
|
|
301
|
+
|
|
302
|
+
case 'hideField':
|
|
303
|
+
mergeOverride(overrides, step.fieldSlug, { hidden: true });
|
|
304
|
+
break;
|
|
305
|
+
|
|
306
|
+
case 'setFieldRequired':
|
|
307
|
+
mergeOverride(overrides, step.fieldSlug, { required: step.required });
|
|
308
|
+
break;
|
|
309
|
+
|
|
310
|
+
case 'setFieldDisabled':
|
|
311
|
+
mergeOverride(overrides, step.fieldSlug, { disabled: step.disabled });
|
|
312
|
+
break;
|
|
313
|
+
|
|
314
|
+
case 'setFieldReadOnly':
|
|
315
|
+
mergeOverride(overrides, step.fieldSlug, { readOnly: step.readOnly });
|
|
316
|
+
break;
|
|
317
|
+
|
|
318
|
+
default:
|
|
319
|
+
break;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/*
|
|
324
|
+
* ---------------------------------------------------------------------------
|
|
325
|
+
* Helpers
|
|
326
|
+
* ---------------------------------------------------------------------------
|
|
327
|
+
*/
|
|
328
|
+
|
|
329
|
+
function mergeOverride(
|
|
330
|
+
map: Map<string, FieldActionPropertyOverrides>,
|
|
331
|
+
slug: string,
|
|
332
|
+
patch: Partial<FieldActionPropertyOverrides>
|
|
333
|
+
): void {
|
|
334
|
+
const existing = map.get(slug) ?? {};
|
|
335
|
+
|
|
336
|
+
map.set(slug, { ...existing, ...patch });
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
function mapsShallowEqual(
|
|
340
|
+
a: Map<string, FieldActionPropertyOverrides>,
|
|
341
|
+
b: Map<string, FieldActionPropertyOverrides>
|
|
342
|
+
): boolean {
|
|
343
|
+
if (a.size !== b.size) return false;
|
|
344
|
+
|
|
345
|
+
for (const [slug, aVal] of a.entries()) {
|
|
346
|
+
const bVal = b.get(slug);
|
|
347
|
+
|
|
348
|
+
if (!bVal) return false;
|
|
349
|
+
|
|
350
|
+
if (
|
|
351
|
+
aVal.hidden !== bVal.hidden
|
|
352
|
+
|| aVal.required !== bVal.required
|
|
353
|
+
|| aVal.disabled !== bVal.disabled
|
|
354
|
+
|| aVal.readOnly !== bVal.readOnly
|
|
355
|
+
) {
|
|
356
|
+
return false;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
return true;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
export type { FieldActionBlockItem };
|