Confirm deleting before delete the label

This commit is contained in:
Mo Tarbin 2024-11-28 21:18:25 -05:00
parent 6c1df9cb36
commit ce514cb43a

View file

@ -15,33 +15,55 @@ import { useLabels } from './LabelQueries'
// import { useMutation, useQueryClient } from '@tanstack/react-query' // import { useMutation, useQueryClient } from '@tanstack/react-query'
import { Add } from '@mui/icons-material' import { Add } from '@mui/icons-material'
import { useQueryClient } from 'react-query' import { useQueryClient } from 'react-query'
import { useNavigate } from 'react-router-dom'
import { DeleteLabel } from '../../utils/Fetcher' import { DeleteLabel } from '../../utils/Fetcher'
import ConfirmationModal from '../Modals/Inputs/ConfirmationModal'
const LabelView = () => { const LabelView = () => {
const { data: labels, isLabelsLoading, isError } = useLabels() const { data: labels, isLabelsLoading, isError } = useLabels()
const [userLabels, setUserLabels] = useState([labels]) const [userLabels, setUserLabels] = useState([labels])
const [modalOpen, setModalOpen] = useState(false) const [modalOpen, setModalOpen] = useState(false)
const [currentLabel, setCurrentLabel] = useState(null) // Label being edited or null for new label const [currentLabel, setCurrentLabel] = useState(null)
const queryClient = useQueryClient() const queryClient = useQueryClient()
const [confirmationModel, setConfirmationModel] = useState({})
const Navigate = useNavigate()
const handleAddLabel = () => { const handleAddLabel = () => {
setCurrentLabel(null) // Adding a new label setCurrentLabel(null)
setModalOpen(true) setModalOpen(true)
} }
const handleEditLabel = label => { const handleEditLabel = label => {
setCurrentLabel(label) // Editing an existing label setCurrentLabel(label)
setModalOpen(true) setModalOpen(true)
} }
const handleDeleteClicked = id => {
setConfirmationModel({
isOpen: true,
title: 'Delete Label',
message:
'Are you sure you want to delete this label? This will remove the label from all tasks.',
confirmText: 'Delete',
color: 'danger',
cancelText: 'Cancel',
onClose: confirmed => {
if (confirmed) {
handleDeleteLabel(id)
}
setConfirmationModel({})
},
})
}
const handleDeleteLabel = id => { const handleDeleteLabel = id => {
DeleteLabel(id).then(res => { DeleteLabel(id).then(res => {
// Invalidate and refetch labels after deleting a label
const updatedLabels = userLabels.filter(label => label.id !== id) const updatedLabels = userLabels.filter(label => label.id !== id)
setUserLabels(updatedLabels) setUserLabels(updatedLabels)
queryClient.invalidateQueries('labels') queryClient.invalidateQueries('labels')
}) })
// Implement deletion logic here
} }
const handleSaveLabel = newOrUpdatedLabel => { const handleSaveLabel = newOrUpdatedLabel => {
@ -92,7 +114,13 @@ const LabelView = () => {
<tbody> <tbody>
{userLabels.map(label => ( {userLabels.map(label => (
<tr key={label.id}> <tr key={label.id}>
<td>{label.name}</td> <td
onClick={() => {
Navigate('/my/chores', { state: { label: label.id } })
}}
>
{label.name}
</td>
<td <td
style={{ style={{
// center without display flex: // center without display flex:
@ -121,7 +149,7 @@ const LabelView = () => {
<EditIcon /> <EditIcon />
</IconButton> </IconButton>
<IconButton <IconButton
onClick={() => handleDeleteLabel(label.id)} onClick={() => handleDeleteClicked(label.id)}
color='danger' color='danger'
> >
<DeleteIcon /> <DeleteIcon />
@ -151,7 +179,7 @@ const LabelView = () => {
position: 'fixed', position: 'fixed',
bottom: 0, bottom: 0,
left: 10, left: 10,
p: 2, // padding p: 2,
display: 'flex', display: 'flex',
justifyContent: 'flex-end', justifyContent: 'flex-end',
gap: 2, gap: 2,
@ -171,6 +199,7 @@ const LabelView = () => {
<Add /> <Add />
</IconButton> </IconButton>
</Box> </Box>
<ConfirmationModal config={confirmationModel} />
</Container> </Container>
) )
} }