Skip to content

Commit 38ca735

Browse files
authored
Merge pull request #354 from qa-guru/QAGDEV-679
QAGDEV-679 - [FE] Fix bug with loading main screen v2.0
2 parents ea066d7 + d4ddca1 commit 38ca735

File tree

3 files changed

+74
-77
lines changed

3 files changed

+74
-77
lines changed

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

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
1-
import { FC, ReactNode, createContext, useContext, useState } from "react";
1+
import {
2+
FC,
3+
ReactNode,
4+
createContext,
5+
useContext,
6+
useEffect,
7+
useState,
8+
} from "react";
29
import { useLocation, useNavigate } from "react-router-dom";
310
import { useSnackbar } from "notistack";
411

12+
import { userRolesVar } from "cache";
513
import AuthService from "api/rest/auth-service";
614
import {
715
UserCreateInput,
816
useCheckResetPasswordTokenLazyQuery,
917
useCreateUserMutation,
1018
useResetPasswordMutation,
1119
useSetPasswordMutation,
20+
useUserRolesQuery,
1221
} from "api/graphql/generated/graphql";
1322

1423
import { RESPONSE_STATUS, ROUTES } from "../constants";
@@ -25,6 +34,7 @@ interface AuthContextType {
2534
resetPassword: (email: string) => Promise<void>;
2635
setNewPassword: (newPassword: string) => Promise<void>;
2736
confirmToken: (token: string) => Promise<void>;
37+
isAuth: boolean;
2838
}
2939

3040
const AuthContext = createContext<AuthContextType>({
@@ -35,6 +45,7 @@ const AuthContext = createContext<AuthContextType>({
3545
resetPassword: async () => {},
3646
setNewPassword: async () => {},
3747
confirmToken: async () => {},
48+
isAuth: false,
3849
});
3950

4051
export const useAuth = () => {
@@ -47,6 +58,7 @@ export const useAuth = () => {
4758

4859
export const AuthProvider: FC<IAuthProvider> = ({ children }) => {
4960
const [isLoading, setIsLoading] = useState<boolean>(false);
61+
const [isAuth, setIsAuth] = useState<boolean>(false);
5062
const navigate = useNavigate();
5163
const { enqueueSnackbar } = useSnackbar();
5264
const location = useLocation();
@@ -56,12 +68,42 @@ export const AuthProvider: FC<IAuthProvider> = ({ children }) => {
5668
const [createUser] = useCreateUserMutation();
5769
const [checkToken] = useCheckResetPasswordTokenLazyQuery();
5870

71+
const { loading: rolesLoading } = useUserRolesQuery({
72+
skip: !isAuth,
73+
onCompleted: (data) => {
74+
userRolesVar(data?.user?.roles);
75+
},
76+
});
77+
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]);
99+
59100
const login = async (username: string, password: string) => {
60101
setIsLoading(true);
61102
await AuthService.login(username, password)
62103
.then((response) => {
63104
if (response.status === RESPONSE_STATUS.SUCCESSFUL) {
64105
localStorage.setItem("isAuth", "true");
106+
setIsAuth(true);
65107
setIsLoading(false);
66108
navigate(ROUTES.HOME);
67109
} else {
@@ -88,6 +130,7 @@ export const AuthProvider: FC<IAuthProvider> = ({ children }) => {
88130
.then((response) => {
89131
if (response.status === RESPONSE_STATUS.SUCCESSFUL) {
90132
localStorage.removeItem("isAuth");
133+
setIsAuth(false);
91134
setIsLoading(false);
92135
navigate(ROUTES.AUTHORIZATION);
93136
} else {
@@ -197,6 +240,7 @@ export const AuthProvider: FC<IAuthProvider> = ({ children }) => {
197240
return (
198241
<AuthContext.Provider
199242
value={{
243+
isAuth,
200244
isLoading,
201245
login,
202246
logout,

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: "calc(100dvh - 60px)",
28+
height: "100dvh",
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: "calc(100dvh - 60px)",
79+
minHeight: "100dvh",
8080
[theme.breakpoints.down(1380)]: {
8181
position: "inherit",
8282
},

src/routes/index.tsx

Lines changed: 27 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,11 @@ import {
1111
SetPasswordPage,
1212
SignUpPage,
1313
} from "pages/auth";
14-
import {
15-
Maybe,
16-
UserRole,
17-
useUserRolesQuery,
18-
} from "api/graphql/generated/graphql";
14+
import { Maybe, UserRole } from "api/graphql/generated/graphql";
1915
import { AppSpinner } from "shared/components/spinners";
2016
import Layout from "shared/components/layout";
2117
import ScrollPageSectionPage from "pages/scroll-page-section";
18+
import { useAuth } from "features/authorization/context/auth-context";
2219

2320
import StudentRoutes from "./student";
2421
import MentorRoutes from "./mentor";
@@ -34,13 +31,17 @@ interface IRoutnig {
3431
}
3532

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

39-
if (isAuth) {
40-
return <Navigate to="/" replace />;
40+
if (!isAuth) {
41+
return <Navigate to="/authorization" replace />;
4142
}
4243

43-
return <Layout isLogging>{children}</Layout>;
44+
return <>{children}</>;
4445
};
4546

4647
export const roleRoutes: { [key in UserRole]?: ReactElement[] } = {
@@ -63,28 +64,6 @@ export const getUserRoutes = (userRoles: Maybe<Array<Maybe<UserRole>>>) => {
6364
return Array.from(routesSet);
6465
};
6566

66-
export const useUserRoutes = () => {
67-
const location = useLocation();
68-
const isAuthPage =
69-
location.pathname === "/authorization" ||
70-
location.pathname === "/signup" ||
71-
location.pathname === "/reset" ||
72-
location.pathname === "/reset/token" ||
73-
location.pathname === "/reset/password";
74-
75-
const { data, loading } = useUserRolesQuery({
76-
skip: isAuthPage,
77-
onCompleted: (data) => {
78-
userRolesVar(data?.user?.roles);
79-
},
80-
});
81-
82-
const roles = data?.user?.roles ?? [];
83-
const usersRoutes = getUserRoutes(roles);
84-
85-
return { usersRoutes, loading };
86-
};
87-
8867
const Routing: FC<IRoutnig> = () => {
8968
const location = useLocation();
9069
const [errorBoundaryKey, setErrorBoundaryKey] = useState<string>(
@@ -95,9 +74,11 @@ const Routing: FC<IRoutnig> = () => {
9574
setErrorBoundaryKey(location.pathname);
9675
}, [location]);
9776

98-
const { usersRoutes, loading } = useUserRoutes();
77+
const userRoles = userRolesVar();
78+
const usersRoutes = getUserRoutes(userRoles);
79+
const { isLoading } = useAuth();
9980

100-
if (loading) {
81+
if (isLoading) {
10182
return <AppSpinner />;
10283
}
10384

@@ -111,7 +92,14 @@ const Routing: FC<IRoutnig> = () => {
11192
}
11293
>
11394
<Routes>
114-
<Route path="/" element={<Layout />}>
95+
<Route
96+
path="/"
97+
element={
98+
<ProtectedRoute>
99+
<Layout />
100+
</ProtectedRoute>
101+
}
102+
>
115103
{usersRoutes?.map((route) => (
116104
<Route
117105
key={route.key}
@@ -120,46 +108,11 @@ const Routing: FC<IRoutnig> = () => {
120108
/>
121109
))}
122110
</Route>
123-
<Route
124-
path="/authorization"
125-
element={
126-
<ProtectedRoute>
127-
<LoginPage />
128-
</ProtectedRoute>
129-
}
130-
/>
131-
<Route
132-
path="/signup"
133-
element={
134-
<ProtectedRoute>
135-
<SignUpPage />
136-
</ProtectedRoute>
137-
}
138-
/>
139-
<Route
140-
path="/reset"
141-
element={
142-
<ProtectedRoute>
143-
<ResetPage />
144-
</ProtectedRoute>
145-
}
146-
/>
147-
<Route
148-
path="/reset/token"
149-
element={
150-
<ProtectedRoute>
151-
<ConfirmTokenPage />
152-
</ProtectedRoute>
153-
}
154-
/>
155-
<Route
156-
path="/reset/password"
157-
element={
158-
<ProtectedRoute>
159-
<SetPasswordPage />
160-
</ProtectedRoute>
161-
}
162-
/>
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 />} />
163116
<Route
164117
path="*"
165118
element={

0 commit comments

Comments
 (0)