Skip to content

Commit

Permalink
fix:useQuery changed to Request
Browse files Browse the repository at this point in the history
  • Loading branch information
jevil25 committed Sep 23, 2023
1 parent 28976c9 commit f1ab2a8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 67 deletions.
16 changes: 0 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 39 additions & 51 deletions src/Components/Auth/Login.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { useEffect, useState } from "react";
import useQuery from "../../Utils/request/useQuery";
import { postForgotPassword, postLogin } from "../../Redux/actions";
import request from "../../Utils/request/request";
import routes from "../../Redux/api";
import { useTranslation } from "react-i18next";
import ReCaptcha from "react-google-recaptcha";
import * as Notification from "../../Utils/Notifications.js";
import { get } from "lodash";
import LegendInput from "../../CAREUI/interactive/LegendInput";
import LanguageSelectorLogin from "../Common/LanguageSelectorLogin";
import CareIcon from "../../CAREUI/icons/CareIcon";
Expand All @@ -13,7 +12,6 @@ import CircularProgress from "../Common/components/CircularProgress";
import { LocalStorageKeys } from "../../Common/constants";
import ReactMarkdown from "react-markdown";
import rehypeRaw from "rehype-raw";
import routes from "../../Redux/api";

export const Login = (props: { forgot?: boolean }) => {
const {
Expand All @@ -26,11 +24,6 @@ export const Login = (props: { forgot?: boolean }) => {
custom_logo_alt,
custom_description,
} = useConfig();
const {
data: asset,
loading: queryLoading,
refetch,
} = useQuery(routes.getAsset);
const initForm: any = {
username: "",
password: "",
Expand All @@ -42,7 +35,7 @@ export const Login = (props: { forgot?: boolean }) => {
const [isCaptchaEnabled, setCaptcha] = useState(false);
const { t } = useTranslation();
// display spinner while login is under progress
const [loading, setLoading] = useState(queryLoading);
const [loading, setLoading] = useState(false);
const [forgotPassword, setForgotPassword] = useState(forgot);

// Login form validation
Expand Down Expand Up @@ -91,41 +84,38 @@ export const Login = (props: { forgot?: boolean }) => {
// set loading to false when component is dismounted
useEffect(() => {
return () => {
setLoading(queryLoading);
setLoading(false);
};
}, []);

const handleSubmit = (e: any) => {
const handleSubmit = async (e: any) => {
e.preventDefault();
const valid = validateData();
if (valid) {
// replaces button with spinner
setLoading(queryLoading);
setLoading(true);

refetch().then((resp: any) => {
const res = get(resp, "data", null);
const statusCode = get(resp, "status", "");
if (res && statusCode === 429) {
setCaptcha(true);
// captcha displayed set back to login button
setLoading(queryLoading);
} else if (res && statusCode === 200) {
localStorage.setItem(LocalStorageKeys.accessToken, res.access);
localStorage.setItem(LocalStorageKeys.refreshToken, res.refresh);
const { res, data } = await request(routes.login);
if (res && res.status === 429) {
setCaptcha(true);
// captcha displayed set back to login button
setLoading(false);
} else if (res && res.status === 200 && data) {
localStorage.setItem(LocalStorageKeys.accessToken, data.access);
localStorage.setItem(LocalStorageKeys.refreshToken, data.refresh);

if (
window.location.pathname === "/" ||
window.location.pathname === "/login"
) {
window.location.href = "/facility";
} else {
window.location.href = window.location.pathname.toString();
}
if (
window.location.pathname === "/" ||
window.location.pathname === "/login"
) {
window.location.href = "/facility";
} else {
// error from server set back to login button
setLoading(queryLoading);
window.location.href = window.location.pathname.toString();
}
});
} else {
// error from server set back to login button
setLoading(false);
}
}
};

Expand All @@ -151,26 +141,24 @@ export const Login = (props: { forgot?: boolean }) => {
return form;
};

const handleForgetSubmit = (e: any) => {
const handleForgetSubmit = async (e: any) => {
e.preventDefault();
const valid = validateForgetData();
if (valid) {
setLoading(queryLoading);
refetch().then((resp: any) => {
setLoading(queryLoading);
const res = resp && resp.data;
if (res && res.status === "OK") {
Notification.Success({
msg: t("password_sent"),
});
} else if (res && res.data) {
setErrors(res.data);
} else {
Notification.Error({
msg: t("something_wrong"),
});
}
});
setLoading(true);
const { res, error } = await request(routes.forgotPassword);
setLoading(false);
if (res && res.statusText === "OK") {
Notification.Success({
msg: t("password_sent"),
});
} else if (res && error) {
setErrors(error);
} else {
Notification.Error({
msg: t("something_wrong"),
});
}
}
};

Expand Down
2 changes: 2 additions & 0 deletions src/Redux/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const routes = {
path: "/api/v1/auth/login/",
method: "POST",
noAuth: true,
TRes: Res<JwtTokenObtainPair>(),
},

token_refresh: {
Expand All @@ -56,6 +57,7 @@ const routes = {
forgotPassword: {
path: "/api/v1/password_reset/",
method: "POST",
TRes: Res<IConfig>(),
},

updatePassword: {
Expand Down

0 comments on commit f1ab2a8

Please sign in to comment.