diff --git a/packages/core/src/state/QuickFormContext.tsx b/packages/core/src/state/QuickFormContext.tsx index 3b9ca02..9976a4e 100644 --- a/packages/core/src/state/QuickFormContext.tsx +++ b/packages/core/src/state/QuickFormContext.tsx @@ -2,7 +2,8 @@ import React, { useContext } from "react"; import { QuickformState, defaultState } from "./QuickformState"; import { QuickformAction } from "./index"; -import { SlideModel } from "../model"; +import { QuickFormDefinition, SlideModel } from "../model"; +import { ServerActionSubmitHandler } from "./action-handlers/SubmitActionHandler"; interface IQuickFormContext { state: QuickformState; @@ -15,7 +16,7 @@ interface IQuickFormContext { setErrorMsg: (msg: string) => void; isFirstQuestionInCurrentSlide: (questionLogicalName: string) => boolean; getCurrentSlide: () => SlideModel; - onSubmitAsync?: (formdata: any) => Promise; + onSubmitAsync?: ServerActionSubmitHandler, addPayloadAugmenter: (augmenter: (payload: any) => any) => void; removePayloadAugmenter: (augmenter: (payload: any) => any) => void; cssVariables: { [key: string]: string }; @@ -35,7 +36,9 @@ export const QuickFormContext = React.createContext( getCurrentSlide: () => ( { questions: [], rows: [], isAnswered: false, addQuestion: () => ({ type: "question", ref: "" }) } ), - onSubmitAsync: async (formdata) => { return "" }, + onSubmitAsync: async (formdata) => { + return {} as Partial + }, addPayloadAugmenter: () => { }, removePayloadAugmenter: () => { }, cssVariables: {} diff --git a/packages/core/src/state/QuickformAction.ts b/packages/core/src/state/QuickformAction.ts index 9856817..b70bf2c 100644 --- a/packages/core/src/state/QuickformAction.ts +++ b/packages/core/src/state/QuickformAction.ts @@ -1,5 +1,6 @@ import { ValidationResult } from "../model/ValidationResult"; import { SubmitStatus } from "../model/SubmitStatus"; +import { QuickFormDefinition } from "../model/json-definitions/QuickFormDefinition"; export type QuickformAnswerQuestionAction = { type: 'ANSWER_QUESTION'; logicalName: string; output: string; dispatch: React.Dispatch, intermediate?: boolean, validationResult?: ValidationResult }; @@ -16,6 +17,7 @@ export type QuickformAction = | { type: 'SUBMIT', dispatch: React.Dispatch, id: string } | { type: 'SET_INTRO_VISITED' } | { type: 'GO_TO_ENDING' } - | { type: 'ADD_PAYLOAD_AUGMENTER', augmenter: (payload:any)=>any } + | { type: 'ADD_PAYLOAD_AUGMENTER', augmenter: (payload: any) => any } | { type: 'REMOVE_PAYLOAD_AUGMENTER', augmenter: (payload: any) => any } + | { type: 'UPDATE_QUICKFORM_DEFINITION', definition: QuickFormDefinition } ; diff --git a/packages/core/src/state/QuickformReducer.ts b/packages/core/src/state/QuickformReducer.ts index 06c959f..0a60667 100644 --- a/packages/core/src/state/QuickformReducer.ts +++ b/packages/core/src/state/QuickformReducer.ts @@ -19,7 +19,21 @@ export const quickformReducer = (state: QuickformState, action: QuickformAction) } switch (action.type) { + case 'UPDATE_QUICKFORM_DEFINITION': { + + console.log("Reducer", [state, action.definition]); + state = { + ...state, data: { + ...state.data, + ending: { + ...state.data.ending, + ...(action.definition.ending ?? {}) + }, + } +} + return state; + } case 'ADD_PAYLOAD_AUGMENTER': { state.payloadAugments = [...state.payloadAugments, action.augmenter]; return state; diff --git a/packages/core/src/state/action-handlers/SubmitActionHandler.ts b/packages/core/src/state/action-handlers/SubmitActionHandler.ts index 212e18b..adbeeab 100644 --- a/packages/core/src/state/action-handlers/SubmitActionHandler.ts +++ b/packages/core/src/state/action-handlers/SubmitActionHandler.ts @@ -1,14 +1,19 @@ +import { QuickFormDefinition } from "../../model/json-definitions/QuickFormDefinition"; import { resolveQuickFormService } from "../../services/QuickFormServices"; import { QuickformAction, QuickformState } from "../index"; +export type ServerActionSubmitHandler = (data: any) => Promise>; export class SubmitActionHandler { - static submit = async (state: QuickformState, dispatch: React.Dispatch, onSubmitAsync?: (data:any)=>Promise) => { + static submit = async (state: QuickformState, dispatch: React.Dispatch, onSubmitAsync?: ServerActionSubmitHandler) => { try { const body = this.generatePayload(state); if (onSubmitAsync) { const rsp = await onSubmitAsync(body); + + dispatch({ type: "UPDATE_QUICKFORM_DEFINITION", definition: rsp }); + } else { let rsp = await fetch(state.data.submit.submitUrl, { diff --git a/packages/designer/src/Components/Views/QuickFormSourceView.tsx b/packages/designer/src/Components/Views/QuickFormSourceView.tsx index e6ce319..7d5fc2b 100644 --- a/packages/designer/src/Components/Views/QuickFormSourceView.tsx +++ b/packages/designer/src/Components/Views/QuickFormSourceView.tsx @@ -31,14 +31,14 @@ export const QuickFormSourceView = () => { }, [monaco]); return ( -
+
{data && { updateQuickFormPayload(old => { try { if (!value) return; return JSON.parse(value); } catch (error) { } }); }} + onChange={(value) => { updateQuickFormPayload(old => { try { if (!value) return; return JSON.parse(value); } catch (error) { } return old }); }} onMount={handleEditorDidMount} defaultLanguage="json" value={data}