Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(progress-bar): make progress bar work for simple flow as well as branch flow #116

Merged
merged 1 commit into from
Oct 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions src/composable/Questionnaire.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,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
Expand All @@ -236,28 +237,58 @@ 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)
?.questions.map((q) => q.id) || []
);
}

/**
* 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
Expand Down