From ad9453e1f4bf7d299c1c6d3205bbe246e6478ea0 Mon Sep 17 00:00:00 2001 From: Thomas Lathuiliere Date: Wed, 30 Aug 2023 19:57:33 +0200 Subject: [PATCH] feat(AstBuilder): add save method --- packages/app-builder/src/models/ast-node.ts | 4 ++-- .../i/$iterationId/edit2.rules.$ruleId.tsx | 20 +++++++++++-------- .../src/services/editor/ast-editor.ts | 9 +++++++++ 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/packages/app-builder/src/models/ast-node.ts b/packages/app-builder/src/models/ast-node.ts index e09795c6..d0e47132 100644 --- a/packages/app-builder/src/models/ast-node.ts +++ b/packages/app-builder/src/models/ast-node.ts @@ -1,12 +1,12 @@ import { type NodeDto } from '@marble-api'; import * as R from 'remeda'; -export interface AstNode { +export type AstNode = { name: string | null; constant?: ConstantType; children: AstNode[]; namedChildren: Record; -} +}; export const undefinedAstNodeName = 'Undefined'; diff --git a/packages/app-builder/src/routes/__builder/scenarios/$scenarioId/i/$iterationId/edit2.rules.$ruleId.tsx b/packages/app-builder/src/routes/__builder/scenarios/$scenarioId/i/$iterationId/edit2.rules.$ruleId.tsx index caed8072..fcec9fc0 100644 --- a/packages/app-builder/src/routes/__builder/scenarios/$scenarioId/i/$iterationId/edit2.rules.$ruleId.tsx +++ b/packages/app-builder/src/routes/__builder/scenarios/$scenarioId/i/$iterationId/edit2.rules.$ruleId.tsx @@ -92,13 +92,11 @@ export async function action({ request, params }: ActionArgs) { try { const ruleId = fromParams(params, 'ruleId'); - const expression = (await request.json()) as { - astNode: AstNode; - }; + const astNode = (await request.json()) as AstNode; await editor.saveRule({ ruleId, - astNode: expression.astNode, + astNode, }); setToastMessage(session, { @@ -109,7 +107,7 @@ export async function action({ request, params }: ActionArgs) { { success: true as const, error: null, - values: expression, + astNode, }, { headers: { 'Set-Cookie': await commitSession(session) } } ); @@ -122,7 +120,7 @@ export async function action({ request, params }: ActionArgs) { { success: false as const, error: null, - values: null, + astNode: null, }, { headers: { 'Set-Cookie': await commitSession(session) } } ); @@ -146,7 +144,7 @@ export default function RuleEdit() { identifiers, operators, onSave: (astNodeToSave: AstNode) => { - fetcher.submit(JSON.stringify(astNodeToSave), { + fetcher.submit(astNodeToSave, { method: 'PATCH', encType: 'application/json', }); @@ -183,7 +181,13 @@ export default function RuleEdit() {
-
diff --git a/packages/app-builder/src/services/editor/ast-editor.ts b/packages/app-builder/src/services/editor/ast-editor.ts index 07ab1f01..c34ac63a 100644 --- a/packages/app-builder/src/services/editor/ast-editor.ts +++ b/packages/app-builder/src/services/editor/ast-editor.ts @@ -72,6 +72,7 @@ export interface AstBuilder { setOperator(nodeId: string, name: string): void; appendChild(nodeId: string, childAst: AstNode): void; remove(nodeId: string): void; + save(): void; } export function useAstBuilder({ @@ -79,6 +80,7 @@ export function useAstBuilder({ validation, identifiers, operators, + onSave, }: { ast: AstNode; validation: NodeEvaluation; @@ -204,6 +206,12 @@ export function useAstBuilder({ // Todo: debonced save }, []); + const save = useCallback(() => { + onSave(adaptAstNodeFromEditorViewModel(astViewModel)); + + // Todo: debonced save + }, [astViewModel, onSave]); + return { astViewModel, identifiers, @@ -213,5 +221,6 @@ export function useAstBuilder({ setOperator, appendChild, remove, + save, }; }