Skip to content

Commit 98ca3b9

Browse files
taken suggestion from ai and fixed test cases
1 parent e4e3401 commit 98ca3b9

File tree

8 files changed

+148
-38
lines changed

8 files changed

+148
-38
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
[Admin Docs](/)
2+
3+
***
4+
5+
# Enumeration: UserRole
6+
7+
Defined in: [src/components/CheckIn/types.ts:45](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/components/CheckIn/types.ts#L45)
8+
9+
## Enumeration Members
10+
11+
### ADMIN
12+
13+
> **ADMIN**: `"admin"`
14+
15+
Defined in: [src/components/CheckIn/types.ts:47](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/components/CheckIn/types.ts#L47)
16+
17+
***
18+
19+
### SUPER\_ADMIN
20+
21+
> **SUPER\_ADMIN**: `"superAdmin"`
22+
23+
Defined in: [src/components/CheckIn/types.ts:48](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/components/CheckIn/types.ts#L48)
24+
25+
***
26+
27+
### USER
28+
29+
> **USER**: `"user"`
30+
31+
Defined in: [src/components/CheckIn/types.ts:46](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/components/CheckIn/types.ts#L46)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[Admin Docs](/)
2+
3+
***
4+
5+
# Type Alias: VerifyRoleResponse
6+
7+
> **VerifyRoleResponse**: `object`
8+
9+
Defined in: [src/components/CheckIn/types.ts:51](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/components/CheckIn/types.ts#L51)
10+
11+
## Type declaration
12+
13+
### verifyRole
14+
15+
> **verifyRole**: `object`
16+
17+
#### verifyRole.isAuthorized
18+
19+
> **isAuthorized**: `boolean`
20+
21+
#### verifyRole.role
22+
23+
> **role**: [`UserRole`](../enumerations/UserRole.md)

docs/docs/auto-docs/components/SecuredRoute/SecuredRoute/functions/default.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
> **default**(): `Element`
88
9-
Defined in: [src/components/SecuredRoute/SecuredRoute.tsx:18](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/components/SecuredRoute/SecuredRoute.tsx#L18)
9+
Defined in: [src/components/SecuredRoute/SecuredRoute.tsx:19](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/components/SecuredRoute/SecuredRoute.tsx#L19)
1010

1111
A route guard that checks if the user is logged in and has the necessary permissions.
1212

docs/docs/auto-docs/components/UserPortal/SecuredRouteForUser/SecuredRouteForUser/functions/default.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
> **default**(): `Element`
88
9-
Defined in: [src/components/UserPortal/SecuredRouteForUser/SecuredRouteForUser.tsx:16](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/components/UserPortal/SecuredRouteForUser/SecuredRouteForUser.tsx#L16)
9+
Defined in: [src/components/UserPortal/SecuredRouteForUser/SecuredRouteForUser.tsx:18](https://github.com/PalisadoesFoundation/talawa-admin/blob/main/src/components/UserPortal/SecuredRouteForUser/SecuredRouteForUser.tsx#L18)
1010

1111
A component that guards routes by checking if the user is logged in.
1212
If the user is logged in and does not have 'AdminFor' set, the child routes are rendered.

src/components/CheckIn/types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,15 @@ export interface InterfaceTableData {
4242
id: string;
4343
checkInData: InterfaceTableCheckIn;
4444
}
45+
export enum UserRole {
46+
USER = 'user',
47+
ADMIN = 'admin',
48+
SUPER_ADMIN = 'superAdmin',
49+
}
50+
51+
export type VerifyRoleResponse = {
52+
verifyRole: {
53+
isAuthorized: boolean;
54+
role: UserRole;
55+
};
56+
};

src/components/SecuredRoute/SecuredRoute.tsx

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useQuery } from '@apollo/client';
2+
import { UserRole, type VerifyRoleResponse } from 'components/CheckIn/types';
23
import { VERIFY_ROLE } from 'GraphQl/Queries/Queries';
34
import React, { useEffect } from 'react';
45
import { Navigate, Outlet, useLocation } from 'react-router-dom';
@@ -17,14 +18,17 @@ const { getItem, setItem } = useLocalStorage();
1718
*/
1819
const SecuredRoute = (): JSX.Element => {
1920
const location = useLocation();
20-
const { data, loading, error, refetch } = useQuery(VERIFY_ROLE, {
21-
skip: !getItem('token'),
22-
context: {
23-
headers: {
24-
Authorization: `Bearer ${getItem('token')}`,
21+
const { data, loading, error, refetch } = useQuery<VerifyRoleResponse>(
22+
VERIFY_ROLE,
23+
{
24+
skip: !getItem('token'),
25+
context: {
26+
headers: {
27+
Authorization: `Bearer ${getItem('token')}`,
28+
},
2529
},
2630
},
27-
});
31+
);
2832
const [token, setToken] = React.useState(getItem('token'));
2933

3034
useEffect(() => {
@@ -43,13 +47,13 @@ const SecuredRoute = (): JSX.Element => {
4347
} else if (error) {
4448
return <div>Error During Routing ...</div>;
4549
} else {
46-
const role = data?.verifyRole?.role || '';
47-
const isLoggedIn = data?.verifyRole?.isAuthorized ?? false;
50+
const { isAuthorized = false, role = '' } = (data as VerifyRoleResponse)
51+
.verifyRole;
4852
const restrictedRoutesForAdmin = ['/member', '/users', '/communityProfile'];
49-
if (isLoggedIn) {
50-
if (role == 'superAdmin') {
53+
if (isAuthorized) {
54+
if (role == UserRole.SUPER_ADMIN) {
5155
return <Outlet />;
52-
} else if (role == 'admin') {
56+
} else if (role == UserRole.ADMIN) {
5357
if (restrictedRoutesForAdmin.includes(location.pathname)) {
5458
return <PageNotFound />;
5559
}

src/components/UserPortal/SecuredRouteForUser/SecuredRouteForUser.tsx

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { useQuery } from '@apollo/client';
2+
import type { VerifyRoleResponse } from 'components/CheckIn/types';
3+
import { UserRole } from 'components/CheckIn/types';
24
import { VERIFY_ROLE } from 'GraphQl/Queries/Queries';
35
import React, { useEffect } from 'react';
46
import { Navigate, Outlet } from 'react-router-dom';
@@ -16,27 +18,44 @@ import useLocalStorage from 'utils/useLocalstorage';
1618
const SecuredRouteForUser = (): JSX.Element => {
1719
// Custom hook to interact with local storage
1820
const { getItem } = useLocalStorage();
19-
const { data, loading, error, refetch } = useQuery(VERIFY_ROLE, {
20-
context: {
21-
headers: {
22-
Authorization: `Bearer ${getItem('token')}`,
21+
const { data, loading, error, refetch } = useQuery<VerifyRoleResponse>(
22+
VERIFY_ROLE,
23+
{
24+
skip: !getItem('token'),
25+
context: {
26+
headers: {
27+
Authorization: `Bearer ${getItem('token')}`,
28+
},
2329
},
2430
},
25-
});
31+
);
32+
const [token, setToken] = React.useState(getItem('token'));
33+
34+
useEffect(() => {
35+
const newToken = getItem('token');
36+
if (newToken !== token) {
37+
setToken(newToken);
38+
}
39+
}, []);
40+
2641
useEffect(() => {
2742
refetch(); // Refetch when token updates
28-
}, [getItem('token')]);
43+
}, [token]);
2944

3045
if (loading) {
3146
return <div> Loading.....</div>;
3247
} else if (error) {
3348
return <div>Error During Routing ...</div>;
3449
} else {
35-
const role = data?.verifyRole?.role || '';
36-
const isLoggedIn = data?.verifyRole?.isAuthorized ?? false;
50+
const { isAuthorized = false, role = '' } = (data as VerifyRoleResponse)
51+
.verifyRole;
3752

38-
if (isLoggedIn) {
39-
if (role == 'user' || role == 'admin' || role == 'superAdmin') {
53+
if (isAuthorized) {
54+
if (
55+
role == UserRole.USER ||
56+
role == UserRole.ADMIN ||
57+
UserRole.SUPER_ADMIN
58+
) {
4059
return <Outlet />;
4160
} else {
4261
return <PageNotFound />;

src/screens/PageNotFound/PageNotFound.spec.tsx

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,38 @@ import PageNotFound from './PageNotFound';
99
import i18nForTest from 'utils/i18nForTest';
1010
import useLocalStorage from 'utils/useLocalstorage';
1111
import { it, expect, describe } from 'vitest';
12+
import type { NormalizedCacheObject } from '@apollo/client';
13+
import {
14+
ApolloClient,
15+
ApolloLink,
16+
ApolloProvider,
17+
InMemoryCache,
18+
} from '@apollo/client';
1219
const { setItem } = useLocalStorage();
1320

21+
const link = new ApolloLink((operation, forward) => {
22+
return forward(operation);
23+
});
24+
25+
const client: ApolloClient<NormalizedCacheObject> = new ApolloClient({
26+
cache: new InMemoryCache(),
27+
link: ApolloLink.from([link]),
28+
});
29+
1430
describe('Testing Page not found component', () => {
1531
it('should render component properly for User', () => {
16-
//setItem('AdminFor', undefined);
1732
render(
18-
<BrowserRouter>
19-
<Provider store={store}>
20-
<I18nextProvider i18n={i18nForTest}>
21-
<PageNotFound />
22-
</I18nextProvider>
23-
</Provider>
24-
</BrowserRouter>,
33+
<ApolloProvider client={client}>
34+
{' '}
35+
{/* ✅ Add ApolloProvider */}
36+
<BrowserRouter>
37+
<Provider store={store}>
38+
<I18nextProvider i18n={i18nForTest}>
39+
<PageNotFound />
40+
</I18nextProvider>
41+
</Provider>
42+
</BrowserRouter>
43+
</ApolloProvider>,
2544
);
2645

2746
expect(screen.getByText(/Talawa User/i)).toBeInTheDocument();
@@ -40,13 +59,15 @@ describe('Testing Page not found component', () => {
4059
},
4160
]);
4261
render(
43-
<BrowserRouter>
44-
<Provider store={store}>
45-
<I18nextProvider i18n={i18nForTest}>
46-
<PageNotFound />
47-
</I18nextProvider>
48-
</Provider>
49-
</BrowserRouter>,
62+
<ApolloProvider client={client}>
63+
<BrowserRouter>
64+
<Provider store={store}>
65+
<I18nextProvider i18n={i18nForTest}>
66+
<PageNotFound />
67+
</I18nextProvider>
68+
</Provider>
69+
</BrowserRouter>
70+
</ApolloProvider>,
5071
);
5172

5273
expect(screen.getByText(/Talawa Admin Portal/i)).toBeInTheDocument();

0 commit comments

Comments
 (0)