import DeleteIcon from '@mui/icons-material/Delete' import EditIcon from '@mui/icons-material/Edit' import { Box, Button, Chip, CircularProgress, Container, IconButton, Typography, } from '@mui/joy' import { useEffect, useState } from 'react' import LabelModal from '../Modals/Inputs/LabelModal' import { useLabels } from './LabelQueries' // import { useMutation, useQueryClient } from '@tanstack/react-query' import { Add } from '@mui/icons-material' import { useQueryClient } from 'react-query' import { useNavigate } from 'react-router-dom' import { DeleteLabel } from '../../utils/Fetcher' import { getTextColorFromBackgroundColor } from '../../utils/LabelColors' import ConfirmationModal from '../Modals/Inputs/ConfirmationModal' const LabelView = () => { const { data: labels, isLabelsLoading, isError } = useLabels() const [userLabels, setUserLabels] = useState([labels]) const [modalOpen, setModalOpen] = useState(false) const [currentLabel, setCurrentLabel] = useState(null) const queryClient = useQueryClient() const [confirmationModel, setConfirmationModel] = useState({}) const Navigate = useNavigate() const handleAddLabel = () => { setCurrentLabel(null) setModalOpen(true) } const handleEditLabel = label => { setCurrentLabel(label) 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 => { DeleteLabel(id).then(res => { const updatedLabels = userLabels.filter(label => label.id !== id) setUserLabels(updatedLabels) queryClient.invalidateQueries('labels') }) } const handleSaveLabel = newOrUpdatedLabel => { queryClient.invalidateQueries('labels') setModalOpen(false) const updatedLabels = userLabels.map(label => label.id === newOrUpdatedLabel.id ? newOrUpdatedLabel : label, ) setUserLabels(updatedLabels) } useEffect(() => { if (labels) { setUserLabels(labels) } }, [labels]) if (isLabelsLoading) { return ( ) } if (isError) { return ( Failed to load labels. Please try again. ) } return ( {userLabels.map(label => ( {label.name} handleEditLabel(label)} startDecorator={} > Edit handleDeleteLabel(label.id)} color='danger' > ))} {userLabels.length === 0 && ( No labels available. Add a new label to get started. )} {modalOpen && ( setModalOpen(false)} onSave={handleSaveLabel} label={currentLabel} /> )} ) } export default LabelView