Skip to content

Commit

Permalink
Merge pull request #24 from Dami-18/minor
Browse files Browse the repository at this point in the history
add otp/login spinner and fix roll number regex
  • Loading branch information
proffapt authored Jul 13, 2024
2 parents 51e986f + 77390e8 commit 35e616f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
5 changes: 2 additions & 3 deletions frontend/src/components/RollForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ interface IFormInput {
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!"),
.required("Roll number is required!"),
password: yup.string().required("Password is required!"),
});

Expand Down Expand Up @@ -111,7 +110,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 35e616f

Please sign in to comment.