Skip to content

Commit

Permalink
fix: (wip) - validation and issues with finding correct
Browse files Browse the repository at this point in the history
  • Loading branch information
pksorensen committed Oct 21, 2024
1 parent a2d59c8 commit 5e4fd64
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 29 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/components/icons/ImArrowRightIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { quickformtokens } from "../../style/quickFormTokensDefinition";

export const ImArrowRightIcon: React.FC<IconProps> = ({ size = '20px', style }) => {
return (
<svg style={{ fill: quickformtokens.onSurface, ... style }} width={size} stroke="currentColor" fill="currentColor" stroke-width="0" version="1.1" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg">
<svg style={{ fill: quickformtokens.onSurface, ... style }} width={size} stroke="currentColor" fill="currentColor" strokeWidth="0" version="1.1" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg">
<path d="M15.5 8l-7.5-7.5v4.5h-8v6h8v4.5z"></path>
</svg>
//<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function mapJsonQuestionToModelQuestion(questionKey: string, question: QuestionJ

return {
answered: hasDefaultValueOrPayload,
isRequired: question.isRequired ?? true,
isRequired: question.isRequired ?? false,
dataType: question.dataType ?? "string",
inputProperties: parseInputProperties(question),
inputType: question.inputType ?? "text",
Expand Down
14 changes: 12 additions & 2 deletions packages/core/src/state/QuickformReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,15 @@ export const quickformReducer = (state: QuickformState, action: QuickformAction)

if (!isValidating && updatedState.onValidationCompleteCallback) {
updatedState.onValidationCompleteCallback(updatedState);
updatedState.onValidationCompleteCallback = undefined;
}

return updatedState;
}
case 'ON_VALIDATION_COMPLETED': {
let isValidating = getAllQuestions(state).some(q => q.validationResult?.isValidating ?? false);
logger.log("QuickForm Reducer {action} - {isValidating}: {logicalNames}", action.type, isValidating,
getAllQuestions(state).filter(q => q.validationResult?.isValidating ?? false).map(c => c.logicalName).join(','));
if (isValidating) {
return { ...state, onValidationCompleteCallback: action.callback }
}
Expand All @@ -135,7 +138,7 @@ export const quickformReducer = (state: QuickformState, action: QuickformAction)

if (typeof allIntermediateQuestions !== "undefined" && allIntermediateQuestions.length > 0) {
const timestamp = new Date().getTime();
for (var intermediateQuestion of allIntermediateQuestions) {
for (let intermediateQuestion of allIntermediateQuestions) {
state = QuestionActionHandler.updateQuestionProperties(state, intermediateQuestion.logicalName,
{
answered: true,
Expand All @@ -145,7 +148,14 @@ export const quickformReducer = (state: QuickformState, action: QuickformAction)
);

tasks.push(QuestionActionHandler.validateInput(state, intermediateQuestion.logicalName).then(result => {
action.dispatch({ type: 'SET_VALIDATION_RESULT', logicalName: intermediateQuestion.logicalName, validationResult: result, timestamp: timestamp });
logger.log("QuickForm Reducer {action} - {logicalName}: {result}",
action.type, intermediateQuestion.logicalName, result);

action.dispatch({
type: 'SET_VALIDATION_RESULT',
logicalName: intermediateQuestion.logicalName,
validationResult: result, timestamp: timestamp
});
return result;
}));
}
Expand Down
55 changes: 30 additions & 25 deletions packages/core/src/state/action-handlers/QuestionActionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,38 @@ import { ValidationResult } from "../../model/ValidationResult";
export class QuestionActionHandler {
private static inputValidator = resolveQuickFormService("inputValidator");

static findSlideIdxAndQuestionIdx = (state: QuickformState, logicalName: string): { slideIndex: number; questionIndex: number } => {
if (state.isSubmitSlide)
return { slideIndex: -1, questionIndex: state.data.submit.submitFields.findIndex(x => x.logicalName === logicalName) };

for (let slideIndex = 0; slideIndex < state.slides.length; slideIndex++) {
const questionIndex: number = state.slides[slideIndex].questions.findIndex((q: QuestionModel) => q.logicalName === logicalName);
if (questionIndex !== -1) {
return { slideIndex, questionIndex };
}
}
return { slideIndex: -1, questionIndex: -1 };
};
//static findSlideIdxAndQuestionIdx = (state: QuickformState, logicalName: string): { slideIndex: number; questionIndex: number } => {
// if (state.isSubmitSlide)
// return { slideIndex: -1, questionIndex: state.data.submit.submitFields.findIndex(x => x.logicalName === logicalName) };

// for (let slideIndex = 0; slideIndex < state.slides.length; slideIndex++) {
// const questionIndex: number = state.slides[slideIndex].questions.findIndex((q: QuestionModel) => q.logicalName === logicalName);
// if (questionIndex !== -1) {
// return { slideIndex, questionIndex };
// }
// }
// return { slideIndex: -1, questionIndex: -1 };
//};

static updateQuestionProperties = (state: QuickformState, logicalName: string, propertiesToUpdate: any): QuickformState => {
const { slideIndex, questionIndex } = this.findSlideIdxAndQuestionIdx(state, logicalName);

//TODO - figure out if object need to be a new object / rerender ect - as the old implementation does
//not work when updateQuestionProperties is called after the state has been updated changing to submit page.

// const { slideIndex, questionIndex } = this.findSlideIdxAndQuestionIdx(state, logicalName);

const newState = { ...state };
const targetQuestion = state.isSubmitSlide ?
newState.data.submit.submitFields[questionIndex] :
newState.slides[slideIndex].questions[questionIndex];


// const newState = { ...state };
// const targetQuestion = state.isSubmitSlide ?
// newState.data.submit.submitFields[questionIndex] :
// newState.slides[slideIndex].questions[questionIndex];
const targetQuestion = getAllQuestions(state).find(x => x.logicalName === logicalName);

if (!targetQuestion) {
const logger = resolveQuickFormService("logger");
logger.log("QuickForm Reducer - Question not found: {logicalName} {questionIndex} {isSubmitSlide}",
logicalName, questionIndex, state.isSubmitSlide);
logger.log("QuickForm Reducer - Question not found: {logicalName} {isSubmitSlide}",
logicalName, state.isSubmitSlide);
return state;
}

Expand All @@ -47,13 +52,13 @@ export class QuestionActionHandler {
}
});

if (state.isSubmitSlide) {
newState.data.submit.submitFields[questionIndex] = targetQuestion;
} else {
newState.slides[slideIndex].questions[questionIndex] = targetQuestion;
}
//if (state.isSubmitSlide) {
// newState.data.submit.submitFields[questionIndex] = targetQuestion;
//} else {
// newState.slides[slideIndex].questions[questionIndex] = targetQuestion;
//}

return newState;
return { ...state };
};

static answerQuestion = (state: QuickformState, { logicalName, output, intermediate, validationResult }: QuickformAnswerQuestionAction) => {
Expand Down

0 comments on commit 5e4fd64

Please sign in to comment.