Skip to content

Commit

Permalink
fix: Added such ending slide is updated upon submit. Submit handler c…
Browse files Browse the repository at this point in the history
…an return updated information
  • Loading branch information
pksorensen committed Aug 26, 2024
1 parent f9b6844 commit 7bcb788
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 deletions.
9 changes: 6 additions & 3 deletions packages/core/src/state/QuickFormContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,7 +16,7 @@ interface IQuickFormContext {
setErrorMsg: (msg: string) => void;
isFirstQuestionInCurrentSlide: (questionLogicalName: string) => boolean;
getCurrentSlide: () => SlideModel;
onSubmitAsync?: (formdata: any) => Promise<string>;
onSubmitAsync?: ServerActionSubmitHandler,
addPayloadAugmenter: (augmenter: (payload: any) => any) => void;
removePayloadAugmenter: (augmenter: (payload: any) => any) => void;
cssVariables: { [key: string]: string };
Expand All @@ -35,7 +36,9 @@ export const QuickFormContext = React.createContext<IQuickFormContext>(
getCurrentSlide: () => (
{ questions: [], rows: [], isAnswered: false, addQuestion: () => ({ type: "question", ref: "" }) }
),
onSubmitAsync: async (formdata) => { return "" },
onSubmitAsync: async (formdata) => {
return {} as Partial<QuickFormDefinition>
},
addPayloadAugmenter: () => { },
removePayloadAugmenter: () => { },
cssVariables: {}
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/state/QuickformAction.ts
Original file line number Diff line number Diff line change
@@ -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<QuickformAction>, intermediate?: boolean, validationResult?: ValidationResult };

Expand All @@ -16,6 +17,7 @@ export type QuickformAction =
| { type: 'SUBMIT', dispatch: React.Dispatch<QuickformAction>, 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 }
;
14 changes: 14 additions & 0 deletions packages/core/src/state/QuickformReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Partial<QuickFormDefinition>>;
export class SubmitActionHandler {
static submit = async (state: QuickformState, dispatch: React.Dispatch<QuickformAction>, onSubmitAsync?: (data:any)=>Promise<string>) => {
static submit = async (state: QuickformState, dispatch: React.Dispatch<QuickformAction>, 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, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ export const QuickFormSourceView = () => {
}, [monaco]);

return (
<div style={{ height: height, width: "100%" }} ref={div}>
<div style={{ height: "100%", width: "100%" }} ref={div}>
{data &&
<Editor
options={{
automaticLayout: true,
scrollBeyondLastLine: false
}}
onChange={(value) => { 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}
Expand Down

0 comments on commit 7bcb788

Please sign in to comment.