Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add otp/login spinner and fix roll number regex #24

Merged
merged 2 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading