Skip to content

Commit

Permalink
Merge pull request #18 from fga-eps-mds/front_fix_permissions
Browse files Browse the repository at this point in the history
permissions handle
  • Loading branch information
guipeeix7 authored Jan 11, 2025
2 parents 8747f65 + 71c24e7 commit 660245b
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Components/SideBar/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default function SideBar() {
const permissions = usePermissions();

Check failure on line 22 in src/Components/SideBar/index.jsx

View workflow job for this annotation

GitHub Actions / lint

'permissions' is assigned a value but never used
const [role, setRole] = useState("");

useEffect(() => {}, [navigate]);
useEffect(() => { }, [navigate]);

const handleItemClick = async (user) => {
if (user) {
Expand Down Expand Up @@ -80,6 +80,15 @@ export default function SideBar() {
setIsSideBarOpen(false);
}}
/>,
<SideButton
// hidden={checkModule(permissions, "benefits") ? "flex" : "none"}
key="permissions"
text="PERMISSIONS"
onClick={() => {
navigate("/permissions");
setIsSideBarOpen(false);
}}
/>,
<SideButton
hidden={user ? "none" : "flex"}
key="login"
Expand Down
126 changes: 126 additions & 0 deletions src/Pages/Protected/Permissions/permissionsHandler.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import React, { useState, useEffect } from 'react';

Check failure on line 1 in src/Pages/Protected/Permissions/permissionsHandler.jsx

View workflow job for this annotation

GitHub Actions / lint

'React' is defined but never used
import axios from 'axios';

Check failure on line 2 in src/Pages/Protected/Permissions/permissionsHandler.jsx

View workflow job for this annotation

GitHub Actions / lint

'axios' is defined but never used
import { Box, Button, TextField, Typography, Table, TableBody, TableCell, TableContainer, TableHead, TableRow, Paper, IconButton } from '@mui/material';
import { Delete, Edit, Search } from '@mui/icons-material';

Check failure on line 4 in src/Pages/Protected/Permissions/permissionsHandler.jsx

View workflow job for this annotation

GitHub Actions / lint

'Search' is defined but never used
import { APIUsers } from "./../../../Services/BaseService/index";
import { getToken, getUser } from "./../../../Services/Functions/loader";

Check failure on line 6 in src/Pages/Protected/Permissions/permissionsHandler.jsx

View workflow job for this annotation

GitHub Actions / lint

'getUser' is defined but never used


const PermissionCRUD = () => {
const [permissions, setPermissions] = useState([]);
const [form, setForm] = useState({ name: '' });
const [searchQuery, setSearchQuery] = useState('');

Check failure on line 12 in src/Pages/Protected/Permissions/permissionsHandler.jsx

View workflow job for this annotation

GitHub Actions / lint

'setSearchQuery' is assigned a value but never used
const [search, setSearch] = useState("");

Check failure on line 13 in src/Pages/Protected/Permissions/permissionsHandler.jsx

View workflow job for this annotation

GitHub Actions / lint

'setSearch' is assigned a value but never used
const [editId, setEditId] = useState(null);

useEffect(() => {
fetchPermissions();
}, []);

const fetchPermissions = async () => {
try {
const response = await APIUsers.get('permission');
setPermissions(response.data);
} catch (error) {
console.error('Error fetching permissions:', error);
}
};

const handleSubmit = async (e) => {
e.preventDefault();
try {
if (editId) {
await APIUsers.patch(`permission/patch/${editId}`, form);
} else {
await APIUsers.post('permission/create/', form);
}
setForm({ name: '' });
setEditId(null);
fetchPermissions();
} catch (error) {
console.error('Error saving permission:', error);
}
};

const handleEdit = (permission) => {
setForm({ name: permission.name });
setEditId(permission._id);
};

const handleDelete = async (id) => {
try {
await APIUsers.delete(`permission/delete/${id}`);
fetchPermissions();
} catch (error) {
console.error('Error deleting permission:', error);
}
};

const handleSearch = async () => {

Check failure on line 59 in src/Pages/Protected/Permissions/permissionsHandler.jsx

View workflow job for this annotation

GitHub Actions / lint

'handleSearch' is assigned a value but never used
try {
const response = await APIUsers.get(`permissions/search`,
{ name: searchQuery },
{headers: {
Authorization: `Bearer ${getToken()}`,
}}
);
setPermissions(response.data);
} catch (error) {
console.error('Error searching permissions:', error);
}
};

const filteredPermissions = permissions.filter((permission) =>
permission.name.toLowerCase().includes(search.toLowerCase())
);

return (
<Box sx={{ padding: 4 }}>
<Typography variant="h4" gutterBottom>
Permission Management
</Typography>

<Box component="form" onSubmit={handleSubmit} sx={{ marginBottom: 4 }}>
<TextField
label="Permission Name"
value={form.name}
onChange={(e) => setForm({ ...form, name: e.target.value })}
required
fullWidth
margin="normal"
/>
<Button sx={{ backgroundColor: '#ae883c', '&:hover': { backgroundColor: '#936d30' }}} type="submit" variant="contained" color="primary">
{editId ? 'Atualizar Permissão' : 'Criar Permissão'}
</Button>
</Box>

<TableContainer component={Paper}>
<Table sx={{ backgroundColor: '#eae3d7', '&:hover': { backgroundColor: '#eae3d7' }}}>
<TableHead>
<TableRow>
<TableCell>Name</TableCell>
<TableCell>Actions</TableCell>
</TableRow>
</TableHead>
<TableBody>
{filteredPermissions.map((permission, index) => (

Check failure on line 106 in src/Pages/Protected/Permissions/permissionsHandler.jsx

View workflow job for this annotation

GitHub Actions / lint

'index' is defined but never used
<TableRow key={permission._id}>
<TableCell>{permission.name}</TableCell>
<TableCell>
<IconButton color="primary" onClick={() => handleEdit(permission)}>
<Edit />
</IconButton>
<IconButton color="error" onClick={() => handleDelete(permission._id)}>
<Delete />
</IconButton>
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
</Box>
);
};

export default PermissionCRUD;
12 changes: 12 additions & 0 deletions src/Routes/protectedRoutes.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import FinancialUpdate from "../Pages/Protected/FinancialMovements/FinancialUpda
import ContributionHistoric from "../Pages/Protected/FinancialMovements/ContributionHistoric";
import Unauthorized from "../Pages/Protected/Unauthorized";
import GenerateFinancialReport from "../Pages/Protected/FinancialMovements/GenerateFinancialReport";
import PermissionCRUD from "../Pages/Protected/Permissions/permissionsHandler.jsx";

const ProtectedRoutes = () => {
return (
Expand Down Expand Up @@ -81,6 +82,17 @@ const ProtectedRoutes = () => {
}
/>

<Route
path="/permissions"
element={
<PermissionProtect
element={<PermissionCRUD />}
moduleName="users"
actions={["create", "update", "read", "delete"]
}
/>
}
/>
<Route path="/user" element={<UserUpdate />} />

<Route path="/usuarios/editar/:nome" element={<UserUpdatePage />} />
Expand Down

0 comments on commit 660245b

Please sign in to comment.