Skip to content

Commit

Permalink
scroll to first error + removed duplicate code in survey question com…
Browse files Browse the repository at this point in the history
…ponent (#55)
  • Loading branch information
JurreBrandsenInfoSupport authored Mar 27, 2024
1 parent d82e76c commit 6e908f1
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 81 deletions.
1 change: 1 addition & 0 deletions src/components/mobile/survey-questions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export function MobileSurveyQuestionnaire({
key={`${question.id}`}
render={({ field }) => (
<Card
id={question.id}
className={`border-2 ${form.formState.errors[question.id] ? "border-red-500" : "border-gray-200 dark:border-slate-900"}`}
>
<CardHeader>
Expand Down
130 changes: 50 additions & 80 deletions src/components/survey-questionnaire.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,88 +213,58 @@ export function SurveyQuestionnaire({
),
}));

const ProgressionBarComponent =
screenSize.width < 768 ? MobileProgressionBar : ProgressionBar;
const QuestionsComponent =
screenSize.width < 768 ? MobileSurveyQuestionnaire : SurveyQuestions;

return (
<div>
{screenSize.width < 768 && (
<div>
<div className="mb-4">
<MobileProgressionBar roles={selectedRolesForProgressBar} />
</div>
<Form {...form}>
<form
onSubmit={form.handleSubmit(async () => {
setIsSubmitting(true);
onSubmit(
form.getValues(),
session,
selectedRolesForProgressBar,
submitResponse,
).catch((error) => {
console.error("Error in form submission:", error);
});
})}
className="grid gap-4 md:grid-cols-1 lg:grid-cols-1"
>
<MobileSurveyQuestionnaire
session={session}
filteredQuestions={filteredQuestions}
answerOptions={answerOptions}
form={form}
responses={responses}
setResponses={setResponses}
submitResponse={submitResponse}
/>
<SpinnerButton
type="submit"
state={isSubmitting || submitResponse.isLoading}
disabled={isSubmitting || submitResponse.isLoading}
name={
getNextHref(selectedRolesForProgressBar) ? "Next" : "Submit"
}
/>
</form>
</Form>
</div>
)}
{screenSize.width >= 768 && (
<div>
<ProgressionBar roles={selectedRolesForProgressBar} />
<Form {...form}>
<form
onSubmit={form.handleSubmit(async () => {
setIsSubmitting(true);
onSubmit(
form.getValues(),
session,
selectedRolesForProgressBar,
submitResponse,
).catch((error) => {
console.error("Error in form submission:", error);
});
})}
className="grid gap-4 md:grid-cols-1 lg:grid-cols-1"
>
<SurveyQuestions
session={session}
filteredQuestions={filteredQuestions}
answerOptions={answerOptions}
form={form}
responses={responses}
setResponses={setResponses}
submitResponse={submitResponse}
/>
<SpinnerButton
type="submit"
state={isSubmitting || submitResponse.isLoading}
disabled={isSubmitting || submitResponse.isLoading}
name={
getNextHref(selectedRolesForProgressBar) ? "Next" : "Submit"
}
/>
</form>
</Form>
</div>
)}
<ProgressionBarComponent roles={selectedRolesForProgressBar} />
<Form {...form}>
<form
onSubmit={form.handleSubmit(
async () => {
setIsSubmitting(true);
onSubmit(
form.getValues(),
session,
selectedRolesForProgressBar,
submitResponse,
).catch((error) => {
console.error("Error in form submission:", error);
});
},
(errors) => {
// scroll to the first error
const firstError = Object.keys(errors)[0];
const element = document.getElementById(firstError ?? "");
element?.scrollIntoView({
behavior: "smooth",
block: "center",
inline: "center",
});
},
)}
className="grid gap-4 md:grid-cols-1 lg:grid-cols-1"
>
<QuestionsComponent
session={session}
filteredQuestions={filteredQuestions}
answerOptions={answerOptions}
form={form}
responses={responses}
setResponses={setResponses}
submitResponse={submitResponse}
/>
<SpinnerButton
type="submit"
state={isSubmitting || submitResponse.isLoading}
disabled={isSubmitting || submitResponse.isLoading}
name={getNextHref(selectedRolesForProgressBar) ? "Next" : "Submit"}
/>
</form>
</Form>
</div>
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/components/survey-questions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,10 @@ export function SurveyQuestions({
render={({ field }) => (
<TableRow
key={question.id}
id={question.id}
className={
form.formState.errors[question.id]
? "!border-2 !border-dashed !border-red-500"
? "error !border-2 !border-dashed !border-red-500"
: ""
}
>
Expand Down

0 comments on commit 6e908f1

Please sign in to comment.