Add Otp Input
parent
b69df8c707
commit
9b6264fc2d
|
|
@ -108,7 +108,7 @@ function LoginForm() {
|
||||||
name="phoneNumber"
|
name="phoneNumber"
|
||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
<FormItem className="space-y-2">
|
<FormItem className="space-y-2">
|
||||||
<FormLabel className="text-black text-lg absolute -mt-4 bg-white mr-4">تلفن همراه</FormLabel>
|
<FormLabel >تلفن همراه</FormLabel>
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<Input
|
<Input
|
||||||
placeholder="09"
|
placeholder="09"
|
||||||
|
|
@ -116,7 +116,7 @@ function LoginForm() {
|
||||||
className=" w-full"
|
className=" w-full"
|
||||||
/>
|
/>
|
||||||
</FormControl>
|
</FormControl>
|
||||||
<FormMessage className="bg-red text-white text-sm p-1" />
|
<FormMessage />
|
||||||
</FormItem>
|
</FormItem>
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
|
|
@ -135,7 +135,7 @@ function LoginForm() {
|
||||||
className="w-full h-10 m-0"
|
className="w-full h-10 m-0"
|
||||||
/>
|
/>
|
||||||
</FormControl>
|
</FormControl>
|
||||||
<FormMessage className="bg-red text-white text-sm p-1 mb-4" />
|
<FormMessage />
|
||||||
</FormItem>
|
</FormItem>
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,74 @@
|
||||||
|
import React, { useState, useEffect } from "react";
|
||||||
|
import Countdown from "react-countdown";
|
||||||
|
// import { CN } from "../../utils/functions";
|
||||||
|
|
||||||
|
const CountdownTimer = ({ isShow, setIsShow }) => {
|
||||||
|
const [remainingTime, setRemainingTime] = useState(2 * 60000);
|
||||||
|
const [key, setKey] = useState(0);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
// Retrieve remaining time from local storage on component mount
|
||||||
|
const storedTime = localStorage.getItem("countdownTime");
|
||||||
|
if (storedTime && storedTime != NaN) {
|
||||||
|
setRemainingTime(parseInt(storedTime));
|
||||||
|
}
|
||||||
|
// else {
|
||||||
|
// setRemainingTime(2 * 60000);
|
||||||
|
// }
|
||||||
|
if (localStorage.getItem("CountDownCompleted")) {
|
||||||
|
setIsShow(false);
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleComplete = () => {
|
||||||
|
// Clear remaining time in local storage when countdown completes
|
||||||
|
localStorage.removeItem("countdownTime");
|
||||||
|
localStorage.setItem("CountDownCompleted", 1);
|
||||||
|
console.log("ccccc");
|
||||||
|
setIsShow(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (localStorage.getItem("CountDownCompleted")) {
|
||||||
|
setIsShow(false);
|
||||||
|
} else {
|
||||||
|
setIsShow(true);
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
const RestartTimer = () => {
|
||||||
|
setKey(!key);
|
||||||
|
};
|
||||||
|
const handleTick = (time) => {
|
||||||
|
// Store remaining time in local storage on each tick
|
||||||
|
localStorage.setItem("countdownTime", time.total);
|
||||||
|
};
|
||||||
|
const renderer = ({ hours, minutes, seconds, completed }) => {
|
||||||
|
return (
|
||||||
|
<span>
|
||||||
|
{minutes}:{seconds}
|
||||||
|
</span>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
useEffect(() => {
|
||||||
|
if (!isShow) {
|
||||||
|
setRemainingTime(2 * 60000);
|
||||||
|
const newKey = Math.random();
|
||||||
|
setKey(newKey);
|
||||||
|
}
|
||||||
|
}, [isShow]);
|
||||||
|
return (
|
||||||
|
<div className={`${isShow ? "" : "hidden"} text-2xl text-gray-500`}>
|
||||||
|
{/* {!localStorage.getItem("CountDownCompleted") && ( */}
|
||||||
|
<Countdown
|
||||||
|
key={key}
|
||||||
|
date={Date.now() + remainingTime}
|
||||||
|
onComplete={handleComplete}
|
||||||
|
onTick={handleTick}
|
||||||
|
renderer={renderer}
|
||||||
|
/>
|
||||||
|
{/* )} */}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default CountdownTimer;
|
||||||
|
|
@ -7,8 +7,6 @@ import useData from "@/store/useData";
|
||||||
|
|
||||||
const Verify = () => {
|
const Verify = () => {
|
||||||
const data = useData((state) => state.data);
|
const data = useData((state) => state.data);
|
||||||
|
|
||||||
|
|
||||||
console.log(data);
|
console.log(data);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
"use client"
|
"use client";
|
||||||
import React, { useState ,useEffect } from 'react'
|
import React, { useState, useEffect } from "react";
|
||||||
import OtpInput from "react-otp-input";
|
import OtpInput from "react-otp-input";
|
||||||
import { useMutation } from "@tanstack/react-query";
|
import { useMutation } from "@tanstack/react-query";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
|
|
@ -7,8 +7,10 @@ import useData from "@/store/useData";
|
||||||
import { setCookie } from "cookies-next";
|
import { setCookie } from "cookies-next";
|
||||||
import { Oval, Triangle } from "react-loader-spinner";
|
import { Oval, Triangle } from "react-loader-spinner";
|
||||||
import useToken from "@/store/useToken";
|
import useToken from "@/store/useToken";
|
||||||
|
import { JwtPayload, jwtDecode } from "jwt-decode";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
|
import { verifyCitizen } from "@/utils/authenticateApi";
|
||||||
|
import CountdownTimer from "./CountDown";
|
||||||
|
|
||||||
export const VerifyInput = () => {
|
export const VerifyInput = () => {
|
||||||
const [otp, setOtp] = useState("");
|
const [otp, setOtp] = useState("");
|
||||||
|
|
@ -17,12 +19,118 @@ export const VerifyInput = () => {
|
||||||
const userData = useData((state) => state.data);
|
const userData = useData((state) => state.data);
|
||||||
const [VerifyRole, setVerifyRole] = useState<any>("");
|
const [VerifyRole, setVerifyRole] = useState<any>("");
|
||||||
const saveToken = useToken((state) => state.saveToken);
|
const saveToken = useToken((state) => state.saveToken);
|
||||||
|
const [isShow, setIsShow] = useState(true);
|
||||||
|
|
||||||
|
|
||||||
|
const CitizenVerifyMutation = useMutation({
|
||||||
|
mutationFn: verifyCitizen,
|
||||||
|
onSuccess: (res) => {
|
||||||
|
saveToken(true);
|
||||||
|
const decodedToken = jwtDecode<any>(res.jwtToken);
|
||||||
|
setCookie("Sootzani_Token", res.jwtToken);
|
||||||
|
setCookie("Sootzani_Refresh_Token", res.refreshToken);
|
||||||
|
const userRole =
|
||||||
|
decodedToken[
|
||||||
|
"http://schemas.microsoft.com/ws/2008/06/identity/claims/role"
|
||||||
|
];
|
||||||
|
localStorage.setItem("Sootzani_Role", userRole);
|
||||||
|
const justLogin = localStorage.getItem("Sootzani_Login_State");
|
||||||
|
if (justLogin) {
|
||||||
|
router.push("/Account/Complaints");
|
||||||
|
localStorage.removeItem("Sootzani_Login_State");
|
||||||
|
} else {
|
||||||
|
router.push("/RequestStep1");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onError: (err: any | string) => {
|
||||||
|
toast(<>{err.response.data.detail}</>, {
|
||||||
|
className: "!bg-red-500 !text-white",
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const handelVerify = () => {
|
||||||
|
const payload = {
|
||||||
|
otpToken: localStorage.getItem("verificationToken"),
|
||||||
|
verificationCode: otp,
|
||||||
|
};
|
||||||
|
CitizenVerifyMutation.mutate(payload);
|
||||||
|
};
|
||||||
|
|
||||||
|
// useEffect(() => {
|
||||||
|
// useData.persist.rehydrate();
|
||||||
|
// }, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (otp.length == 6) {
|
||||||
|
const payload = {
|
||||||
|
otpToken: localStorage.getItem("verificationToken"),
|
||||||
|
verificationCode: otp,
|
||||||
|
};
|
||||||
|
CitizenVerifyMutation.mutate(payload);
|
||||||
|
}
|
||||||
|
}, [otp]);
|
||||||
|
|
||||||
|
// SH....
|
||||||
|
|
||||||
|
|
||||||
|
const handelResendOtp = () => {
|
||||||
|
console.log(11);
|
||||||
|
// resendOtpMutation.mutate({
|
||||||
|
// otpToken: localStorage.getItem(appConstants.SH_CT_OTP_TOKEN),
|
||||||
|
// });
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<p className="text-2xl color-black text-center font-bold">
|
{CitizenVerifyMutation.isPending && (
|
||||||
|
<div className="absolute top-3 left-5 ">
|
||||||
|
<Triangle
|
||||||
|
visible={true}
|
||||||
|
height="40"
|
||||||
|
width="40"
|
||||||
|
color="#003778"
|
||||||
|
ariaLabel="triangle-loading"
|
||||||
|
wrapperStyle={{}}
|
||||||
|
wrapperClass=""
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<p className="text-lg color-black text-center font-bold">
|
||||||
کد تایید ارسالی به تلفن خود را وارد نمایید.
|
کد تایید ارسالی به تلفن خود را وارد نمایید.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
<div className="w-full mt-5" dir="ltr">
|
||||||
|
<OtpInput
|
||||||
|
value={otp}
|
||||||
|
onChange={setOtp}
|
||||||
|
numInputs={6}
|
||||||
|
renderSeparator={<span>{space}</span>}
|
||||||
|
renderInput={(props, idx) => (
|
||||||
|
<input autoFocus={idx == 0} pattern="[0-9]*" {...props} />
|
||||||
|
)}
|
||||||
|
containerStyle=" flex justify-center gap-1"
|
||||||
|
inputStyle=" !w-12 rounded-full h-12 bg-[#F5F5F5] border border-blue/30 text-black"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="mx-auto w-full text-center text-xl mt-6">
|
||||||
|
{" "}
|
||||||
|
<CountdownTimer
|
||||||
|
setIsShow={(state : any) => setIsShow(state)}
|
||||||
|
isShow={isShow}
|
||||||
|
/>
|
||||||
|
|
||||||
|
{!isShow && (
|
||||||
|
<p
|
||||||
|
onClick={handelResendOtp}
|
||||||
|
className=" text-blue text-sm font-semibold cursor-pointer"
|
||||||
|
>
|
||||||
|
ارسال مجدد کد تایید
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
</>
|
</>
|
||||||
)
|
);
|
||||||
}
|
};
|
||||||
|
|
|
||||||
|
|
@ -93,7 +93,7 @@ const FormLabel = React.forwardRef<
|
||||||
return (
|
return (
|
||||||
<Label
|
<Label
|
||||||
ref={ref}
|
ref={ref}
|
||||||
className={cn(error && "text-destructive", className)}
|
className={cn(error && "text-destructive" || `text-black text-lg absolute -mt-4 bg-white mr-4`, className)}
|
||||||
htmlFor={formItemId}
|
htmlFor={formItemId}
|
||||||
{...props}
|
{...props}
|
||||||
/>
|
/>
|
||||||
|
|
@ -155,7 +155,7 @@ const FormMessage = React.forwardRef<
|
||||||
<p
|
<p
|
||||||
ref={ref}
|
ref={ref}
|
||||||
id={formMessageId}
|
id={formMessageId}
|
||||||
className={cn("text-xs font-medium text-destructive", className)}
|
className={cn("text-xs text-destructive text-rose-600 font-bold", className)}
|
||||||
{...props}
|
{...props}
|
||||||
>
|
>
|
||||||
{body}
|
{body}
|
||||||
|
|
|
||||||
|
|
@ -18,10 +18,12 @@
|
||||||
"class-variance-authority": "^0.7.0",
|
"class-variance-authority": "^0.7.0",
|
||||||
"clsx": "^2.1.0",
|
"clsx": "^2.1.0",
|
||||||
"cookies-next": "^4.1.1",
|
"cookies-next": "^4.1.1",
|
||||||
|
"jwt-decode": "^4.0.0",
|
||||||
"lucide-react": "^0.365.0",
|
"lucide-react": "^0.365.0",
|
||||||
"next": "14.1.4",
|
"next": "14.1.4",
|
||||||
"next-themes": "^0.3.0",
|
"next-themes": "^0.3.0",
|
||||||
"react": "^18",
|
"react": "^18",
|
||||||
|
"react-countdown": "^2.3.5",
|
||||||
"react-dom": "^18",
|
"react-dom": "^18",
|
||||||
"react-hook-form": "^7.51.2",
|
"react-hook-form": "^7.51.2",
|
||||||
"react-loader-spinner": "^6.1.6",
|
"react-loader-spinner": "^6.1.6",
|
||||||
|
|
@ -3545,6 +3547,14 @@
|
||||||
"node": ">=4.0"
|
"node": ">=4.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/jwt-decode": {
|
||||||
|
"version": "4.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/jwt-decode/-/jwt-decode-4.0.0.tgz",
|
||||||
|
"integrity": "sha512-+KJGIyHgkGuIq3IEBNftfhW/LfWhXUIY6OmyVWjliu5KH1y0fw7VQ8YndE2O4qZdMSd9SqbnC8GOcZEy0Om7sA==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/keyv": {
|
"node_modules/keyv": {
|
||||||
"version": "4.5.4",
|
"version": "4.5.4",
|
||||||
"resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.4.tgz",
|
"resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.4.tgz",
|
||||||
|
|
@ -4307,7 +4317,6 @@
|
||||||
"version": "15.8.1",
|
"version": "15.8.1",
|
||||||
"resolved": "https://registry.npmjs.org/prop-types/-/prop-types-15.8.1.tgz",
|
"resolved": "https://registry.npmjs.org/prop-types/-/prop-types-15.8.1.tgz",
|
||||||
"integrity": "sha512-oj87CgZICdulUohogVAR7AjlC0327U4el4L6eAvOqCeudMDVU0NThNaV+b9Df4dXgSP1gXMTnPdhfe/2qDH5cg==",
|
"integrity": "sha512-oj87CgZICdulUohogVAR7AjlC0327U4el4L6eAvOqCeudMDVU0NThNaV+b9Df4dXgSP1gXMTnPdhfe/2qDH5cg==",
|
||||||
"dev": true,
|
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"loose-envify": "^1.4.0",
|
"loose-envify": "^1.4.0",
|
||||||
"object-assign": "^4.1.1",
|
"object-assign": "^4.1.1",
|
||||||
|
|
@ -4358,6 +4367,18 @@
|
||||||
"node": ">=0.10.0"
|
"node": ">=0.10.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/react-countdown": {
|
||||||
|
"version": "2.3.5",
|
||||||
|
"resolved": "https://registry.npmjs.org/react-countdown/-/react-countdown-2.3.5.tgz",
|
||||||
|
"integrity": "sha512-K26ENYEesMfPxhRRtm1r+Pf70SErrvW3g4CArLi/x6MPFjgfDFYePT4UghEj8p2nI0cqVV7/JjDgjyr//U60Og==",
|
||||||
|
"dependencies": {
|
||||||
|
"prop-types": "^15.7.2"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"react": ">= 15",
|
||||||
|
"react-dom": ">= 15"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/react-dom": {
|
"node_modules/react-dom": {
|
||||||
"version": "18.2.0",
|
"version": "18.2.0",
|
||||||
"resolved": "https://registry.npmjs.org/react-dom/-/react-dom-18.2.0.tgz",
|
"resolved": "https://registry.npmjs.org/react-dom/-/react-dom-18.2.0.tgz",
|
||||||
|
|
@ -4388,8 +4409,7 @@
|
||||||
"node_modules/react-is": {
|
"node_modules/react-is": {
|
||||||
"version": "16.13.1",
|
"version": "16.13.1",
|
||||||
"resolved": "https://registry.npmjs.org/react-is/-/react-is-16.13.1.tgz",
|
"resolved": "https://registry.npmjs.org/react-is/-/react-is-16.13.1.tgz",
|
||||||
"integrity": "sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==",
|
"integrity": "sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ=="
|
||||||
"dev": true
|
|
||||||
},
|
},
|
||||||
"node_modules/react-loader-spinner": {
|
"node_modules/react-loader-spinner": {
|
||||||
"version": "6.1.6",
|
"version": "6.1.6",
|
||||||
|
|
|
||||||
|
|
@ -19,10 +19,12 @@
|
||||||
"class-variance-authority": "^0.7.0",
|
"class-variance-authority": "^0.7.0",
|
||||||
"clsx": "^2.1.0",
|
"clsx": "^2.1.0",
|
||||||
"cookies-next": "^4.1.1",
|
"cookies-next": "^4.1.1",
|
||||||
|
"jwt-decode": "^4.0.0",
|
||||||
"lucide-react": "^0.365.0",
|
"lucide-react": "^0.365.0",
|
||||||
"next": "14.1.4",
|
"next": "14.1.4",
|
||||||
"next-themes": "^0.3.0",
|
"next-themes": "^0.3.0",
|
||||||
"react": "^18",
|
"react": "^18",
|
||||||
|
"react-countdown": "^2.3.5",
|
||||||
"react-dom": "^18",
|
"react-dom": "^18",
|
||||||
"react-hook-form": "^7.51.2",
|
"react-hook-form": "^7.51.2",
|
||||||
"react-loader-spinner": "^6.1.6",
|
"react-loader-spinner": "^6.1.6",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue