import './GeneralSettings.styles.scss'; import { Col, Input, Select, Space, Typography } from 'antd'; import { SOMETHING_WENT_WRONG } from 'constants/api'; import AddTags from 'container/NewDashboard/DashboardSettings/General/AddTags'; import { useUpdateDashboard } from 'hooks/dashboard/useUpdateDashboard'; import { useNotifications } from 'hooks/useNotifications'; import { isEqual } from 'lodash-es'; import { Check, X } from 'lucide-react'; import { useDashboard } from 'providers/Dashboard/Dashboard'; import { useEffect, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { Button } from './styles'; import { Base64Icons } from './utils'; const { Option } = Select; function GeneralDashboardSettings(): JSX.Element { const { selectedDashboard, setSelectedDashboard } = useDashboard(); const updateDashboardMutation = useUpdateDashboard(); const selectedData = selectedDashboard?.data; const { title = '', tags = [], description = '', image = Base64Icons[0] } = selectedData || {}; const [updatedTitle, setUpdatedTitle] = useState(title); const [updatedTags, setUpdatedTags] = useState(tags || []); const [updatedDescription, setUpdatedDescription] = useState( description || '', ); const [updatedImage, setUpdatedImage] = useState(image); const [numberOfUnsavedChanges, setNumberOfUnsavedChanges] = useState( 0, ); const { t } = useTranslation('common'); const { notifications } = useNotifications(); const onSaveHandler = (): void => { if (!selectedDashboard) return; updateDashboardMutation.mutateAsync( { ...selectedDashboard, data: { ...selectedDashboard.data, description: updatedDescription, tags: updatedTags, title: updatedTitle, image: updatedImage, }, }, { onSuccess: (updatedDashboard) => { if (updatedDashboard.payload) { setSelectedDashboard(updatedDashboard.payload); } }, onError: () => { notifications.error({ message: SOMETHING_WENT_WRONG, }); }, }, ); }; useEffect(() => { let numberOfUnsavedChanges = 0; const initialValues = [title, description, tags, image]; const updatedValues = [ updatedTitle, updatedDescription, updatedTags, updatedImage, ]; initialValues.forEach((val, index) => { if (!isEqual(val, updatedValues[index])) { numberOfUnsavedChanges += 1; } }); setNumberOfUnsavedChanges(numberOfUnsavedChanges); }, [ description, image, tags, title, updatedDescription, updatedImage, updatedTags, updatedTitle, ]); const discardHandler = (): void => { setUpdatedTitle(title); setUpdatedImage(image); setUpdatedTags(tags); setUpdatedDescription(description); }; return (
Dashboard Name
setUpdatedTitle(e.target.value)} />
Description setUpdatedDescription(e.target.value)} />
Tags
{numberOfUnsavedChanges > 0 && (
{numberOfUnsavedChanges} unsaved change {numberOfUnsavedChanges > 1 && 's'}
)}
); } export default GeneralDashboardSettings;