|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * @object-ui/core - Action Runner |
| 11 | + * |
| 12 | + * Executes actions defined in ActionSchema and EventHandler. |
| 13 | + */ |
| 14 | + |
| 15 | +import { ExpressionEvaluator } from '../evaluator/ExpressionEvaluator'; |
| 16 | + |
| 17 | +export interface ActionResult { |
| 18 | + success: boolean; |
| 19 | + data?: any; |
| 20 | + error?: string; |
| 21 | + reload?: boolean; |
| 22 | + close?: boolean; |
| 23 | + redirect?: string; |
| 24 | +} |
| 25 | + |
| 26 | +export interface ActionContext { |
| 27 | + data?: Record<string, any>; |
| 28 | + record?: any; |
| 29 | + user?: any; |
| 30 | + [key: string]: any; |
| 31 | +} |
| 32 | + |
| 33 | +export type ActionHandler = ( |
| 34 | + action: any, |
| 35 | + context: ActionContext |
| 36 | +) => Promise<ActionResult> | ActionResult; |
| 37 | + |
| 38 | +export class ActionRunner { |
| 39 | + private handlers = new Map<string, ActionHandler>(); |
| 40 | + private evaluator: ExpressionEvaluator; |
| 41 | + private context: ActionContext; |
| 42 | + |
| 43 | + constructor(context: ActionContext = {}) { |
| 44 | + this.context = context; |
| 45 | + this.evaluator = new ExpressionEvaluator(context); |
| 46 | + } |
| 47 | + |
| 48 | + registerHandler(actionName: string, handler: ActionHandler): void { |
| 49 | + this.handlers.set(actionName, handler); |
| 50 | + } |
| 51 | + |
| 52 | + async execute(action: any): Promise<ActionResult> { |
| 53 | + try { |
| 54 | + if (action.condition) { |
| 55 | + const shouldExecute = this.evaluator.evaluateCondition(action.condition); |
| 56 | + if (!shouldExecute) { |
| 57 | + return { success: false, error: 'Action condition not met' }; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + if (action.disabled) { |
| 62 | + const isDisabled = typeof action.disabled === 'string' |
| 63 | + ? this.evaluator.evaluateCondition(action.disabled) |
| 64 | + : action.disabled; |
| 65 | + |
| 66 | + if (isDisabled) { |
| 67 | + return { success: false, error: 'Action is disabled' }; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + if (action.type === 'action' || action.actionType) { |
| 72 | + return await this.executeActionSchema(action); |
| 73 | + } else if (action.type === 'navigation' || action.navigate) { |
| 74 | + return await this.executeNavigation(action); |
| 75 | + } else if (action.type === 'api' || action.api) { |
| 76 | + return await this.executeAPI(action); |
| 77 | + } else if (action.onClick) { |
| 78 | + await action.onClick(); |
| 79 | + return { success: true }; |
| 80 | + } |
| 81 | + |
| 82 | + return { success: false, error: 'Unknown action type' }; |
| 83 | + } catch (error) { |
| 84 | + return { success: false, error: (error as Error).message }; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private async executeActionSchema(action: any): Promise<ActionResult> { |
| 89 | + const result: ActionResult = { success: true }; |
| 90 | + |
| 91 | + if (action.confirmText) { |
| 92 | + const confirmed = await this.showConfirmation(action.confirmText); |
| 93 | + if (!confirmed) { |
| 94 | + return { success: false, error: 'Action cancelled by user' }; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if (action.api) { |
| 99 | + const apiResult = await this.executeAPI(action); |
| 100 | + if (!apiResult.success) return apiResult; |
| 101 | + result.data = apiResult.data; |
| 102 | + } |
| 103 | + |
| 104 | + if (action.onClick) { |
| 105 | + await action.onClick(); |
| 106 | + } |
| 107 | + |
| 108 | + result.reload = action.reload !== false; |
| 109 | + result.close = action.close !== false; |
| 110 | + |
| 111 | + if (action.redirect) { |
| 112 | + result.redirect = this.evaluator.evaluate(action.redirect) as string; |
| 113 | + } |
| 114 | + |
| 115 | + return result; |
| 116 | + } |
| 117 | + |
| 118 | + private async executeNavigation(action: any): Promise<ActionResult> { |
| 119 | + const nav = action.navigate || action; |
| 120 | + const to = this.evaluator.evaluate(nav.to) as string; |
| 121 | + |
| 122 | + if (nav.external) { |
| 123 | + window.open(to, '_blank'); |
| 124 | + } else { |
| 125 | + return { success: true, redirect: to }; |
| 126 | + } |
| 127 | + |
| 128 | + return { success: true }; |
| 129 | + } |
| 130 | + |
| 131 | + private async executeAPI(action: any): Promise<ActionResult> { |
| 132 | + const apiConfig = action.api; |
| 133 | + |
| 134 | + if (typeof apiConfig === 'string') { |
| 135 | + try { |
| 136 | + const response = await fetch(apiConfig, { |
| 137 | + method: action.method || 'POST', |
| 138 | + headers: { 'Content-Type': 'application/json' }, |
| 139 | + body: JSON.stringify(this.context.data || {}) |
| 140 | + }); |
| 141 | + |
| 142 | + if (!response.ok) { |
| 143 | + throw new Error(`HTTP ${response.status}: ${response.statusText}`); |
| 144 | + } |
| 145 | + |
| 146 | + const data = await response.json(); |
| 147 | + return { success: true, data }; |
| 148 | + } catch (error) { |
| 149 | + return { success: false, error: (error as Error).message }; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + return { success: false, error: 'Complex API configuration not yet implemented' }; |
| 154 | + } |
| 155 | + |
| 156 | + private async showConfirmation(message: string): Promise<boolean> { |
| 157 | + const evaluatedMessage = this.evaluator.evaluate(message) as string; |
| 158 | + return window.confirm(evaluatedMessage); |
| 159 | + } |
| 160 | + |
| 161 | + updateContext(newContext: Partial<ActionContext>): void { |
| 162 | + this.context = { ...this.context, ...newContext }; |
| 163 | + this.evaluator.updateContext(newContext); |
| 164 | + } |
| 165 | + |
| 166 | + getContext(): ActionContext { |
| 167 | + return this.context; |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +export async function executeAction( |
| 172 | + action: any, |
| 173 | + context: ActionContext = {} |
| 174 | +): Promise<ActionResult> { |
| 175 | + const runner = new ActionRunner(context); |
| 176 | + return await runner.execute(action); |
| 177 | +} |
0 commit comments