modify login page
This commit is contained in:
1 parent
402361ce97
commit
0508fde86c
1 file changed
+71
-69
@@ -1,4 +1,4 @@
|
|||||||
import { useState, useEffect } from "react";
|
import { useState, useEffect, useCallback } from "react";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import { Box, Stack, Typography, Alert, Button } from "@mui/material";
|
import { Box, Stack, Typography, Alert, Button } from "@mui/material";
|
||||||
import { useTheme } from "@emotion/react";
|
import { useTheme } from "@emotion/react";
|
||||||
@@ -44,82 +44,69 @@ const Login = () => {
|
|||||||
});
|
});
|
||||||
const [errors, setErrors] = useState({});
|
const [errors, setErrors] = useState({});
|
||||||
const [step, setStep] = useState(0);
|
const [step, setStep] = useState(0);
|
||||||
|
|
||||||
|
// State variables for backend connectivity status and loading state
|
||||||
const [backendReachable, setBackendReachable] = useState(true);
|
const [backendReachable, setBackendReachable] = useState(true);
|
||||||
const [isCheckingConnection, setIsCheckingConnection] = useState(false);
|
const [isCheckingConnection, setIsCheckingConnection] = useState(false);
|
||||||
|
|
||||||
|
// Function to check if the backend server is reachable and handle connectivity status
|
||||||
|
// Wrapped in useCallback to prevent recreation on each render
|
||||||
|
const checkConnectivity = useCallback(async (isRetry = false) => {
|
||||||
|
setIsCheckingConnection(true);
|
||||||
|
try {
|
||||||
|
const isReachable = await networkService.checkBackendReachability();
|
||||||
|
setBackendReachable(isReachable);
|
||||||
|
|
||||||
|
if (isReachable) {
|
||||||
|
networkService
|
||||||
|
.doesSuperAdminExist()
|
||||||
|
.then((response) => {
|
||||||
|
if (response.data.data === false) {
|
||||||
|
navigate("/register");
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
logger.error(error);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Only show reconnection toast if this was a retry attempt
|
||||||
|
if (isRetry) {
|
||||||
|
createToast({
|
||||||
|
body: t("backendReconnected"),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else if (isRetry) {
|
||||||
|
// Only show still unreachable toast if this was a retry attempt
|
||||||
|
createToast({
|
||||||
|
body: t("backendStillUnreachable"),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
logger.error("Error checking backend connectivity:", error);
|
||||||
|
setBackendReachable(false);
|
||||||
|
|
||||||
|
if (isRetry) {
|
||||||
|
createToast({
|
||||||
|
body: t("backendConnectionError"),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
setIsCheckingConnection(false);
|
||||||
|
}
|
||||||
|
}, [navigate, t]);
|
||||||
|
|
||||||
|
// Function to handle retry button click
|
||||||
|
const handleRetry = () => checkConnectivity(true);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (authToken) {
|
if (authToken) {
|
||||||
navigate("/uptime");
|
navigate("/uptime");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check backend connectivity first
|
// Initial connectivity check
|
||||||
const handleRetry = async () => {
|
|
||||||
setIsCheckingConnection(true);
|
|
||||||
try {
|
|
||||||
const isReachable = await networkService.checkBackendReachability();
|
|
||||||
setBackendReachable(isReachable);
|
|
||||||
|
|
||||||
// If backend is now reachable, proceed with admin check
|
|
||||||
if (isReachable) {
|
|
||||||
networkService
|
|
||||||
.doesSuperAdminExist()
|
|
||||||
.then((response) => {
|
|
||||||
if (response.data.data === false) {
|
|
||||||
navigate("/register");
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
logger.error(error);
|
|
||||||
});
|
|
||||||
createToast({
|
|
||||||
body: t("backendReconnected"),
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
createToast({
|
|
||||||
body: t("backendStillUnreachable"),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
logger.error("Error checking backend connectivity:", error);
|
|
||||||
setBackendReachable(false);
|
|
||||||
createToast({
|
|
||||||
body: t("backendConnectionError"),
|
|
||||||
});
|
|
||||||
} finally {
|
|
||||||
setIsCheckingConnection(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const checkConnectivity = async () => {
|
|
||||||
setIsCheckingConnection(true);
|
|
||||||
try {
|
|
||||||
const isReachable = await networkService.checkBackendReachability();
|
|
||||||
setBackendReachable(isReachable);
|
|
||||||
|
|
||||||
// Only proceed with admin check if backend is reachable
|
|
||||||
if (isReachable) {
|
|
||||||
networkService
|
|
||||||
.doesSuperAdminExist()
|
|
||||||
.then((response) => {
|
|
||||||
if (response.data.data === false) {
|
|
||||||
navigate("/register");
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
logger.error(error);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
logger.error("Error checking backend connectivity:", error);
|
|
||||||
setBackendReachable(false);
|
|
||||||
} finally {
|
|
||||||
setIsCheckingConnection(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
checkConnectivity();
|
checkConnectivity();
|
||||||
}, [authToken, navigate]);
|
}, [authToken, navigate, checkConnectivity]);
|
||||||
|
|
||||||
const handleChange = (event) => {
|
const handleChange = (event) => {
|
||||||
const { value, id } = event.target;
|
const { value, id } = event.target;
|
||||||
@@ -279,7 +266,21 @@ const Login = () => {
|
|||||||
>
|
>
|
||||||
{!backendReachable ? (
|
{!backendReachable ? (
|
||||||
<Stack spacing={2} alignItems="center">
|
<Stack spacing={2} alignItems="center">
|
||||||
<Alert severity="error" sx={{ width: '100%' }}>
|
<Alert
|
||||||
|
severity="error"
|
||||||
|
sx={{
|
||||||
|
width: '100%',
|
||||||
|
'& .MuiAlert-message': {
|
||||||
|
display: 'flex',
|
||||||
|
alignItems: 'center'
|
||||||
|
},
|
||||||
|
'& .MuiAlert-icon': {
|
||||||
|
display: 'flex',
|
||||||
|
alignItems: 'center',
|
||||||
|
marginRight: theme.spacing(2)
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>
|
||||||
{t("backendUnreachable")}
|
{t("backendUnreachable")}
|
||||||
</Alert>
|
</Alert>
|
||||||
<Typography variant="body1" align="center">
|
<Typography variant="body1" align="center">
|
||||||
@@ -287,9 +288,10 @@ const Login = () => {
|
|||||||
</Typography>
|
</Typography>
|
||||||
<Button
|
<Button
|
||||||
variant="contained"
|
variant="contained"
|
||||||
color="primary"
|
color="accent"
|
||||||
onClick={handleRetry}
|
onClick={handleRetry}
|
||||||
disabled={isCheckingConnection}
|
disabled={isCheckingConnection}
|
||||||
|
|
||||||
>
|
>
|
||||||
{isCheckingConnection ? t("retryingConnection") : t("retryConnection")}
|
{isCheckingConnection ? t("retryingConnection") : t("retryConnection")}
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
Reference in new issue
Block a user