import { Card, Group, Stack, Text, TextInput } from '@mantine/core'; import { useDebouncedValue, useDisclosure } from '@mantine/hooks'; import { DeleteItemModal, ManagementPill } from 'components'; import { useCommonHooks, useMutationAPI } from 'hooks'; import { useEffect, useState } from 'react'; import { cardClasses, textInputClasses } from 'styles'; import type { Bookmark } from 'types'; function DebouncedBookmarkItem({ bookmarkItem, setIsOpen, }: { bookmarkItem: Bookmark; setIsOpen: (isOpen: boolean) => void; }) { const { handleDelete, isMobile, theme } = useCommonHooks(); const [modalOpened, { open: openModal, close: closeModal }] = useDisclosure(false); const { mutateAsync } = useMutationAPI(); const [bookmark, setBookmark] = useState(bookmarkItem); 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/bookmarks', body: debounced }); } }, [debounced]); useEffect(() => { if (update !== bookmark && Object.keys(update).length) { setBookmark(update); } }, [update]); return ( handleDelete(bookmark, 'api/bookmarks')} body={ Are you sure you want to delete this bookmark? {`"${bookmark.name}"`} This action cannot be undone! } title='Delete Bookmark' /> ) => { setUpdate({ ...bookmark, enabled: event.target.checked ? 1 : 0, }); }} openModal={openModal} /> { setUpdate({ ...bookmark, name: event.target.value, }); }} styles={ theme.textShadowsEnabled ? { label: { boxShadow: '2px 2px var(--mantine-color-border-0)' }, } : undefined } /> { setUpdate({ ...bookmark, category: event.target.value, }); }} styles={ theme.textShadowsEnabled ? { label: { boxShadow: '2px 2px var(--mantine-color-border-0)' }, } : undefined } /> { setUpdate({ ...bookmark, url: event.target.value, }); }} styles={ theme.textShadowsEnabled ? { label: { boxShadow: '2px 2px var(--mantine-color-border-0)' }, } : undefined } /> ); } export default DebouncedBookmarkItem;