Skip to content

Commit dc0723e

Browse files
chore: formatting
1 parent 06821e5 commit dc0723e

File tree

27 files changed

+2085
-1905
lines changed

27 files changed

+2085
-1905
lines changed

client/src/App.tsx

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { AbilityContext, AuthProvider } from '@/contexts';
2+
import { useAbility, useAuth } from '@/hooks';
13
import NewUser from '@/pages/AdminDashboard/NewUser';
24
import AdminDashboard from '@/pages/AdminDashboard/StaffDashboard';
35
import QrPage from '@/pages/CompletedSurvey/QrPage';
@@ -13,7 +15,6 @@ import Signup from '@/pages/Signup/Signup';
1315
import SurveyComponent from '@/pages/Survey/SurveyComponent';
1416
import SurveyEntryDashboard from '@/pages/SurveyEntryDashboard/SurveyEntryDashboard';
1517
import CssBaseline from '@mui/material/CssBaseline';
16-
import { ProtectedRoute } from './components/ProtectedRoute';
1718
import { ThemeProvider } from '@mui/material/styles';
1819
import {
1920
Navigate,
@@ -22,9 +23,8 @@ import {
2223
Routes
2324
} from 'react-router-dom';
2425

26+
import { ProtectedRoute } from './components/ProtectedRoute';
2527
import { muiTheme } from './theme/muiTheme';
26-
import { AbilityContext, AuthProvider } from '@/contexts';
27-
import { useAbility, useAuth } from '@/hooks';
2828

2929
function App() {
3030
const { isLoggedIn, handleLogin, handleLogout } = useAuth();
@@ -48,111 +48,122 @@ function App() {
4848
<Route
4949
path="/survey/:id/survey"
5050
element={
51-
<ProtectedRoute
52-
isLoggedIn={isLoggedIn}
53-
children={<SurveyComponent />}
51+
<ProtectedRoute
52+
isLoggedIn={isLoggedIn}
53+
children={<SurveyComponent />}
5454
/>
5555
}
5656
/>
5757
<Route path="/signup" element={<Signup />} />
5858
<Route
5959
path="/dashboard"
6060
element={
61-
<ProtectedRoute
62-
isLoggedIn={isLoggedIn}
63-
children={<LandingPage />}
64-
/>
61+
<ProtectedRoute
62+
isLoggedIn={isLoggedIn}
63+
children={<LandingPage />}
64+
/>
6565
}
6666
/>
6767
<Route
6868
path="/survey"
6969
element={
70-
<ProtectedRoute
71-
isLoggedIn={isLoggedIn}
72-
children={<SurveyComponent />}
70+
<ProtectedRoute
71+
isLoggedIn={isLoggedIn}
72+
children={<SurveyComponent />}
7373
/>
7474
}
7575
/>
7676
<Route
7777
path="/admin-dashboard"
7878
element={
79-
<ProtectedRoute
79+
<ProtectedRoute
8080
isLoggedIn={isLoggedIn}
81-
children={<AdminDashboard />}
81+
children={<AdminDashboard />}
8282
/>
8383
}
8484
/>
8585
<Route
8686
path="/admin-edit-profile/:id"
8787
element={
88-
<ProtectedRoute
88+
<ProtectedRoute
8989
isLoggedIn={isLoggedIn}
90-
children={<AdminEditProfile />}
90+
children={<AdminEditProfile />}
9191
/>
9292
}
9393
/>
9494
<Route
9595
path="/add-new-user"
9696
element={
97-
<ProtectedRoute
97+
<ProtectedRoute
9898
isLoggedIn={isLoggedIn}
99-
children={<NewUser />}
99+
children={<NewUser />}
100100
/>
101101
}
102102
/>
103103
<Route
104104
path="/survey-entries"
105105
element={
106-
<ProtectedRoute isLoggedIn={isLoggedIn}
107-
children={<SurveyEntryDashboard />}
106+
<ProtectedRoute
107+
isLoggedIn={isLoggedIn}
108+
children={<SurveyEntryDashboard />}
108109
/>
109110
}
110111
/>
111112
<Route
112113
path="/qrcode"
113114
element={
114-
<ProtectedRoute isLoggedIn={isLoggedIn}
115-
children={<QrPage />}
115+
<ProtectedRoute
116+
isLoggedIn={isLoggedIn}
117+
children={<QrPage />}
116118
/>
117119
}
118120
/>
119121
<Route
120122
path="/past-entries"
121123
element={
122-
<ProtectedRoute isLoggedIn={isLoggedIn}
123-
children={<PastEntries />}
124+
<ProtectedRoute
125+
isLoggedIn={isLoggedIn}
126+
children={<PastEntries />}
124127
/>
125128
}
126129
/>
127130
<Route
128131
path="/survey/:id"
129132
element={
130-
<ProtectedRoute isLoggedIn={isLoggedIn}
131-
children={<SurveyDetails />}
133+
<ProtectedRoute
134+
isLoggedIn={isLoggedIn}
135+
children={<SurveyDetails />}
132136
/>
133137
}
134138
/>
135139
<Route
136140
path="/survey/:id/edit"
137141
element={
138-
<ProtectedRoute isLoggedIn={isLoggedIn}
139-
children={<SurveyEdit />}
142+
<ProtectedRoute
143+
isLoggedIn={isLoggedIn}
144+
children={<SurveyEdit />}
140145
/>
141146
}
142147
/>
143148
<Route
144149
path="/apply-referral"
145150
element={
146-
<ProtectedRoute isLoggedIn={isLoggedIn}
147-
children={<ApplyReferral />}
151+
<ProtectedRoute
152+
isLoggedIn={isLoggedIn}
153+
children={<ApplyReferral />}
148154
/>
149155
}
150156
/>
151157
<Route
152158
path="/view-profile"
153159
element={
154-
<ProtectedRoute isLoggedIn={isLoggedIn}
155-
children={<ViewProfile onLogout={handleLogout} />} // TODO: rm onLogout when updating API fetching to useApi hook
160+
<ProtectedRoute
161+
isLoggedIn={isLoggedIn}
162+
children={
163+
<ViewProfile
164+
onLogout={handleLogout}
165+
/>
166+
} // TODO: rm onLogout when updating API fetching to useApi hook
156167
/>
157168
}
158169
/>

client/src/components/Header.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import '@/styles/header.css';
22

33
import { useEffect, useRef, useState } from 'react';
44

5-
import { useNavigate } from 'react-router-dom';
6-
75
import { useAuthContext } from '@/contexts';
6+
import { useNavigate } from 'react-router-dom';
87

98
export function Header() {
109
const { onLogout } = useAuthContext();
Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
1-
import { Navigate } from 'react-router-dom';
21
import { ReactNode } from 'react';
2+
33
import { Header } from '@/components';
4+
import { Navigate } from 'react-router-dom';
45

56
interface ProtectedRouteProps {
6-
isLoggedIn: boolean;
7-
children: ReactNode;
8-
redirectTo?: string;
7+
isLoggedIn: boolean;
8+
children: ReactNode;
9+
redirectTo?: string;
910
}
1011

11-
export const ProtectedRoute = ({
12-
isLoggedIn,
13-
children,
14-
redirectTo = '/login'
12+
export const ProtectedRoute = ({
13+
isLoggedIn,
14+
children,
15+
redirectTo = '/login'
1516
}: ProtectedRouteProps) => {
16-
return isLoggedIn ? <><Header />{children}</> : <Navigate replace to={redirectTo} />;
17+
return isLoggedIn ? (
18+
<>
19+
<Header />
20+
{children}
21+
</>
22+
) : (
23+
<Navigate replace to={redirectTo} />
24+
);
1725
};

client/src/components/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export { ProtectedRoute } from './ProtectedRoute';
2-
export { Header } from './Header';
2+
export { Header } from './Header';
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { createContext } from 'react';
2-
import { createContextualCan } from '@casl/react';
3-
import { Ability } from '@/permissions/constants';
2+
43
import defineAbilitiesForUser from '@/permissions/abilityBuilder';
4+
import { Ability } from '@/permissions/constants';
5+
import { createContextualCan } from '@casl/react';
56

67
// Default ability will be nonexistent user (i.e. no permissions)
7-
export const AbilityContext = createContext<Ability>(defineAbilitiesForUser('', '', '', []));
8-
export const Can = createContextualCan<Ability>(AbilityContext.Consumer);
8+
export const AbilityContext = createContext<Ability>(
9+
defineAbilitiesForUser('', '', '', [])
10+
);
11+
export const Can = createContextualCan<Ability>(AbilityContext.Consumer);
Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
1-
import { createContext, useContext, ReactNode } from 'react';
1+
import { createContext, ReactNode, useContext } from 'react';
22

33
interface AuthContextValue {
4-
onLogout: () => void;
5-
isLoggedIn: boolean;
4+
onLogout: () => void;
5+
isLoggedIn: boolean;
66
}
77

88
const AuthContext = createContext<AuthContextValue | undefined>(undefined);
99

1010
export const useAuthContext = () => {
11-
const context = useContext(AuthContext);
12-
if (!context) {
13-
throw new Error('useAuthContext must be used within AuthProvider');
14-
}
15-
return context;
11+
const context = useContext(AuthContext);
12+
if (!context) {
13+
throw new Error('useAuthContext must be used within AuthProvider');
14+
}
15+
return context;
1616
};
1717

18-
export const AuthProvider = ({
19-
children,
20-
value
21-
}: {
22-
children: ReactNode;
23-
value: AuthContextValue
18+
export const AuthProvider = ({
19+
children,
20+
value
21+
}: {
22+
children: ReactNode;
23+
value: AuthContextValue;
2424
}) => {
25-
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
26-
};
25+
return (
26+
<AuthContext.Provider value={value}>{children}</AuthContext.Provider>
27+
);
28+
};

client/src/contexts/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export { AbilityContext } from './AbilityContext';
2-
export { AuthProvider, useAuthContext } from './AuthContext';
2+
export { AuthProvider, useAuthContext } from './AuthContext';

client/src/hooks/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export { useAbility } from './useAbility';
22
export { useAuth } from './useAuth';
3-
export { useApi } from './useApi';
3+
export { useApi } from './useApi';

client/src/hooks/useAbility.tsx

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1-
import { useAuthStore } from '@/stores';
21
import { useMemo } from 'react';
2+
33
import defineAbilitiesForUser from '@/permissions/abilityBuilder';
4+
import { useAuthStore } from '@/stores';
45

56
export const useAbility = () => {
6-
const { userRole, userObjectId, lastestLocationObjectId, permissions } = useAuthStore();
7-
8-
// updates whenever our auth store updates
9-
return useMemo(
10-
() => defineAbilitiesForUser(
11-
userRole ?? '',
12-
userObjectId ?? '',
13-
lastestLocationObjectId ?? '',
14-
permissions
15-
),
16-
[userRole, userObjectId, lastestLocationObjectId, permissions]
17-
);
7+
const { userRole, userObjectId, lastestLocationObjectId, permissions } =
8+
useAuthStore();
9+
10+
// updates whenever our auth store updates
11+
return useMemo(
12+
() =>
13+
defineAbilitiesForUser(
14+
userRole ?? '',
15+
userObjectId ?? '',
16+
lastestLocationObjectId ?? '',
17+
permissions
18+
),
19+
[userRole, userObjectId, lastestLocationObjectId, permissions]
20+
);
1821
};

0 commit comments

Comments
 (0)