diff --git a/apps/web/src/app/_components/form/review-form.tsx b/apps/web/src/app/_components/form/review-form.tsx index 5e66c91..75cd7a9 100644 --- a/apps/web/src/app/_components/form/review-form.tsx +++ b/apps/web/src/app/_components/form/review-form.tsx @@ -229,9 +229,31 @@ export function ReviewForm(props: ReviewFormProps) { }); const [currentStep, setCurrentStep] = useState(0); + const [validForm, setValidForm] = useState(true); + const [errorMessage, setErrorMessage] = useState(null); + + const { toast } = useToast(); type FieldName = keyof z.infer; + const profile = api.profile.getCurrentUser.useQuery(undefined, { + refetchOnWindowFocus: false, + }); + const profileId = profile.data?.id; + + const reviews = api.review.getByProfile.useQuery( + { id: profileId ?? "" }, + { + enabled: !!profileId, + }, + ); + + const canReviewForTerm = (): boolean => { + console.log("reviews", reviews.data); + + return false; + }; + const next = async (e: React.MouseEvent) => { e.preventDefault(); const fields = steps[currentStep - 1]?.fields; @@ -243,6 +265,11 @@ export function ReviewForm(props: ReviewFormProps) { return; } + if (currentStep === 1 && !canReviewForTerm()) { + alert("You have already submitted too many reviews for this term!"); + return; + } + // FIXME: Fix the scrolling eslint issue if (currentStep <= steps.length) { @@ -264,33 +291,25 @@ export function ReviewForm(props: ReviewFormProps) { scroll.scrollToTop({ duration: 250, smooth: true }); }; - const [validForm, setValidForm] = useState(true); //wait where do i put these - const { toast } = useToast(); //like at the tipity top? - - const [errorMessage, setErrorMessage] = useState(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.", + description: error.message || "Something went wrong. Please try again.", variant: "destructive", }); }, }); - - - async function onSubmit(values: z.infer) { try { await mutation.mutateAsync({ roleId: props.roleId, profileId: props.profileId, - companyId: props.company?.id ?? "", + companyId: props.company.id, ...values, }); } catch (error) { @@ -302,7 +321,7 @@ export function ReviewForm(props: ReviewFormProps) { if (validForm) { return ; } else { - return ; + return ; } }