Skip to content

Commit

Permalink
adding an option to delete organization
Browse files Browse the repository at this point in the history
  • Loading branch information
EmanuelLorenc96 committed Dec 3, 2023
1 parent 159e6ae commit ebd2fc9
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 14 deletions.
13 changes: 13 additions & 0 deletions backend/databases/organization_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
This module provides a OrganizationManager class that handles database operations related to the Organization model.
It uses SQLAlchemy for ORM operations and facilitates CRUD operations on user data.
"""
from fastapi import HTTPException
from sqlalchemy.orm import Session
from models.organization import Organization

Expand Down Expand Up @@ -65,3 +66,15 @@ def create_organization(self, organization: Organization):
self.db_session.commit()
self.db_session.refresh(organization)
return organization

def delete_organization(self, org_id: int):
organization = (
self.db_session.query(Organization)
.filter(Organization.id == org_id)
.first()
)
if organization:
self.db_session.delete(organization)
self.db_session.commit()
else:
raise HTTPException(status_code=404, detail="Organization not found")
10 changes: 10 additions & 0 deletions backend/routes/organization_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,13 @@ async def save_organization(
created_organization = org_manager.create_organization(new_organization)
response = OrganizationCreateResponse(created_organization.name)
return response


@organization_router.delete("/organization/{org_id}")
async def delete_organization(
org_id: int, current_admin: User = Depends(get_current_admin_user)
):
with DatabaseManager() as session:
org_manager = OrganizationManager(session)
org_manager.delete_organization(org_id)
return {"detail": "Organization deleted successfully"}
82 changes: 68 additions & 14 deletions frontend/src/pages/Admin/OrganizationManagement.jsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,63 @@
// OrganizationManagement.jsx
import React, { useState } from 'react';
import React, { useState, useEffect } from 'react';
import axios from 'axios';
import { API_URL } from '../../utils/constants.jsx';
import { Button, Card, CardContent, TextField, Typography, Snackbar, Alert } from '@mui/material';
import { Button, Card, CardContent, TextField, Typography, Snackbar, Alert, Select, MenuItem, Box } from '@mui/material';

function OrganizationManagement() {
const [organizationName, setOrganizationName] = useState('');
const [organizations, setOrganizations] = useState([]);
const [selectedOrganization, setSelectedOrganization] = useState('');
const [openSnackbar, setOpenSnackbar] = useState(false);
const [snackbarMessage, setSnackbarMessage] = useState('');
const [snackbarSeverity, setSnackbarSeverity] = useState('success'); // 'error' or 'success'

useEffect(() => {
// Fetch organizations when the component mounts
fetchOrganizations();
}, []);

const fetchOrganizations = () => {
axios.get(`${API_URL}organizations/`)
.then(response => {
setOrganizations(response.data);
})
.catch(error => {
console.error('Error fetching organizations', error);
});
};

const handleAddOrganization = () => {
axios.post(`${API_URL}organization/`, { name: organizationName })
.then(response => {
// Handle successful addition
console.log('Organization added:', response.data);
setSnackbarMessage('Organization added successfully.');
setSnackbarSeverity('success');
setOpenSnackbar(true);
setOrganizationName(''); // Reset the input field
setOrganizationName('');
fetchOrganizations(); // Refresh the list of organizations
})
.catch(error => {
// Handle error
console.error('Error adding organization', error);
setSnackbarMessage('Error adding organization.');
setSnackbarSeverity('error');
setOpenSnackbar(true);
});
};

const handleDeleteOrganization = () => {
axios.delete(`${API_URL}organization/${selectedOrganization}`)
.then(response => {
setSnackbarMessage('Organization deleted successfully.');
setSnackbarSeverity('success');
setOpenSnackbar(true);
fetchOrganizations(); // Refresh the list of organizations
})
.catch(error => {
setSnackbarMessage('Error deleting organization.');
setSnackbarSeverity('error');
setOpenSnackbar(true);
});
};

return (
<Card>
<CardContent>
Expand All @@ -40,16 +69,41 @@ function OrganizationManagement() {
fullWidth
margin="normal"
/>
<Button
variant="contained"
onClick={handleAddOrganization}
disabled={!organizationName}
<Box mt={1}> {/* Added margin top */}
<Button
variant="contained"
onClick={handleAddOrganization}
disabled={!organizationName}
>
Add Organization
</Button>
</Box>

<Box mt={2}> {/* Added margin top to the dropdown as well */}
<Select
value={selectedOrganization}
onChange={e => setSelectedOrganization(e.target.value)}
fullWidth
displayEmpty
>
<MenuItem value="" disabled>Select Organization</MenuItem>
{organizations.map(org => (
<MenuItem key={org.id} value={org.id}>{org.name}</MenuItem>
))}
</Select>
</Box>
<Box mt={2}>
<Button
variant="contained"
color="error"
onClick={handleDeleteOrganization}
disabled={!selectedOrganization}
>
Add Organization
Delete Organization
</Button>
</CardContent>
</Box>

{/* Snackbar for showing notifications */}
</CardContent>
<Snackbar open={openSnackbar} autoHideDuration={6000} onClose={() => setOpenSnackbar(false)}>
<Alert onClose={() => setOpenSnackbar(false)} severity={snackbarSeverity} sx={{ width: '100%' }}>
{snackbarMessage}
Expand Down

0 comments on commit ebd2fc9

Please sign in to comment.