|
| 1 | +import { useState } from 'react' |
| 2 | +import { |
| 3 | + Box, |
| 4 | + Button, |
| 5 | + Card, |
| 6 | + CardContent, |
| 7 | + Dialog, |
| 8 | + DialogActions, |
| 9 | + DialogContent, |
| 10 | + DialogTitle, |
| 11 | + IconButton, |
| 12 | + Typography, |
| 13 | + Alert, |
| 14 | +} from '@mui/material' |
| 15 | +import EditIcon from '@mui/icons-material/Edit' |
| 16 | +import DeleteIcon from '@mui/icons-material/Delete' |
| 17 | +import AddIcon from '@mui/icons-material/Add' |
| 18 | +import { useTranslation } from 'react-i18next' |
| 19 | +import { enqueueSnackbar } from 'notistack' |
| 20 | +import MDEditor from '@uiw/react-md-editor' |
| 21 | + |
| 22 | +import { ControlReport } from '@types' |
| 23 | +import useLoggedInUser from '../../hooks/useLoggedInUser' |
| 24 | +import Markdown from '../Common/Markdown' |
| 25 | +import apiClient from '../../util/apiClient' |
| 26 | +import styles from '../../styles' |
| 27 | + |
| 28 | +interface ControlReportsProps { |
| 29 | + entryId: string |
| 30 | + controlReports: ControlReport[] |
| 31 | + totalRiskLevel: number | null | undefined |
| 32 | + onUpdate: () => void |
| 33 | +} |
| 34 | + |
| 35 | +const ControlReports = ({ entryId, controlReports, totalRiskLevel, onUpdate }: ControlReportsProps) => { |
| 36 | + const { t } = useTranslation() |
| 37 | + const { user } = useLoggedInUser() |
| 38 | + const [isDialogOpen, setIsDialogOpen] = useState(false) |
| 39 | + const [editingReport, setEditingReport] = useState<ControlReport | null>(null) |
| 40 | + const [text, setText] = useState('') |
| 41 | + const [deleteDialogOpen, setDeleteDialogOpen] = useState(false) |
| 42 | + const [reportToDelete, setReportToDelete] = useState<string | null>(null) |
| 43 | + const [isSubmitting, setIsSubmitting] = useState(false) |
| 44 | + |
| 45 | + const { cardStyles } = styles |
| 46 | + |
| 47 | + const isAdmin = user?.isAdmin |
| 48 | + const canAddReport = isAdmin && totalRiskLevel === 3 |
| 49 | + |
| 50 | + const handleOpenDialog = (report?: ControlReport) => { |
| 51 | + if (report) { |
| 52 | + setEditingReport(report) |
| 53 | + setText(report.text) |
| 54 | + } else { |
| 55 | + setEditingReport(null) |
| 56 | + setText('') |
| 57 | + } |
| 58 | + setIsDialogOpen(true) |
| 59 | + } |
| 60 | + |
| 61 | + const handleCloseDialog = () => { |
| 62 | + setIsDialogOpen(false) |
| 63 | + setEditingReport(null) |
| 64 | + setText('') |
| 65 | + } |
| 66 | + |
| 67 | + const handleSave = async () => { |
| 68 | + if (!text.trim()) { |
| 69 | + enqueueSnackbar(t('controlReport:createError'), { variant: 'error' }) |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + setIsSubmitting(true) |
| 74 | + try { |
| 75 | + const payload = { |
| 76 | + text, |
| 77 | + } |
| 78 | + |
| 79 | + if (editingReport) { |
| 80 | + await apiClient.put(`/entries/${entryId}/control-report/${editingReport.id}`, payload) |
| 81 | + enqueueSnackbar(t('controlReport:updateSuccess'), { variant: 'success' }) |
| 82 | + } else { |
| 83 | + await apiClient.post(`/entries/${entryId}/control-report`, payload) |
| 84 | + enqueueSnackbar(t('controlReport:createSuccess'), { variant: 'success' }) |
| 85 | + } |
| 86 | + |
| 87 | + handleCloseDialog() |
| 88 | + onUpdate() |
| 89 | + } catch (error: any) { |
| 90 | + const message = editingReport ? t('controlReport:updateError') : t('controlReport:createError') |
| 91 | + enqueueSnackbar(error?.response?.data || message, { variant: 'error' }) |
| 92 | + } finally { |
| 93 | + setIsSubmitting(false) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + const handleDeleteClick = (reportId: string) => { |
| 98 | + setReportToDelete(reportId) |
| 99 | + setDeleteDialogOpen(true) |
| 100 | + } |
| 101 | + |
| 102 | + const handleDeleteConfirm = async () => { |
| 103 | + if (!reportToDelete) { |
| 104 | + return |
| 105 | + } |
| 106 | + |
| 107 | + setIsSubmitting(true) |
| 108 | + try { |
| 109 | + await apiClient.delete(`/entries/${entryId}/control-report/${reportToDelete}`) |
| 110 | + enqueueSnackbar(t('controlReport:deleteSuccess'), { variant: 'success' }) |
| 111 | + setDeleteDialogOpen(false) |
| 112 | + setReportToDelete(null) |
| 113 | + onUpdate() |
| 114 | + } catch (error) { |
| 115 | + enqueueSnackbar(t('controlReport:deleteError'), { variant: 'error' }) |
| 116 | + } finally { |
| 117 | + setIsSubmitting(false) |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + const formatDate = (dateString: string) => |
| 122 | + `${new Date(dateString).toLocaleDateString()} ${new Date(dateString).toLocaleTimeString()}` |
| 123 | + |
| 124 | + return ( |
| 125 | + <Box sx={cardStyles.nestedSubSection}> |
| 126 | + {canAddReport && ( |
| 127 | + <Box sx={{ mb: 2 }}> |
| 128 | + <Button variant="contained" startIcon={<AddIcon />} onClick={() => handleOpenDialog()}> |
| 129 | + {t('controlReport:addButton')} |
| 130 | + </Button> |
| 131 | + </Box> |
| 132 | + )} |
| 133 | + |
| 134 | + <Typography variant="h5" sx={{ fontWeight: 'bold', mb: 2 }}> |
| 135 | + {t('controlReport:title')} |
| 136 | + </Typography> |
| 137 | + |
| 138 | + {isAdmin && totalRiskLevel !== 3 && controlReports.length === 0 && ( |
| 139 | + <Alert severity="info" sx={{ mb: 2 }}> |
| 140 | + {t('controlReport:riskLevel3Required')} |
| 141 | + </Alert> |
| 142 | + )} |
| 143 | + |
| 144 | + {controlReports.length === 0 ? ( |
| 145 | + <Typography variant="body2" color="text.secondary"> |
| 146 | + {t('controlReport:noReports')} |
| 147 | + </Typography> |
| 148 | + ) : ( |
| 149 | + <Box sx={{ display: 'flex', flexDirection: 'column', gap: 2 }}> |
| 150 | + {controlReports.map(report => ( |
| 151 | + <Card key={report.id} variant="outlined"> |
| 152 | + <CardContent> |
| 153 | + <Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start', mb: 2 }}> |
| 154 | + <Box> |
| 155 | + <Typography variant="caption" color="text.secondary"> |
| 156 | + {t('controlReport:createdBy')}: {report.createdBy} |
| 157 | + </Typography> |
| 158 | + <Typography variant="caption" color="text.secondary" sx={{ ml: 2 }}> |
| 159 | + {t('controlReport:lastUpdated')}: {formatDate(report.updatedAt)} |
| 160 | + </Typography> |
| 161 | + </Box> |
| 162 | + {isAdmin && ( |
| 163 | + <Box> |
| 164 | + <IconButton size="small" onClick={() => handleOpenDialog(report)} sx={{ mr: 1 }}> |
| 165 | + <EditIcon fontSize="small" /> |
| 166 | + </IconButton> |
| 167 | + <IconButton size="small" onClick={() => handleDeleteClick(report.id)} color="error"> |
| 168 | + <DeleteIcon fontSize="small" /> |
| 169 | + </IconButton> |
| 170 | + </Box> |
| 171 | + )} |
| 172 | + </Box> |
| 173 | + <Markdown>{report.text}</Markdown> |
| 174 | + </CardContent> |
| 175 | + </Card> |
| 176 | + ))} |
| 177 | + </Box> |
| 178 | + )} |
| 179 | + |
| 180 | + {/* Edit/Create Dialog */} |
| 181 | + <Dialog open={isDialogOpen} onClose={handleCloseDialog} maxWidth="md" fullWidth> |
| 182 | + <DialogTitle>{editingReport ? t('controlReport:editButton') : t('controlReport:addButton')}</DialogTitle> |
| 183 | + <DialogContent> |
| 184 | + <Typography variant="caption" color="text.secondary" sx={{ mb: 2, display: 'block' }}> |
| 185 | + {t('controlReport:markdownSupported')} |
| 186 | + </Typography> |
| 187 | + |
| 188 | + <Box sx={{ mb: 2 }}> |
| 189 | + <MDEditor value={text} onChange={value => setText(value ?? '')} height={300} /> |
| 190 | + </Box> |
| 191 | + </DialogContent> |
| 192 | + <DialogActions> |
| 193 | + <Button onClick={handleCloseDialog} disabled={isSubmitting}> |
| 194 | + {t('controlReport:cancelButton')} |
| 195 | + </Button> |
| 196 | + <Button onClick={handleSave} variant="contained" disabled={isSubmitting}> |
| 197 | + {t('controlReport:saveButton')} |
| 198 | + </Button> |
| 199 | + </DialogActions> |
| 200 | + </Dialog> |
| 201 | + |
| 202 | + {/* Delete Confirmation Dialog */} |
| 203 | + <Dialog open={deleteDialogOpen} onClose={() => setDeleteDialogOpen(false)}> |
| 204 | + <DialogTitle>{t('controlReport:deleteButton')}</DialogTitle> |
| 205 | + <DialogContent> |
| 206 | + <Typography>{t('controlReport:confirmDelete')}</Typography> |
| 207 | + </DialogContent> |
| 208 | + <DialogActions> |
| 209 | + <Button onClick={() => setDeleteDialogOpen(false)} disabled={isSubmitting}> |
| 210 | + {t('controlReport:cancelButton')} |
| 211 | + </Button> |
| 212 | + <Button onClick={handleDeleteConfirm} color="error" variant="contained" disabled={isSubmitting}> |
| 213 | + {t('controlReport:deleteButton')} |
| 214 | + </Button> |
| 215 | + </DialogActions> |
| 216 | + </Dialog> |
| 217 | + </Box> |
| 218 | + ) |
| 219 | +} |
| 220 | + |
| 221 | +export default ControlReports |
0 commit comments