|
1 | 1 | import Base from "../../src/components/Base"; |
2 | | -import { Box, Button, Dialog, Grid, IconButton, TableContainer, Table, TableHead, TableRow,TableBody, TableCell } from "@mui/material"; |
| 2 | +import { Alert, Box, Button, Dialog, Grid, IconButton, Paper, TableContainer, Table, TableHead, TableRow,TableBody, TableCell, Typography } from "@mui/material"; |
3 | 3 | import AddIcon from '@mui/icons-material/Add'; |
4 | 4 | import PublicIcon from '@mui/icons-material/Public'; |
| 5 | +import DeleteIcon from '@mui/icons-material/Delete'; |
| 6 | +import EditIcon from '@mui/icons-material/Edit'; |
5 | 7 | import { useState, useEffect, use } from "react"; |
6 | 8 | import { getCookie } from "../../src/utils"; |
7 | 9 | import render from "../../src/render"; |
@@ -32,50 +34,108 @@ function Organizations() { |
32 | 34 |
|
33 | 35 | const apiBaseUrl = localStorage.getItem('apiBaseUrl'); |
34 | 36 |
|
35 | | - const handleOrganizationCreated = (data) => { |
| 37 | + const handleSuccessFormSubmission = (data) => { |
36 | 38 | console.log('Organization created successfully:', data); |
37 | 39 | setDialogOpen(false); |
38 | 40 | setTableUpdates(prev => [...prev, data]); |
39 | 41 | }; |
40 | 42 |
|
41 | | - const handleOrganizationCreationFailed = (error) => { |
| 43 | + const handleFailedFormSubmission = (error) => { |
42 | 44 | console.error('Error creating organization:', error); |
43 | 45 | }; |
44 | 46 |
|
| 47 | + const goToUrl = (url) => { |
| 48 | + window.open(url, '_blank'); |
| 49 | + } |
| 50 | + |
| 51 | + const deleteOrganization = (organizationId) => { |
| 52 | + fetch(`${apiBaseUrl}/organizations/${organizationId}/`, { |
| 53 | + method: 'DELETE', |
| 54 | + headers: { |
| 55 | + 'Content-Type': 'application/json', |
| 56 | + 'X-CSRFToken': getCookie('csrftoken'), |
| 57 | + }, |
| 58 | + }) |
| 59 | + .then(response => { |
| 60 | + if (response.ok) { |
| 61 | + console.log('Organization deleted successfully'); |
| 62 | + setTableUpdates(prev => [...prev, { deletedOrganizationId: organizationId }]); |
| 63 | + } else { |
| 64 | + console.error('Error deleting organization'); |
| 65 | + } |
| 66 | + }) |
| 67 | + .catch(error => { |
| 68 | + console.error('Error deleting organization:', error); |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + const deleteConfirmationDialog = (organization) => { |
| 73 | + setDialogContent( |
| 74 | + <Box sx={{ p: 2 }}> |
| 75 | + <Typography variant="h6">Confirm Deletion</Typography> |
| 76 | + <Alert severity="warning" variant="outlined" sx={{ mt: 1 }}>Are you sure you want to delete the organization "{organization.name}"? All the courses contents and users under this organization will also be deleted.</Alert> |
| 77 | + <Box sx={{ display: 'flex', justifyContent: 'flex-end', mt: 2 }}> |
| 78 | + <Button onClick={() => setDialogOpen(false)} sx={{ mr: 1 }}>Cancel</Button> |
| 79 | + <Button variant="contained" color="error" onClick={() => { |
| 80 | + deleteOrganization(organization.id); |
| 81 | + setDialogOpen(false); |
| 82 | + }}>Delete</Button> |
| 83 | + </Box> |
| 84 | + </Box> |
| 85 | + ); |
| 86 | + setDialogOpen(true); |
| 87 | + } |
| 88 | + |
45 | 89 | return ( |
46 | 90 | <Base breadCrumbList={[{label: 'Organizations', href: '#'}]} showOrganizationSwitcher={false}> |
47 | 91 | <Grid size={12} py={2} pl={2}> |
48 | | - <Box p={2} sx={{ border: '1px solid', borderColor: 'grey.300', borderRadius: 1, minHeight: 300 }}> |
| 92 | + <Box p={2} sx={{ border: '1px solid', borderColor: 'grey.300', borderRadius: 1, minHeight: 300, width: { lg: '80%' } }}> |
49 | 93 | <Button variant="contained" startIcon={<AddIcon />} sx={{ marginBottom: 2 }} onClick={() => { |
50 | 94 | setDialogContent(<OrganizationForm |
51 | | - successCallback={handleOrganizationCreated} |
52 | | - failureCallback={handleOrganizationCreationFailed} |
| 95 | + successCallback={handleSuccessFormSubmission} |
| 96 | + failureCallback={handleFailedFormSubmission} |
53 | 97 | cancelCallback={() => setDialogOpen(false)} |
54 | 98 | createMode={true} |
55 | 99 | />); |
56 | 100 | setDialogOpen(true); |
57 | 101 | }}>Add an Organization</Button> |
58 | 102 |
|
59 | | - { organizations.length > 0 && (<TableContainer sx={{ maxHeight: 440, border: '1px solid', borderColor: 'grey.300', borderRadius: 1 }}> |
| 103 | + { organizations.length > 0 && (<TableContainer component={Paper} sx={{ maxHeight: 440, border: '1px solid', borderColor: 'grey.300', borderRadius: 1 }}> |
60 | 104 | <Table> |
61 | 105 | <TableHead> |
62 | 106 | <TableRow> |
63 | 107 | <TableCell>Name</TableCell> |
64 | | - <TableCell>Public URL</TableCell> |
65 | | - <TableCell>Actions</TableCell> |
| 108 | + <TableCell sx={{ width: '100px' }}>Actions</TableCell> |
66 | 109 | </TableRow> |
67 | 110 | </TableHead> |
68 | 111 | <TableBody> |
69 | 112 | { organizations.map((org) => ( |
70 | 113 | <TableRow key={org.id}> |
71 | 114 | <TableCell>{org.name}</TableCell> |
72 | | - <TableCell><a href={org.public_url}><IconButton><PublicIcon fontSize="small"/></IconButton></a></TableCell> |
73 | | - <TableCell>Actions</TableCell> |
| 115 | + <TableCell> |
| 116 | + <IconButton onClick={() => goToUrl(org.public_url)}><PublicIcon fontSize="small"/></IconButton> |
| 117 | + <IconButton onClick={() => { |
| 118 | + setDialogContent(<OrganizationForm |
| 119 | + successCallback={handleSuccessFormSubmission} |
| 120 | + failureCallback={handleFailedFormSubmission} |
| 121 | + cancelCallback={() => setDialogOpen(false)} |
| 122 | + createMode={false} |
| 123 | + initialName={org.name} |
| 124 | + initialDescription={org.description} |
| 125 | + initialLogoUrl={org.logo} |
| 126 | + organizationId={org.id} |
| 127 | + />); |
| 128 | + setDialogOpen(true); |
| 129 | + }}><EditIcon fontSize="small"/></IconButton> |
| 130 | + <IconButton onClick={() => deleteConfirmationDialog(org)}><DeleteIcon fontSize="small" /></IconButton> |
| 131 | + </TableCell> |
74 | 132 | </TableRow> |
75 | 133 | ))} |
76 | 134 | </TableBody> |
77 | 135 | </Table> |
78 | | - </TableContainer>)} |
| 136 | + </TableContainer> |
| 137 | + |
| 138 | + )} |
79 | 139 |
|
80 | 140 | </Box> |
81 | 141 |
|
|
0 commit comments