I have 5 different inputs for taking OTP. and I am using 6 useState
hook to get their value.
Now I want to store those 5 input value in the 6th variable I'm passing to my redux store.
I tried this way but it returns an object and also use parseInt
but it doesn't work.
const [otp, setOtp] = useState(null);
const [digitOne, setDigitOne] = useState(null);
const [digitTwo, setDigitTwo] = useState(null);
const [digitThree, setDigitThree] = useState(null);
const [digitFour, setDigitFour] = useState(null);
const [digitFive, setDigitFive] = useState(null);
const handleSubmit = (e) => {
e.preventDefault();
const otp = { digitOne, digitTwo, digitThree, digitFour, digitFive };
console.log(digitOne, digitTwo, digitThree, digitFour, digitFive);
dispatch(checkVerification({ user_info, otp })).then(() => {
console.log(otp);
});
};
return (
<form
onSubmit={handleSubmit}
className="flex flex-col justify-start w-full mt-lg "
>
<div className="flex flex-row justify-between mx-auto max-w-screen-xs space-x-2">
<input
type="text"
className="text-xl w-10 text-center bg-transparent border-t-0 border-l-0 border-r-0 focus:ring-0 focus:border-gray-400 border-b border-gray-400 h-10 "
pattern="[0-9]"
maxLength={1}
value={digitOne}
onChange={(e) => setDigitOne(e.target.value)}
/>
<input
type="text"
className="text-xl w-10 text-center bg-transparent border-t-0 border-l-0 border-r-0 focus:ring-0 focus:border-gray-400 border-b border-gray-400 h-10 "
pattern="[0-9]"
maxLength={1}
value={digitTwo}
onChange={(e) => setDigitTwo(e.target.value)}
/>
<input
type="text"
className="text-xl w-10 text-center bg-transparent border-t-0 border-l-0 border-r-0 focus:ring-0 focus:border-gray-400 border-b border-gray-400 h-10 "
pattern="[0-9]"
maxLength={1}
value={digitThree}
onChange={(e) => setDigitThree(e.target.value)}
/>
<input
type="text"
className="text-xl w-10 text-center bg-transparent border-t-0 border-l-0 border-r-0 focus:ring-0 focus:border-gray-400 border-b border-gray-400 h-10 "
pattern="[0-9]"
maxLength={1}
value={digitFour}
onChange={(e) => setDigitFour(e.target.value)}
/>
<input
type="text"
className="text-xl w-10 text-center bg-transparent border-t-0 border-l-0 border-r-0 focus:ring-0 focus:border-gray-400 border-b border-gray-400 h-10 "
pattern="[0-9]"
maxLength={1}
value={digitFive}
onChange={(e) => setDigitFive(e.target.value)}
/>
</div>
<button className="uppercase flex-grow mt-6 p-2 bg-red-500 text-white px-4 rounded mx-lg">
proceed
</button>
</form>
);
Actions
import ApiInstance from "../../Api/root";
import store from "../store";
import { CHECK_VERIFIED } from "../Types/CustomerVerificationTypes";
import {
CUSTOMER_LOGIN_SUCCESS,
GET_CUSTOMER_LOGIN,
} from "../Types/CustomerLoginTypes";
export const checkVerification =
({ user_info, otp }) =>
async (dispatch, getState) => {
const deviceId = getState().device.device.data.device.id;
localStorage.setItem("id", deviceId);
console.log(otp);
const res = await ApiInstance.get(
`customers/otp/verification?mobile_number=${user_info.mobile_number}&country_code=${user_info.country_code}&otp=${otp}&device_id=${deviceId}`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
}
)
.then((res) => {
console.log("Auth Token", res.data);
dispatch({
type: CHECK_VERIFIED,
payload: res.data,
});
// todo only if API Call is success
if (res.data.success) {
localStorage.setItem("authToken", res.data.token);
} else {
alert("Entered OTP is wrong, Try Again");
}
})
.catch((error) => {
console.error(error);
});
};
Also I've set maxLength
of input to 1 but How do I move one input to another after user puts a number?