Skip to content

Commit 1041f51

Browse files
committed
[FE] Fix bug with loading main screen v3.0
1 parent d4ddca1 commit 1041f51

File tree

3 files changed

+48
-50
lines changed

3 files changed

+48
-50
lines changed

src/features/authorization/context/auth-context.tsx

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
import {
2-
FC,
3-
ReactNode,
4-
createContext,
5-
useContext,
6-
useEffect,
7-
useState,
8-
} from "react";
1+
import { FC, ReactNode, createContext, useContext, useState } from "react";
92
import { useLocation, useNavigate } from "react-router-dom";
103
import { useSnackbar } from "notistack";
114

@@ -34,7 +27,6 @@ interface AuthContextType {
3427
resetPassword: (email: string) => Promise<void>;
3528
setNewPassword: (newPassword: string) => Promise<void>;
3629
confirmToken: (token: string) => Promise<void>;
37-
isAuth: boolean;
3830
}
3931

4032
const AuthContext = createContext<AuthContextType>({
@@ -45,7 +37,6 @@ const AuthContext = createContext<AuthContextType>({
4537
resetPassword: async () => {},
4638
setNewPassword: async () => {},
4739
confirmToken: async () => {},
48-
isAuth: false,
4940
});
5041

5142
export const useAuth = () => {
@@ -58,7 +49,6 @@ export const useAuth = () => {
5849

5950
export const AuthProvider: FC<IAuthProvider> = ({ children }) => {
6051
const [isLoading, setIsLoading] = useState<boolean>(false);
61-
const [isAuth, setIsAuth] = useState<boolean>(false);
6252
const navigate = useNavigate();
6353
const { enqueueSnackbar } = useSnackbar();
6454
const location = useLocation();
@@ -69,41 +59,20 @@ export const AuthProvider: FC<IAuthProvider> = ({ children }) => {
6959
const [checkToken] = useCheckResetPasswordTokenLazyQuery();
7060

7161
const { loading: rolesLoading } = useUserRolesQuery({
72-
skip: !isAuth,
62+
skip: !(localStorage.getItem("isAuth") === "true"),
7363
onCompleted: (data) => {
7464
userRolesVar(data?.user?.roles);
7565
},
7666
});
7767

78-
useEffect(() => {
79-
const checkAuth = () => {
80-
const auth = localStorage.getItem("isAuth");
81-
82-
if (auth) {
83-
setIsAuth(true);
84-
} else {
85-
setIsAuth(false);
86-
}
87-
88-
setIsLoading(false);
89-
};
90-
91-
checkAuth();
92-
}, []);
93-
94-
useEffect(() => {
95-
if (!rolesLoading && !isLoading) {
96-
setIsLoading(false);
97-
}
98-
}, [rolesLoading, isLoading]);
68+
const loading = isLoading || rolesLoading;
9969

10070
const login = async (username: string, password: string) => {
10171
setIsLoading(true);
10272
await AuthService.login(username, password)
10373
.then((response) => {
10474
if (response.status === RESPONSE_STATUS.SUCCESSFUL) {
10575
localStorage.setItem("isAuth", "true");
106-
setIsAuth(true);
10776
setIsLoading(false);
10877
navigate(ROUTES.HOME);
10978
} else {
@@ -130,7 +99,6 @@ export const AuthProvider: FC<IAuthProvider> = ({ children }) => {
13099
.then((response) => {
131100
if (response.status === RESPONSE_STATUS.SUCCESSFUL) {
132101
localStorage.removeItem("isAuth");
133-
setIsAuth(false);
134102
setIsLoading(false);
135103
navigate(ROUTES.AUTHORIZATION);
136104
} else {
@@ -240,8 +208,7 @@ export const AuthProvider: FC<IAuthProvider> = ({ children }) => {
240208
return (
241209
<AuthContext.Provider
242210
value={{
243-
isAuth,
244-
isLoading,
211+
isLoading: loading,
245212
login,
246213
logout,
247214
signup,

src/features/authorization/views/views.styled.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const StyledWrapper = styled(Stack)(({ theme }) => ({
2525
top: 0,
2626
bottom: "60px",
2727
width: "100%",
28-
height: "100dvh",
28+
height: "calc(100dvh - 60px)",
2929
}));
3030

3131
export const StyledLogo = styled(Logo)(({ theme }) => ({
@@ -76,7 +76,7 @@ export const StyledSignupWrapper = styled(Stack)(({ theme }) => ({
7676
top: 0,
7777
bottom: "60px",
7878
width: "100%",
79-
minHeight: "100dvh",
79+
minHeight: "calc(100dvh - 60px)",
8080
[theme.breakpoints.down(1380)]: {
8181
position: "inherit",
8282
},

src/routes/index.tsx

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,7 @@ interface IRoutnig {
3131
}
3232

3333
const ProtectedRoute: FC<IProtectedRoute> = ({ children }) => {
34-
const { isLoading, isAuth } = useAuth();
35-
36-
if (isLoading) {
37-
return <AppSpinner />;
38-
}
34+
const isAuth = localStorage.getItem("isAuth");
3935

4036
if (!isAuth) {
4137
return <Navigate to="/authorization" replace />;
@@ -74,9 +70,9 @@ const Routing: FC<IRoutnig> = () => {
7470
setErrorBoundaryKey(location.pathname);
7571
}, [location]);
7672

73+
const { isLoading } = useAuth();
7774
const userRoles = userRolesVar();
7875
const usersRoutes = getUserRoutes(userRoles);
79-
const { isLoading } = useAuth();
8076

8177
if (isLoading) {
8278
return <AppSpinner />;
@@ -108,11 +104,46 @@ const Routing: FC<IRoutnig> = () => {
108104
/>
109105
))}
110106
</Route>
111-
<Route path="/authorization" element={<LoginPage />} />
112-
<Route path="/signup" element={<SignUpPage />} />
113-
<Route path="/reset" element={<ResetPage />} />
114-
<Route path="/reset/token" element={<ConfirmTokenPage />} />
115-
<Route path="/reset/password" element={<SetPasswordPage />} />
107+
<Route
108+
path="/authorization"
109+
element={
110+
<Layout isLogging>
111+
<LoginPage />
112+
</Layout>
113+
}
114+
/>
115+
<Route
116+
path="/signup"
117+
element={
118+
<Layout isLogging>
119+
<SignUpPage />
120+
</Layout>
121+
}
122+
/>
123+
<Route
124+
path="/reset"
125+
element={
126+
<Layout isLogging>
127+
<ResetPage />
128+
</Layout>
129+
}
130+
/>
131+
<Route
132+
path="/reset/token"
133+
element={
134+
<Layout isLogging>
135+
<ConfirmTokenPage />
136+
</Layout>
137+
}
138+
/>
139+
<Route
140+
path="/reset/password"
141+
element={
142+
<Layout isLogging>
143+
<SetPasswordPage />
144+
</Layout>
145+
}
146+
/>
116147
<Route
117148
path="*"
118149
element={

0 commit comments

Comments
 (0)