Skip to content

Commit

Permalink
add otp/login spinner and fix roll number regex
Browse files Browse the repository at this point in the history
  • Loading branch information
Dami-18 committed Jul 13, 2024
1 parent 51e986f commit cef7947
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
4 changes: 2 additions & 2 deletions frontend/src/components/RollForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const schema = yup.object().shape({
roll_number: yup
.string()
.required("Roll number is required!")
.matches(/^\d{2}[A-Z]{2}\d{5}$/, "Please enter valid roll number!"),
.matches(/^\d{2}[A-Z]{2}[A-Z0-9]{5}$/, "Please enter valid roll number!"),
password: yup.string().required("Password is required!"),
});

Expand Down Expand Up @@ -111,7 +111,7 @@ const RollForm: React.FC = () => {
{errors.password?.message || "\u00A0"}
</span>
</div>
<button type="submit" className="submit-button">
<button type="submit" className="submit-button" disabled={isSubmitting}>
{isSubmitting ? <Spinner /> : "Get security question"}
</button>
</form>
Expand Down
18 changes: 12 additions & 6 deletions frontend/src/components/SecurityQueForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ const SecurityQueForm: React.FC = () => {
handleSubmit,
getValues,
trigger,
formState: { errors, isLoading },
formState: { errors },
} = useForm<IFormInput>({ resolver: yupResolver(schema) });
const { user, setAuth } = useAppContext();

const [otpRequested, setOtpRequested] = useState(false);
const [loadingOTP, setLoadingOTP] = useState(false);
const [loadingLogin, setLoadingLogin] = useState(false);

const getOTP = async () => {
const isValid = await trigger("securityAnswer");
Expand All @@ -41,6 +43,7 @@ const SecurityQueForm: React.FC = () => {
formData.append("secret_answer", securityAns);

try {
setLoadingOTP(true);
const res = await fetch(`${BACKEND_URL}/request-otp`, {
method: "POST",
headers: {
Expand Down Expand Up @@ -75,6 +78,8 @@ const SecurityQueForm: React.FC = () => {
} catch (error) {
console.log(error);
setOtpRequested(false);
} finally {
setLoadingOTP(false);
}
};

Expand All @@ -89,6 +94,7 @@ const SecurityQueForm: React.FC = () => {
login_data.append("otp", otp);

try {
setLoadingLogin(true);
const res = await fetch(`${BACKEND_URL}/login`, {
method: "POST",
headers: {
Expand Down Expand Up @@ -122,6 +128,8 @@ const SecurityQueForm: React.FC = () => {
toast.success("Successfully logged in to ERP!");
} catch (error) {
console.error(error);
} finally {
setLoadingLogin(false);
}
};

Expand Down Expand Up @@ -171,13 +179,11 @@ const SecurityQueForm: React.FC = () => {
</span>
</div>
<div>
<button className="submit-button" type="submit">
{isLoading ? (
<button className="submit-button" type="submit" disabled={loadingOTP || loadingLogin}>
{loadingOTP || loadingLogin ? (
<Spinner />
) : otpRequested ? (
"Login"
) : (
"Send OTP"
otpRequested ? "Login" : "Send OTP"
)}
</button>
</div>
Expand Down

0 comments on commit cef7947

Please sign in to comment.