import { ActionIcon, Card, Group, Stack, Text, TextInput, Tooltip } from '@mantine/core'; import { useDebouncedValue, useDisclosure } from '@mantine/hooks'; import { useCommonHooks, useMutationAPI } from 'hooks'; import { useEffect, useState } from 'react'; import { FiTrash } from 'react-icons/fi'; import { cardClasses, textInputClasses } from 'styles'; import type { WolClient } from 'types'; import DeleteItemModal from '../Shared/DeleteItemModal'; function DebouncedWolClient({ wolClient, setIsOpen, }: { wolClient: WolClient; setIsOpen: (isOpen: boolean) => void; }) { const { handleDelete, isMobile, theme } = useCommonHooks(); const [modalOpened, { open: openModal, close: closeModal }] = useDisclosure(false); const { mutateAsync } = useMutationAPI(); const [client, setClient] = useState(wolClient); const [update, setUpdate] = useState({}); const [debounced] = useDebouncedValue(update, 1000); const handleClose = () => { setIsOpen(false); closeModal(); }; useEffect(() => { if (Object.keys(debounced).length) { mutateAsync({ method: 'PATCH', endpoint: 'api/WolClients', body: debounced }); } }, [debounced]); useEffect(() => { if (update !== client && Object.keys(update).length) { setClient(update); } }, [update]); return ( handleDelete(client, 'api/WolClients')} title={'Delete WoL Client'} body={ Are you sure you want to delete this WoL Client? {`"${client.hostname}"`} This action cannot be undone! } /> { openModal(); }} > { setUpdate({ ...client, hostname: event.target.value, }); }} styles={ theme.textShadowsEnabled && { label: { boxShadow: '2px 2px var(--mantine-color-border-0)' }, } } /> { setUpdate({ ...client, macAddress: event.target.value, }); }} styles={ theme.textShadowsEnabled && { label: { boxShadow: '2px 2px var(--mantine-color-border-0)' }, } } /> ); } export default DebouncedWolClient;