Skip to content

Commit

Permalink
fix: added validation messages for translations + custom tokens support
Browse files Browse the repository at this point in the history
  • Loading branch information
pksorensen committed Sep 16, 2024
1 parent 5703df7 commit 6e4075f
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 12 deletions.
7 changes: 7 additions & 0 deletions packages/core/src/model/QuickFormModel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { EndingModel, IntroModel, SlideModel, SubmitModel } from "./index";

export type QuickFormModel = {
validation?: {
messages?: {
"NOT_ALL_QUESTIONS_ANSWERED"?: string,
"SOME_QUESTIONS_HAS_EMPTY_ANSWER"?: string,
"SOME_QUESTIONS_HAVE_FAILED_VALIDATION"?: string;
}
},
intro?: IntroModel;
slides: SlideModel[];
submit: SubmitModel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import { QuickFormSubmitDefinition } from "./QuickFormSubmitDefinition";
import { LayoutDefinition } from "./Layout";

export type QuickFormDefinition = {
validation?: {
messages?: {
"NOT_ALL_QUESTIONS_ANSWERED"?: string,
"SOME_QUESTIONS_HAS_EMPTY_ANSWER"?: string,
"SOME_QUESTIONS_HAVE_FAILED_VALIDATION"?: string;
}
},
intro?: IntroModel;
questions: QuickFormQuestionsDefinition,
submit: QuickFormSubmitDefinition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ const transformJSONInput: QuickFormModelTransformer = (definition, payload): Qui
}

return {

validation: definition.validation,
intro: definition.intro,
ending: definition.ending,
submit: handleSubmit(definition.submit, payload?.submitFields),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class NavigationActionHandler {
}

// Check all visible questions for answered and validity
const validationResult = this.validateQuestions(visibleQuestions);
const validationResult = this.validateQuestions(visibleQuestions, state);
console.log("reducer validationResult", validationResult)
if (validationResult.isValid) {
return this.computeProgress(NavigationActionHandler.handleSlideChange({ ...state, errorMsg: "" }, 'next'));
Expand All @@ -62,21 +62,21 @@ export class NavigationActionHandler {
}
};

static validateQuestions = (questions: QuestionModel[]) => {
static validateQuestions = (questions: QuestionModel[], state: QuickformState) => {
const logger = resolveQuickFormService("logger");
if (questions.some((q: { answered: boolean; }) => q.answered === false)) {
return { isValid: false, errorMsg: "Not all questions have been answered." };
return { isValid: false, errorMsg: state.data.validation?.messages?.NOT_ALL_QUESTIONS_ANSWERED ?? "Not all questions have been answered." };
}
if (questions.some((q: { output: any; }) => q.output === '' || typeof q.output === "undefined")) {
return { isValid: false, errorMsg: "Some questions are missing outputs." };
return { isValid: false, errorMsg: state.data.validation?.messages?.SOME_QUESTIONS_HAS_EMPTY_ANSWER ?? "Some questions are missing outputs." };
}
if (questions.some(q => !q.validationResult || (!q.validationResult?.isValidating && q.validationResult?.isValid === false))) {
//TODO, if its validating, should that block us from moving to next?
//For now i decided to allow it to move to next - before submitting we can always go back and error hints can be shown in ui.
//I dont think its critical that it allow to move on. Right now this function is only called when validating if it can go to next slide
//it has nothing to do with actually validation of the form.
logger.log("Some questions have failed validation. {questions}", [questions, JSON.stringify(questions)]);
return { isValid: false, errorMsg: "Some questions have failed validation." };
return { isValid: false, errorMsg: state.data.validation?.messages?.SOME_QUESTIONS_HAVE_FAILED_VALIDATION ?? "Some questions have failed validation." };
}

return { isValid: true, errorMsg: "" };
Expand Down
41 changes: 34 additions & 7 deletions packages/designer/src/Components/Views/QuickFormSettingsView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,30 @@ const quickformSettingsSchema = {
}
}
}
} as { label: string, uiSchema: any, schema: JSONSchema7 };
} as { label: string, uiSchema: any, schema: JSONSchema7 & { properties: { tokens: JSONSchema7 } } };

export function registerToken(key: keyof typeof defaultQuickFormTokens, title: string, description: string, widget: string) {
export function registerToken<TokenRegistry = typeof defaultQuickFormTokens>(key: keyof TokenRegistry, title: string, description: string, widget: string, defaultValue?: any, defaultTokenKey?: keyof TokenRegistry) {

const tokensSchema = quickformSettingsSchema.schema.properties?.tokens! as JSONSchema7;
const uiSchema = quickformSettingsSchema.uiSchema.tokens;
if (tokensSchema && tokensSchema.properties) {
tokensSchema.properties[key] = {
let defaultTokenValue = defaultTokenKey && defaultTokenKey in defaultQuickFormTokens ?
defaultQuickFormTokens[defaultTokenKey as keyof typeof defaultQuickFormTokens] ?? defaultValue
: defaultValue

tokensSchema.properties[key as string] = {
title,
description,
type: "string",
default: defaultQuickFormTokens[key]

default:
key in defaultQuickFormTokens ?
defaultQuickFormTokens[key as keyof typeof defaultQuickFormTokens] ?? defaultTokenValue
: defaultTokenValue
}
if (defaultTokenKey) {
//@ts-ignore
tokensSchema.properties[key]["x-default-token"] = defaultTokenKey;
}
}
if (widget) {
Expand Down Expand Up @@ -106,15 +118,30 @@ export const QuickFormSettingsView = () => {
const styles = useViewStyles();
const quickformSettingsStyles = useQuickformSettingsStyles();
const PreviewComponent = Controls["QuickFormSettingsViewPreviewComponent"];
console.log("quickformpayloadV1", JSON.stringify(quickformpayload.layout));
console.log("ENUM", Object.values(IconEnum));

const formData = {
...quickformpayload.layout,
tokens: {
...(Object.fromEntries(Object.entries(quickformSettingsSchema.schema.properties.tokens.properties ?? {})
.filter(([key, value]) => typeof value === "object" && "x-default-token" in value) //@ts-ignore
.map(([key, value]) => [key, quickformpayload.layout?.tokens[value["x-default-token"]]])
)),
...quickformpayload.layout?.tokens ?? {}
}



}

console.log("Settings Data", [quickformSettingsSchema.schema.properties.tokens.properties, JSON.stringify(formData, null, 4)]);

return (
<div className={mergeClasses(styles.section, quickformSettingsStyles.container)}>
<div className={mergeClasses(styles.sectionSlim, styles.section)}>
<Form templates={{ FieldTemplate, BaseInputTemplate }}
validator={validator}
{...quickformSettingsSchema}
formData={quickformpayload.layout}
formData={formData}
onChange={(a, b) => {
console.log("change", [a, b]);
dispatch(old => {
Expand Down

0 comments on commit 6e4075f

Please sign in to comment.