Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cicd #76

Closed
wants to merge 4 commits into from
Closed

Cicd #76

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ jobs:
with:
node-version: "18" # You can specify the Node.js version you need

- name: Checkout frontend
- run: cd frontend

- name: Install dependencies
run: npm install

Expand Down
2 changes: 1 addition & 1 deletion UserService/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
}
}
}
2 changes: 1 addition & 1 deletion frontend/src/auth/MaintainerGuard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface AuthGuardProps {

export default function MaintainerGuard({ children }: AuthGuardProps) {
const { user } = useAuth();
if (!(user?.role == 'maintainer')) {
if (user?.role !== "maintainer") {
return <Navigate to="/login" />;
}
return <>{children}</>;
Expand Down
83 changes: 49 additions & 34 deletions frontend/src/auth/auth.context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { createAdminUser, createUser, getUser, UserModel } from "../api/user";

interface AuthContextData {
user: UserModel | undefined;
setUser: any | undefined,
setUser: any | undefined;
activeUser: User | undefined;
error: string;
signUp: (email: string, password: string) => void;
Expand All @@ -38,7 +38,7 @@ const AuthContext = createContext<AuthContextData>({
signUpAdmin: (email: string, password: string) => undefined,
login: (email: string, password: string) => undefined,
logout: () => undefined,
removeAccount: () => undefined
removeAccount: () => undefined,
});

export function AuthContextProvider({ children }: AuthContextProviderProps) {
Expand All @@ -59,7 +59,7 @@ export function AuthContextProvider({ children }: AuthContextProviderProps) {
throw new Error("user returned without email");
}
const fetchedUser = await createUser(u.email);
setActiveUser(u)
setActiveUser(u);
setUser(fetchedUser.data);
} catch (e) {
if (e instanceof AxiosError && e.response) {
Expand All @@ -72,32 +72,27 @@ export function AuthContextProvider({ children }: AuthContextProviderProps) {
[setUser]
);

const signUpAdmin = useCallback(
async (email: string, password: string) => {
try {
const response = await registerUser({
email: email,
password: password,
});
const u: User = response.data.user;
if (!u.email) {
throw new Error("user returned without email");
}
console.log('creating admin')
const fetchedUser = await createAdminUser(u.email);
console.log('admin created')
// setActiveUser(u)
// setUser(fetchedUser.data);
} catch (e) {
if (e instanceof AxiosError && e.response) {
setError(e.response.data.code);
} else if (e instanceof Error) {
setError(e.message);
}
const signUpAdmin = useCallback(async (email: string, password: string) => {
try {
const response = await registerUser({
email: email,
password: password,
});
const u: User = response.data.user;
if (!u.email) {
throw new Error("user returned without email");
}
},
[setUser]
);
console.log("creating admin");
await createAdminUser(u.email);
console.log("admin created");
} catch (e) {
if (e instanceof AxiosError && e.response) {
setError(e.response.data.code);
} else if (e instanceof Error) {
setError(e.message);
}
}
}, []);

const login = useCallback(
async (email: string, password: string) => {
Expand All @@ -107,7 +102,7 @@ export function AuthContextProvider({ children }: AuthContextProviderProps) {
if (!u.email) {
throw new Error("user returned without email");
}
setActiveUser(u)
setActiveUser(u);
const fetchedUser = await getUser(u.email);
setUser(fetchedUser.data);
navigate("/home", { replace: true });
Expand All @@ -126,7 +121,7 @@ export function AuthContextProvider({ children }: AuthContextProviderProps) {
try {
await signOut();
setUser(undefined);
setActiveUser(undefined)
setActiveUser(undefined);
navigate("/", { replace: true });
} catch (e) {
if (e instanceof AxiosError && e.response) {
Expand All @@ -143,7 +138,7 @@ export function AuthContextProvider({ children }: AuthContextProviderProps) {
await deleteUser();
}
setUser(undefined);
setActiveUser(undefined)
setActiveUser(undefined);
navigate("/", { replace: true });
} catch (e) {
if (e instanceof AxiosError && e.response) {
Expand All @@ -152,11 +147,31 @@ export function AuthContextProvider({ children }: AuthContextProviderProps) {
setError(e.message);
}
}
}, [setUser, navigate]);
}, [setUser, navigate, activeUser]);

const authContextProviderValue = useMemo(
() => ({ user, setUser, activeUser, error, signUp, signUpAdmin, login, logout, removeAccount }),
[user, setUser, activeUser, error, signUp, signUpAdmin, login, logout, removeAccount]
() => ({
user,
setUser,
activeUser,
error,
signUp,
signUpAdmin,
login,
logout,
removeAccount,
}),
[
user,
setUser,
activeUser,
error,
signUp,
signUpAdmin,
login,
logout,
removeAccount,
]
);

return (
Expand Down
170 changes: 97 additions & 73 deletions frontend/src/components/AdminUsersTable.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
import React, { useEffect, useState } from 'react';
import { Table, TableBody, TableCell, TableContainer, TableHead, TableRow, Paper, Select, MenuItem, TablePagination, SelectChangeEvent, Grid, Typography, Button } from '@mui/material';
import { useData } from '../data/data.context';
import { updateUser } from '../api/user';
import React, { useEffect, useState } from "react";
import {
Table,
TableBody,
TableCell,
TableContainer,
TableHead,
TableRow,
Paper,
Select,
MenuItem,
TablePagination,
SelectChangeEvent,
Grid,
Typography,
Button,
} from "@mui/material";
import { useData } from "../data/data.context";
import { updateUser } from "../api/user";

interface AdminUser {
email: string;
Expand All @@ -17,32 +32,33 @@ const ITEMS_PER_PAGE_OPTIONS = [5, 10]; // Number of items to display per page
const AdminUsersTable: React.FC = () => {
const [adminUsersData, setAdminUsers] = useState<AdminUser[]>([]);
const [currentPage, setCurrentPage] = useState<number>(1);
const [itemsPerPage, setItemsPerPage] = useState<number>(ITEMS_PER_PAGE_OPTIONS[0]);
const [itemsPerPage, setItemsPerPage] = useState<number>(
ITEMS_PER_PAGE_OPTIONS[0]
);
const { adminUsers, getAdminUsers } = useData();

useEffect(() => {
async function getAdmins() {
getAdminUsers();
}
getAdmins();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

useEffect(() => {
setAdminUsers(adminUsers);
console.log('check')
console.log("check");
}, [adminUsers]);

const handlePageChange = (event: unknown, newPage: number) => {
setCurrentPage(newPage);
};

const handleChangeItemsPerPage = (
event: SelectChangeEvent<unknown>,
) => {
const handleChangeItemsPerPage = (event: SelectChangeEvent<unknown>) => {
setItemsPerPage(event.target.value as number);
setCurrentPage(1);
};

const removeAdmin = async (admin: AdminUser, index: number) => {
await updateUser({
email: admin?.email ? admin?.email : "",
Expand All @@ -51,78 +67,86 @@ const AdminUsersTable: React.FC = () => {
major: admin?.major ? admin.major : "",
role: "user",
completed: admin?.completed ? admin.completed : 0,
})
});
setAdminUsers(adminUsersData.filter((e, i) => i !== index));
}
};

const indexOfLastAdmin = currentPage * itemsPerPage;
const indexOfFirstAdmin = indexOfLastAdmin - itemsPerPage;
const currentAdmins = adminUsersData.slice(indexOfFirstAdmin, indexOfLastAdmin);
const currentAdmins = adminUsersData.slice(
indexOfFirstAdmin,
indexOfLastAdmin
);

return (
<><div style={{ maxHeight: '400px', overflowY: 'auto', width: '100%' }}>

<TableContainer component={Paper} style={{ margin: '10px', padding: '10px' }}>
<>
<div style={{ maxHeight: "400px", overflowY: "auto", width: "100%" }}>
<TableContainer
component={Paper}
style={{ margin: "10px", padding: "10px" }}
>
<Grid container>
<Grid item xs={3}>
<Typography fontWeight={600}>Administrators:</Typography>
<Grid item xs={3}>
<Typography fontWeight={600}>Administrators:</Typography>
</Grid>
</Grid>
</Grid>
<Table style={{ minWidth: 650, fontSize: '14px' }}>
<TableHead>
<TableRow >
<TableCell>Email</TableCell>
<TableCell>Name</TableCell>
<TableCell>Year</TableCell>
<TableCell>Major</TableCell>
{/* <TableCell>Role</TableCell> */}
{/* <TableCell>Completed</TableCell> */}
<TableCell>Remove Privileges</TableCell>
</TableRow>
</TableHead>
<TableBody>
{currentAdmins.map((admin:AdminUser, index:number) => (
<TableRow key={index}>
<TableCell>{admin.email}</TableCell>
<TableCell>{admin.name}</TableCell>
<TableCell>{admin.year}</TableCell>
<TableCell>{admin.major}</TableCell>
<TableCell>
<Button onClick={() => removeAdmin(admin, index)}>
Confirm
</Button>
</TableCell>
{/* <TableCell>{admin.role}</TableCell> */}
{/* <TableCell>{admin.completed}</TableCell> */}
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
</div><div style={{ display: 'flex', alignItems: 'center' }}>
<Select
value={itemsPerPage}
onChange={handleChangeItemsPerPage}
style={{ marginTop: '10px' }}
>
{ITEMS_PER_PAGE_OPTIONS.map((option) => (
<MenuItem key={option} value={option}>
{`${option} per page`}
</MenuItem>
))}
</Select>
<Table style={{ minWidth: 650, fontSize: "14px" }}>
<TableHead>
<TableRow>
<TableCell>Email</TableCell>
<TableCell>Name</TableCell>
<TableCell>Year</TableCell>
<TableCell>Major</TableCell>
{/* <TableCell>Role</TableCell> */}
{/* <TableCell>Completed</TableCell> */}
<TableCell>Remove Privileges</TableCell>
</TableRow>
</TableHead>
<TableBody>
{currentAdmins.map((admin: AdminUser, index: number) => (
<TableRow key={index}>
<TableCell>{admin.email}</TableCell>
<TableCell>{admin.name}</TableCell>
<TableCell>{admin.year}</TableCell>
<TableCell>{admin.major}</TableCell>
<TableCell>
<Button onClick={() => removeAdmin(admin, index)}>
Confirm
</Button>
</TableCell>

{/* <TableCell>{admin.role}</TableCell> */}
{/* <TableCell>{admin.completed}</TableCell> */}
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
</div>
<div style={{ display: "flex", alignItems: "center" }}>
<Select
value={itemsPerPage}
onChange={handleChangeItemsPerPage}
style={{ marginTop: "10px" }}
>
{ITEMS_PER_PAGE_OPTIONS.map((option) => (
<MenuItem key={option} value={option}>
{`${option} per page`}
</MenuItem>
))}
</Select>

<TablePagination
rowsPerPageOptions={[]}
component="div"
count={adminUsersData.length}
rowsPerPage={itemsPerPage}
page={currentPage - 1}
onPageChange={handlePageChange} />
</div></>

<TablePagination
rowsPerPageOptions={[]}
component="div"
count={adminUsersData.length}
rowsPerPage={itemsPerPage}
page={currentPage - 1}
onPageChange={handlePageChange}
/>
</div>
</>
);
};

export default AdminUsersTable;
export default AdminUsersTable;
Loading