import { useEffect, useState } from 'react'; import { Modal, ModalBody, ModalFooter, ModalHeader } from 'reactstrap'; import { identity, pipe } from 'ramda'; import { ShortUrlDeletion } from '../reducers/shortUrlDeletion'; import { ShortUrlModalProps } from '../data'; import { handleEventPreventingDefault, OptionalString } from '../../utils/utils'; import { Result } from '../../utils/Result'; import { isInvalidDeletionError } from '../../utils/services/types'; interface DeleteShortUrlModalConnectProps extends ShortUrlModalProps { shortUrlDeletion: ShortUrlDeletion; deleteShortUrl: (shortCode: string, domain: OptionalString) => Promise; resetDeleteShortUrl: () => void; } const DeleteShortUrlModal = ( { shortUrl, toggle, isOpen, shortUrlDeletion, resetDeleteShortUrl, deleteShortUrl }: DeleteShortUrlModalConnectProps, ) => { const [ inputValue, setInputValue ] = useState(''); useEffect(() => resetDeleteShortUrl, []); const { error, errorData } = shortUrlDeletion; const close = pipe(resetDeleteShortUrl, toggle); const handleDeleteUrl = handleEventPreventingDefault(() => { const { shortCode, domain } = shortUrl; deleteShortUrl(shortCode, domain) .then(toggle) .catch(identity); }); return (
Delete short URL

Caution! You are about to delete a short URL.

This action cannot be undone. Once you have deleted it, all the visits stats will be lost.

Write {shortUrl.shortCode} to confirm deletion.

setInputValue(e.target.value)} /> {error && isInvalidDeletionError(errorData) && ( {errorData.threshold && `This short URL has received more than ${errorData.threshold} visits, and therefore, it cannot be deleted.`} {!errorData.threshold && 'This short URL has received too many visits, and therefore, it cannot be deleted.'} )} {error && !isInvalidDeletionError(errorData) && ( {errorData?.detail ?? 'Something went wrong while deleting the URL :('} )}
); }; export default DeleteShortUrlModal;