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

Frontend form error handling #125

Merged
merged 9 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
43 changes: 34 additions & 9 deletions apps/web/src/app/_components/form/review-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import { cn } from "@cooper/ui";
import { Button } from "@cooper/ui/button";
import { Form } from "@cooper/ui/form";
import { useToast } from "@cooper/ui/hooks/use-toast";
import { CheckIcon } from "@cooper/ui/icons";

import {
Expand All @@ -23,6 +24,7 @@
} from "~/app/_components/form/sections";
import { SubmissionConfirmation } from "~/app/_components/form/submission-confirmation";
import { api } from "~/trpc/react";
import { SubmissionFailure } from "./submission-failure";

const formSchema = z.object({
workTerm: z.nativeEnum(WorkTerm, {
Expand Down Expand Up @@ -262,20 +264,43 @@
scroll.scrollToTop({ duration: 250, smooth: true });
};

const mutation = api.review.create.useMutation();
const [validForm, setValidForm] = useState(true);
const { toast } = useToast();

function onSubmit(values: z.infer<ReviewFormType>) {
mutation.mutate({
roleId: props.roleId,
profileId: props.profileId,
companyId: props.company.id,
...values,
});
const mutation = api.review.create.useMutation({
onError: (error) => {
console.error("Mutation Error:", error); // Logs the full error details
setValidForm(false);
toast({
title: "Submission Error",
description:
error?.message || "Something went wrong. Please try again.",

Check failure on line 277 in apps/web/src/app/_components/form/review-form.tsx

View workflow job for this annotation

GitHub Actions / lint

Unnecessary optional chain on a non-nullish value
variant: "destructive",
});
setValidForm(false);
},
});

async function onSubmit(values: z.infer<ReviewFormType>) {
try {
await mutation.mutateAsync({
roleId: props.roleId,
profileId: props.profileId,
companyId: props.company?.id ?? "",

Check failure on line 289 in apps/web/src/app/_components/form/review-form.tsx

View workflow job for this annotation

GitHub Actions / lint

Unnecessary conditional, expected left-hand side of `??` operator to be possibly null or undefined

Check failure on line 289 in apps/web/src/app/_components/form/review-form.tsx

View workflow job for this annotation

GitHub Actions / lint

Unnecessary optional chain on a non-nullish value
...values,
});
} catch (error) {
console.error("Mutation failed:", error);
}
}

if (currentStep === steps.length + 1) {
if (validForm) {
return <SubmissionConfirmation />;
} else {
return <SubmissionFailure />;
}
// Also check if the mutation is successful before displaying this. Otherwise, show a loading spinner.
return <SubmissionConfirmation />;
}

if (currentStep === 0) {
Expand Down
24 changes: 24 additions & 0 deletions apps/web/src/app/_components/form/submission-failure.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Link from "next/link";

import { Button } from "@cooper/ui/button";

export function SubmissionFailure() {
return (
<div className="flex flex-col border-2">
<div className="z-10 -mb-4 h-4 w-full rounded-t-xl bg-gradient-to-r from-rose-400 via-yellow-400 to-blue-600" />
<div className="flex h-[80vh] w-full flex-col items-center justify-center space-y-6 rounded-xl bg-white px-8 py-32 text-cooper-blue-600">
<div className="text-5xl font-bold">
Whoops, your review is invalid...
</div>
<div className="max-w-3xl text-center text-2xl">
IM SORRY IT DOES NOT WORK
</div>
<div className="flex justify-between space-x-8">
<Link href="/">
<Button>Return to home </Button>
</Link>
</div>
</div>
</div>
);
}
Loading