From a14d4b7466da384b5f53ec7766b9291fa715d1bf Mon Sep 17 00:00:00 2001 From: Jurre Brandsen Date: Fri, 5 Apr 2024 11:53:41 +0200 Subject: [PATCH 1/3] unable to use the progression bar until all questions have been answered. --- src/components/mobile/progression-bar.tsx | 48 +++++++++---- src/components/progression-bar.tsx | 83 +++++++++++++++-------- src/components/survey-questionnaire.tsx | 20 +++++- src/components/survey-questions.tsx | 2 +- src/models/types.ts | 1 + 5 files changed, 109 insertions(+), 45 deletions(-) diff --git a/src/components/mobile/progression-bar.tsx b/src/components/mobile/progression-bar.tsx index 2204d2b..9c22e43 100644 --- a/src/components/mobile/progression-bar.tsx +++ b/src/components/mobile/progression-bar.tsx @@ -31,21 +31,39 @@ const MobileProgressionBar = ({ roles }: { roles: Section[] }) => { {roles.map((section) => ( - - - {section.completed && ( -
- {" "} -
- )} - {section.started && !section.completed ? ( -
- {" "} -
- ) : null} - {section.label} -
- +
+ {section.currentCompleted ? ( + + + {section.completed && ( +
+ {" "} +
+ )} + {section.started && !section.completed ? ( +
+ {" "} +
+ ) : null} + {section.label} +
+ + ) : ( + + {section.completed && ( +
+ {" "} +
+ )} + {section.started && !section.completed ? ( +
+ {" "} +
+ ) : null} + {section.label} +
+ )} +
))}
diff --git a/src/components/progression-bar.tsx b/src/components/progression-bar.tsx index 6abf0dc..ec76cd1 100644 --- a/src/components/progression-bar.tsx +++ b/src/components/progression-bar.tsx @@ -18,33 +18,62 @@ const ProgressionBar = ({ roles }: { roles: Section[] }) => { {roles.map((section, index) => ( {/* Circle with clickable area */} - -
-
- {section.label} -
- +
+ {section.currentCompleted ? ( + +
+
+ {section.label} +
+ + ) : ( +
+
+
+ {section.label} +
+
+ )} +
+ {/* Line (except for the last section) */} {index !== roles.length - 1 && (
+ const questionsForRole = currentAnswers.filter((answer) => answer.question.roles?.some((role) => role.id === roleId), ); @@ -129,6 +129,19 @@ export function SurveyQuestionnaire({ return answeredQuestionsForRole.length >= totalQuestionsForRole; } + function hasAnsweredAllQuestionsForCurrentRole( + form: ReturnType["form"], + ) { + const formValues = form.getValues(); + + // count the number of undefined values in the form + const unansweredQuestions = Object.values(formValues).filter( + (value) => value === undefined, + ); + + return unansweredQuestions.length === 0; + } + const selectedRolesForProgressBar = userSelectedRoles .sort((a, b) => { const roleA = a.role.toLowerCase(); @@ -152,6 +165,7 @@ export function SurveyQuestionnaire({ started: userAnswersForRole.some((answer) => answer.question.roles?.some((role) => role.id === role.id), ), + currentCompleted: hasAnsweredAllQuestionsForCurrentRole(form), })); const ProgressionBarComponent = @@ -171,6 +185,8 @@ export function SurveyQuestionnaire({ } } + console.log(selectedRolesForProgressBar); + return (
diff --git a/src/components/survey-questions.tsx b/src/components/survey-questions.tsx index 51ef254..6c523ae 100644 --- a/src/components/survey-questions.tsx +++ b/src/components/survey-questions.tsx @@ -100,7 +100,7 @@ export function SurveyQuestions({ : "bg-slate-100 hover:bg-slate-300 dark:bg-slate-900 dark:hover:bg-slate-700" }`} > - + {question.questionText} diff --git a/src/models/types.ts b/src/models/types.ts index 09fd457..ae97fd2 100644 --- a/src/models/types.ts +++ b/src/models/types.ts @@ -56,6 +56,7 @@ export interface Section { current: boolean; completed: boolean; started: boolean; + currentCompleted: boolean; } export type TransformedData = Record< From b740f37f06f4ba5f33ef887bd55c81512d946169 Mon Sep 17 00:00:00 2001 From: Jurre Brandsen Date: Fri, 5 Apr 2024 12:37:16 +0200 Subject: [PATCH 2/3] fix of a bug where the completed bool was true while answers still needed to be answered --- src/components/survey-questionnaire.tsx | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/components/survey-questionnaire.tsx b/src/components/survey-questionnaire.tsx index b37ba70..e43b52e 100644 --- a/src/components/survey-questionnaire.tsx +++ b/src/components/survey-questionnaire.tsx @@ -114,19 +114,25 @@ export function SurveyQuestionnaire({ roleId: string, questions: Question[], ) { - const questionsForRole = currentAnswers.filter((answer) => - answer.question.roles?.some((role) => role.id === roleId), + const totalQuestionsForRole = questions + .filter((question) => question.roleIds?.includes(roleId)) + .map((question) => question.id); + + // Get unique question IDs from totalQuestionsForRole + const uniqueQuestionIds = new Set(totalQuestionsForRole); + + // Filter currentAnswersForRole to remove duplicates + const currentAnswersForRole = currentAnswers.filter( + (answer, index, self) => + self.findIndex((a) => a.question.id === answer.question.id) === index && + uniqueQuestionIds.has(answer.question.id), ); - const totalQuestionsForRole = questions.filter((question) => - question.roleIds?.some((role) => role === roleId), - ).length; - - const answeredQuestionsForRole = questionsForRole.filter( + const answeredQuestionsForRole = currentAnswersForRole.filter( (answer) => answer.answerId !== undefined, ); - return answeredQuestionsForRole.length >= totalQuestionsForRole; + return answeredQuestionsForRole.length >= totalQuestionsForRole.length; } function hasAnsweredAllQuestionsForCurrentRole( From 7260561e14f58332f723f4b19fb4dba1038b919f Mon Sep 17 00:00:00 2001 From: Jurre Brandsen Date: Fri, 5 Apr 2024 12:40:40 +0200 Subject: [PATCH 3/3] fix of lint and typecheck --- src/components/ui/button.tsx | 1 - src/utils/role-utils.ts | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/ui/button.tsx b/src/components/ui/button.tsx index cd80487..23c5df0 100644 --- a/src/components/ui/button.tsx +++ b/src/components/ui/button.tsx @@ -3,7 +3,6 @@ import { Slot } from "@radix-ui/react-slot"; import { cva, type VariantProps } from "class-variance-authority"; import { cn } from "~/lib/utils"; -import type { ReactNode } from "react"; const buttonVariants = cva( "inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50", diff --git a/src/utils/role-utils.ts b/src/utils/role-utils.ts index 26954be..36ce805 100644 --- a/src/utils/role-utils.ts +++ b/src/utils/role-utils.ts @@ -25,6 +25,7 @@ export function generateRolesWithHref( current: false, completed: false, started: false, + currentCompleted: false, })); return availableRoles;