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

permissions handle #18

Merged
merged 1 commit into from
Jan 11, 2025
Merged
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
11 changes: 10 additions & 1 deletion src/Components/SideBar/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import { RiLogoutCircleRLine } from "react-icons/ri";
import { useNavigate } from "react-router-dom";
import AuthContext, { useAuth } from "../../Context/auth";
import { usePermissions, checkModule } from "../../Utils/permission";

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

View workflow job for this annotation

GitHub Actions / lint

'checkModule' is defined but never used

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

View workflow job for this annotation

GitHub Actions / lint

'checkModule' is defined but never used
import { getRoleById } from "../../Services/RoleService/roleService";

export default function SideBar() {
Expand All @@ -19,10 +19,10 @@
const navigate = useNavigate();
const context = useContext(AuthContext);
const { user } = useAuth();
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

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 @@
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

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

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

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

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

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

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

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

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
Loading