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 5 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: 38 additions & 10 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,46 @@
scroll.scrollToTop({ duration: 250, smooth: true });
};

const mutation = api.review.create.useMutation();
const [validForm, setValidForm] = useState(true); //wait where do i put these
const { toast } = useToast(); //like at the tipity top?

function onSubmit(values: z.infer<ReviewFormType>) {
mutation.mutate({
roleId: props.roleId,
profileId: props.profileId,
companyId: props.company.id,
...values,
});
const [errorMessage, setErrorMessage] = useState<string | null>(null);

const mutation = api.review.create.useMutation({
onError: (error) => {
setValidForm(false);
setErrorMessage(error.message || "An unknown error occurred.");

toast({
title: "Submission Error",
description: (error && error.message) ? error.message : "Something went wrong. Please try again.",

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

View workflow job for this annotation

GitHub Actions / lint

Unnecessary conditional, value is always truthy

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

View workflow job for this annotation

GitHub Actions / lint

Prefer using an optional chain expression instead, as it's more concise and easier to read
variant: "destructive",
});
},
});




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 293 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 293 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) {
// Also check if the mutation is successful before displaying this. Otherwise, show a loading spinner.
return <SubmissionConfirmation />;
if (validForm) {
return <SubmissionConfirmation />;
} else {
return <SubmissionFailure message={errorMessage || undefined} />;

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

View workflow job for this annotation

GitHub Actions / lint

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator
}
}

if (currentStep === 0) {
Expand Down
25 changes: 25 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,25 @@
import Link from "next/link";
import { Button } from "@cooper/ui/button";

interface SubmissionFailureProps {
message?: string; // Allow passing an optional error message
}

export function SubmissionFailure({ message }: SubmissionFailureProps) {
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">
{message ? message : "Something went wrong. Please try again."}
</div>
<div className="flex justify-between space-x-8">
<Link href="/">
<Button>Return to home </Button>
</Link>
</div>
</div>
</div>
);
}
Loading