From fa51c4a225784cc2f9a02f76e37c92a33686b38f Mon Sep 17 00:00:00 2001 From: David Furber Date: Mon, 18 Oct 2021 13:21:05 -0400 Subject: [PATCH] fix(progress-bar): make progress bar work for simple flow as well as branch flow (#116) Co-authored-by: David Furber --- src/composable/Questionnaire.ts | 37 ++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/src/composable/Questionnaire.ts b/src/composable/Questionnaire.ts index 42052eb5..ecf2460c 100644 --- a/src/composable/Questionnaire.ts +++ b/src/composable/Questionnaire.ts @@ -230,7 +230,8 @@ export class Questionnaire implements IQuestionnaire { return 101; } - const answerable = this.getBranchQuestions(props); + // if we have branches then we need to do this, otherwise we need to get the number of steps + const answerable = this.getAllAnswerableQuestions(props); const lastStep = answerable.length; // sections[sections.length - 1]?.lastStep; // if there is no step, the questionnaire has just started @@ -246,21 +247,39 @@ export class Questionnaire implements IQuestionnaire { } // To calculate the percent, divide the index of this step // by the index of the last step multiplied by 100. + return Math.round((thisStepIdx / lastStepIdx) * 100); } /** * Gets all of the questions associated with a branch * @param props - * @returns + * @returns string[] */ - getBranchQuestions(props: IStepData): string[] { + getAllAnswerableQuestions(props: IStepData): string[] { const stepId = `${props.stepId}`; const step = this.getStepById(stepId); if (!isEnum(QUESTION_TYPE, step.type)) { return []; } + + if (this.branches.length) { + const question = step as IQuestion; + return this.getBranchQuestions(question); + } else { + return this.getQuestionsWithoutBranches(); + } + + } + + /** + * Gets all of the questions associated with a branch + * @param step + * @returns string[] + */ + private getBranchQuestions(step: IQuestion): string[] { const question = step as IQuestion; + return ( this.branches .find((b) => b.id === question.branch?.id) @@ -268,6 +287,18 @@ export class Questionnaire implements IQuestionnaire { ); } + /** + * Gets all of the questions regardless of branch + * @returns string[] + */ + private getQuestionsWithoutBranches(): string[] { + return ( + this.steps + .filter((q) => isEnum(QUESTION_TYPE, q.type)) + .map(q => q.id) + ) + } + /** * Gets a list of questions that may be answered in the future * @param props