diff --git a/src/views/Labels/LabelView.jsx b/src/views/Labels/LabelView.jsx index 9dd442a..ca5b642 100644 --- a/src/views/Labels/LabelView.jsx +++ b/src/views/Labels/LabelView.jsx @@ -2,13 +2,14 @@ import DeleteIcon from '@mui/icons-material/Delete' import EditIcon from '@mui/icons-material/Edit' import { Box, + Button, + Chip, CircularProgress, Container, IconButton, - Table, Typography, } from '@mui/joy' -import React, { useEffect, useState } from 'react' +import { useEffect, useState } from 'react' import LabelModal from '../Modals/Inputs/LabelModal' import { useLabels } from './LabelQueries' @@ -16,13 +17,17 @@ import { useLabels } from './LabelQueries' import { Add } from '@mui/icons-material' import { useQueryClient } from 'react-query' import { DeleteLabel } from '../../utils/Fetcher' +import { getTextColorFromBackgroundColor } from '../../utils/LabelColors' const LabelView = () => { const { data: labels, isLabelsLoading, isError } = useLabels() + const [userLabels, setUserLabels] = useState([labels]) const [modalOpen, setModalOpen] = useState(false) const [currentLabel, setCurrentLabel] = useState(null) // Label being edited or null for new label + const queryClient = useQueryClient() + const handleAddLabel = () => { setCurrentLabel(null) // Adding a new label setModalOpen(true) @@ -52,6 +57,7 @@ const LabelView = () => { ) setUserLabels(updatedLabels) } + useEffect(() => { if (labels) { setUserLabels(labels) @@ -81,56 +87,50 @@ const LabelView = () => { return ( - - - - - - - - - - {userLabels.map(label => ( - - - - - - ))} - -
LabelColorActions
{label.name} + Labels + +
+ {userLabels.map(label => ( +
+ + {label.name} + + +
+
+ handleDeleteLabel(label.id)} + color='danger' > - handleEditLabel(label)}> - - - handleDeleteLabel(label.id)} - color='danger' - > - - -
+ + + + + ))} + {userLabels.length === 0 && ( @@ -146,6 +146,7 @@ const LabelView = () => { label={currentLabel} /> )} + { - if (!labelName || labelName.trim() === '') { + if (!labelName.trim()) { setError('Name cannot be empty') return false - } else if ( + } + if ( userLabels.some( - userLabel => userLabel.name === labelName && userLabel.id !== label.id, + userLabel => userLabel.name === labelName && userLabel.id !== label?.id, ) ) { setError('Label with this name already exists') return false - } else if (color === '') { + } + if (!color) { setError('Please select a color') return false } return true } - const handleSave = () => { - if (!validateLabel()) { - return - } - - const saveAction = label - ? UpdateLabel({ id: label.id, name: labelName, color }) - : CreateLabel({ name: labelName, color }) - - saveAction.then(res => { - if (res.error) { - console.log(res.error) - setError('Failed to save label. Please try again.') - return - } - res.json().then(data => { - if (data.error) { - setError('Failed to save label. Please try again.') - return - } - onSave({ id: data?.res?.id, name: labelName, color }) + // Mutation for saving labels + const saveLabelMutation = useMutation( + newLabel => + label + ? UpdateLabel({ id: label.id, ...newLabel }) + : CreateLabel(newLabel), + { + onSuccess: () => { + queryClient.invalidateQueries('labels') onClose() - }) - }) + }, + onError: () => { + setError('Failed to save label. Please try again.') + }, + }, + ) + + const handleSave = () => { + if (!validateLabel()) return + + saveLabelMutation.mutate({ name: labelName, color }) } return ( @@ -85,51 +84,39 @@ function LabelModal({ isOpen, onClose, onSave, label }) { {label ? 'Edit Label' : 'Add Label'} + - + Name setLabelName(e.target.value)} /> - {/* Color Selection */} - - Color: + + Color - - {error && ( - - {error} - - )} - + {error && ( + + {error} + + )} + +