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

70 disable navigation to next page if not filled in fully #83

Merged
Show file tree
Hide file tree
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
48 changes: 33 additions & 15 deletions src/components/mobile/progression-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,39 @@ const MobileProgressionBar = ({ roles }: { roles: Section[] }) => {
<DropdownMenuSeparator />
<ScrollArea className="w-50 h-72 rounded-md border">
{roles.map((section) => (
<Link href={section.href} key={section.id}>
<DropdownMenuCheckboxItem checked={section.current}>
{section.completed && (
<div className="absolute left-2 flex h-3.5 w-3.5 items-center justify-center">
<CheckIcon className="h-4 w-4 text-green-500" />{" "}
</div>
)}
{section.started && !section.completed ? (
<div className="absolute left-2 flex h-3.5 w-3.5 items-center justify-center">
<DotFilledIcon className="h-4 w-4 text-orange-500" />{" "}
</div>
) : null}
{section.label}
</DropdownMenuCheckboxItem>
</Link>
<div key={section.id}>
{section.currentCompleted ? (
<Link href={section.href}>
<DropdownMenuCheckboxItem checked={section.current}>
{section.completed && (
<div className="absolute left-2 flex h-3.5 w-3.5 items-center justify-center">
<CheckIcon className="h-4 w-4 text-green-500" />{" "}
</div>
)}
{section.started && !section.completed ? (
<div className="absolute left-2 flex h-3.5 w-3.5 items-center justify-center">
<DotFilledIcon className="h-4 w-4 text-orange-500" />{" "}
</div>
) : null}
{section.label}
</DropdownMenuCheckboxItem>
</Link>
) : (
<DropdownMenuCheckboxItem disabled>
{section.completed && (
<div className="absolute left-2 flex h-3.5 w-3.5 items-center justify-center">
<CheckIcon className="h-4 w-4 text-green-500" />{" "}
</div>
)}
{section.started && !section.completed ? (
<div className="absolute left-2 flex h-3.5 w-3.5 items-center justify-center">
<DotFilledIcon className="h-4 w-4 text-orange-500" />{" "}
</div>
) : null}
{section.label}
</DropdownMenuCheckboxItem>
)}
</div>
))}
</ScrollArea>
</DropdownMenuContent>
Expand Down
83 changes: 56 additions & 27 deletions src/components/progression-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,62 @@ const ProgressionBar = ({ roles }: { roles: Section[] }) => {
{roles.map((section, index) => (
<Fragment key={section.id}>
{/* Circle with clickable area */}
<Link
type="submit"
href={section.href}
className="relative flex items-center justify-center"
>
<div
className={`mb-1 h-6 w-6 rounded-full border-2 ${
section.current
? "border-custom-secondary bg-custom-primary dark:border-custom-primary dark:bg-custom-secondary"
: section.completed
? "border-green-500 bg-green-500"
: section.started
? "border-orange-500 bg-orange-500"
: "border-gray-300"
}`}
></div>
<div
className="absolute -rotate-45 whitespace-nowrap text-xs font-semibold"
style={{
transformOrigin: "bottom left",
top: "-22px",
left: "calc(100% - 0px)",
}}
>
{section.label}
</div>
</Link>
<div className="relative flex items-center justify-center">
{section.currentCompleted ? (
<Link
type="submit"
href={section.href}
className="flex items-center justify-center"
>
<div
className={`mb-1 h-6 w-6 rounded-full border-2 ${
section.current
? "border-custom-secondary bg-custom-primary dark:border-custom-primary dark:bg-custom-secondary"
: section.completed
? "border-green-500 bg-green-500"
: section.started
? "border-orange-500 bg-orange-500"
: "border-gray-300"
}`}
></div>
<div
className="absolute -rotate-45 whitespace-nowrap text-xs font-semibold"
style={{
transformOrigin: "bottom left",
top: "-22px",
left: "calc(100% - 0px)",
}}
>
{section.label}
</div>
</Link>
) : (
<div>
<div
className={`mb-1 h-6 w-6 rounded-full border-2 ${
section.current
? "border-custom-secondary bg-custom-primary dark:border-custom-primary dark:bg-custom-secondary"
: section.completed
? "border-green-500 bg-green-500"
: section.started
? "border-orange-500 bg-orange-500"
: "border-gray-300"
}`}
></div>
<div
className="absolute -rotate-45 whitespace-nowrap text-xs font-semibold"
style={{
transformOrigin: "bottom left",
top: "-22px",
left: "calc(100% - 0px)",
}}
>
{section.label}
</div>
</div>
)}
</div>

{/* Line (except for the last section) */}
{index !== roles.length - 1 && (
<div
Expand Down
40 changes: 31 additions & 9 deletions src/components/survey-questionnaire.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,42 @@ export function SurveyQuestionnaire({

// function that check if a user already has more than 1 response for a question
function hasAnsweredAllQuestionsForRole(
userAnswersForRole: QuestionResult[],
currentAnswers: QuestionResult[],
roleId: string,
questions: Question[],
) {
const questionsForRole = userAnswersForRole.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(
form: ReturnType<typeof useGenerateFormAndSchema>["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
Expand All @@ -152,6 +171,7 @@ export function SurveyQuestionnaire({
started: userAnswersForRole.some((answer) =>
answer.question.roles?.some((role) => role.id === role.id),
),
currentCompleted: hasAnsweredAllQuestionsForCurrentRole(form),
}));

const ProgressionBarComponent =
Expand All @@ -171,6 +191,8 @@ export function SurveyQuestionnaire({
}
}

console.log(selectedRolesForProgressBar);

return (
<div>
<Form {...form}>
Expand Down
2 changes: 1 addition & 1 deletion src/components/survey-questions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export function SurveyQuestions({
: "bg-slate-100 hover:bg-slate-300 dark:bg-slate-900 dark:hover:bg-slate-700"
}`}
>
<TableCell>
<TableCell className="pl-2">
{question.questionText}
<FormMessage />
</TableCell>
Expand Down
1 change: 0 additions & 1 deletion src/components/ui/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/models/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export interface Section {
current: boolean;
completed: boolean;
started: boolean;
currentCompleted: boolean;
}

export type TransformedData = Record<
Expand Down
1 change: 1 addition & 0 deletions src/utils/role-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export function generateRolesWithHref(
current: false,
completed: false,
started: false,
currentCompleted: false,
}));

return availableRoles;
Expand Down