Skip to content

Commit 0cd96b4

Browse files
committed
feat: Added submit payload augmenters
1 parent f533816 commit 0cd96b4

File tree

6 files changed

+34
-4
lines changed

6 files changed

+34
-4
lines changed

packages/core/src/state/QuickFormContext.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ interface IQuickFormContext {
1616
isFirstQuestionInCurrentSlide: (questionLogicalName: string) => boolean;
1717
getCurrentSlide: () => SlideModel;
1818
onSubmitAsync?: (formdata: any) => Promise<string>;
19+
addPayloadAugmenter: (augmenter: (payload: any) => any) => void;
20+
removePayloadAugmenter: (augmenter: (payload: any) => any) => void;
1921
cssVariables: { [key: string]: string };
2022
}
2123

@@ -34,6 +36,8 @@ export const QuickFormContext = React.createContext<IQuickFormContext>(
3436
{ questions: [], rows: [], isAnswered: false, addQuestion: () => ({ type: "question", ref: "" }) }
3537
),
3638
onSubmitAsync: async (formdata) => { return "" },
39+
addPayloadAugmenter: () => { },
40+
removePayloadAugmenter: () => { },
3741
cssVariables: {}
3842
}
3943
);

packages/core/src/state/QuickformAction.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@ export type QuickformAction =
1515
| { type: 'SET_SUBMIT_STATUS', status: SubmitStatus }
1616
| { type: 'SUBMIT', dispatch: React.Dispatch<QuickformAction>, id: string }
1717
| { type: 'SET_INTRO_VISITED' }
18-
| { type: 'GO_TO_ENDING' };
18+
| { type: 'GO_TO_ENDING' }
19+
| { type: 'ADD_PAYLOAD_AUGMENTER', augmenter }
20+
| { type: 'REMOVE_PAYLOAD_AUGMENTER', augmenter }
21+
;

packages/core/src/state/QuickformProvider.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ export const QuickFormProvider: React.FC<QuickFormProviderProps> = (
5555
const isFirstQuestionInCurrentSlide = (questionLogicalName: string) => { return isFirstQInCurrentSlide(questionLogicalName, state); }
5656
const getCurrentSlide = () => (state.slides[state.currIdx]);
5757

58+
const addPayloadAugmenter = (augmenter: (payload: any) => any) => {
59+
dispatch({ type: 'ADD_PAYLOAD_AUGMENTER', augmenter })
60+
}
61+
const removePayloadAugmenter = (augmenter: (payload: any) => any) => {
62+
dispatch({ type: 'REMOVE_PAYLOAD_AUGMENTER', augmenter })
63+
}
64+
5865
return (
5966
<QuickFormContext.Provider value={{
6067
state,
@@ -68,7 +75,9 @@ export const QuickFormProvider: React.FC<QuickFormProviderProps> = (
6875
isFirstQuestionInCurrentSlide,
6976
getCurrentSlide,
7077
onSubmitAsync,
71-
cssVariables
78+
cssVariables,
79+
removePayloadAugmenter,
80+
addPayloadAugmenter
7281
}}>
7382

7483
{asContainer ? (

packages/core/src/state/QuickformReducer.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ export const quickformReducer = (state: QuickformState, action: QuickformAction)
2020

2121
switch (action.type) {
2222

23+
case 'ADD_PAYLOAD_AUGMENTER': {
24+
state.payloadAugments = [...state.payloadAugments, action.augmenter];
25+
return state;
26+
}
27+
case 'REMOVE_PAYLOAD_AUGMENTER': {
28+
state.payloadAugments = state.payloadAugments.filter(augmenter => augmenter !== action.augmenter);
29+
return state;
30+
}
31+
2332
case 'ANSWER_QUESTION': {
2433

2534
if (span) {

packages/core/src/state/QuickformState.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ export type QuickformState = {
2020
slides: SlideModel[];
2121
submitStatus: SubmitStatus;
2222
totalSteps: number;
23-
classes: Partial<QuickformClassNames>
23+
classes: Partial<QuickformClassNames>,
24+
payloadAugments: Array<(payload: any) => any>
2425
}
2526

2627
export const defaultState = (data: QuickFormModel = defaultData, layout?: LayoutDefinition): QuickformState => {
@@ -42,6 +43,7 @@ export const defaultState = (data: QuickFormModel = defaultData, layout?: Layout
4243
slides: data.slides,
4344
submitStatus: { isSubmitting: false, isSubmitError: false, isSubmitSuccess: false },
4445
totalSteps: data.slides.length,
46+
payloadAugments:[]
4547
};
4648

4749
return defState;

packages/core/src/state/action-handlers/SubmitActionHandler.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class SubmitActionHandler {
4242
static generatePayload = (state: QuickformState): Record<string, any> => {
4343

4444
const logger = resolveQuickFormService("logger");
45-
const payload: Record<string, any> = {};
45+
let payload: Record<string, any> = {};
4646
for (var slide of state.slides) {
4747
slide.questions.forEach(q => {
4848
payload[q.logicalName!] = q.output;
@@ -63,6 +63,9 @@ export class SubmitActionHandler {
6363

6464
payload["submitFields"][q.logicalName] = value;
6565
}
66+
for (let augmenter of state.payloadAugments) {
67+
payload = augmenter(payload);
68+
}
6669

6770
return payload;
6871
}

0 commit comments

Comments
 (0)