-
-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathnew-verification-form.tsx
More file actions
81 lines (66 loc) · 2.08 KB
/
new-verification-form.tsx
File metadata and controls
81 lines (66 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* eslint-disable react-hooks/exhaustive-deps */
"use client";
import { useCallback, useEffect, useState } from "react";
import { BeatLoader } from "react-spinners";
import { useSearchParams } from "next/navigation";
import { CardWrapper } from "./Card-wrapper";
import { FormSuccess } from "./form-success";
import { FormError } from "./form-error";
import { newVerification } from "@/Actions/new-verification";
import { useRouter } from "next/navigation";
import { useAppDispatch } from "@/lib/store/hooks";
import { updateUserFields } from "@/lib/store/features/userSlice/userSlice";
export function NewVerificationForm() {
const [error, setError] = useState<string | undefined>();
const [success, setSuccess] = useState<string | undefined>();
const router = useRouter();
const dispatch = useAppDispatch();
const searchParams = useSearchParams();
const email = searchParams.get("email");
const token = searchParams.get("token");
console.log("token",token)
const onsubmit = useCallback(() => {
if (success || error) return;
if (!token) {
setError("Missing token!");
return;
}
newVerification(token)
.then((data) => {
setSuccess(data.success);
console.log(success);
dispatch(updateUserFields({email: email}));
console.log("data", data);
if(data.error) setError(data.error);
console.log("error", error);
})
.catch((e) => {
setError("Something went wrong!");
});
}, []);
useEffect(() => {
onsubmit();
}, [onsubmit]);
useEffect(()=>{
if(success){
setTimeout(() => {
router.push(`/login`);
}, 1000);
}
}, [success]);
return (
<CardWrapper
headerLabel="Verifying your email..."
backButtonHref="/login"
backButtonLabel="Back to login"
>
<div className="flex w-full items-center justify-center">
{!success && !error && <BeatLoader />}
{ success &&
<FormSuccess message={success} />
}
{error && !success && <FormError message={error} />}
</div>
</CardWrapper>
);
}