2020-11-14 00:44:26 +03:00
|
|
|
import { useEffect, useState } from 'react';
|
2018-09-16 10:35:39 +03:00
|
|
|
import { Modal, ModalBody, ModalFooter, ModalHeader } from 'reactstrap';
|
2022-11-06 20:48:47 +03:00
|
|
|
import { pipe } from 'ramda';
|
2022-11-06 21:12:41 +03:00
|
|
|
import { ShortUrlDeletion } from '../reducers/shortUrlDeletion';
|
|
|
|
import { ShortUrlIdentifier, ShortUrlModalProps } from '../data';
|
2022-11-06 14:46:29 +03:00
|
|
|
import { handleEventPreventingDefault } from '../../utils/utils';
|
2020-12-21 19:54:05 +03:00
|
|
|
import { Result } from '../../utils/Result';
|
2020-12-22 11:49:13 +03:00
|
|
|
import { isInvalidDeletionError } from '../../api/utils';
|
2020-12-21 23:19:02 +03:00
|
|
|
import { ShlinkApiError } from '../../api/ShlinkApiError';
|
2020-01-11 14:24:45 +03:00
|
|
|
|
2020-08-26 21:37:36 +03:00
|
|
|
interface DeleteShortUrlModalConnectProps extends ShortUrlModalProps {
|
|
|
|
shortUrlDeletion: ShortUrlDeletion;
|
2022-11-22 21:39:07 +03:00
|
|
|
deleteShortUrl: (shortUrl: ShortUrlIdentifier) => Promise<void>;
|
|
|
|
shortUrlDeleted: (shortUrl: ShortUrlIdentifier) => void;
|
2020-08-26 21:37:36 +03:00
|
|
|
resetDeleteShortUrl: () => void;
|
|
|
|
}
|
2020-05-31 11:23:13 +03:00
|
|
|
|
2022-11-22 21:39:07 +03:00
|
|
|
export const DeleteShortUrlModal = ({
|
|
|
|
shortUrl,
|
|
|
|
toggle,
|
|
|
|
isOpen,
|
|
|
|
shortUrlDeletion,
|
|
|
|
resetDeleteShortUrl,
|
|
|
|
deleteShortUrl,
|
|
|
|
shortUrlDeleted,
|
|
|
|
}: DeleteShortUrlModalConnectProps) => {
|
2022-03-26 14:17:42 +03:00
|
|
|
const [inputValue, setInputValue] = useState('');
|
2018-09-16 10:35:39 +03:00
|
|
|
|
2020-05-31 11:23:13 +03:00
|
|
|
useEffect(() => resetDeleteShortUrl, []);
|
|
|
|
|
2022-11-22 21:39:07 +03:00
|
|
|
const { loading, error, deleted, errorData } = shortUrlDeletion;
|
2020-05-31 11:23:13 +03:00
|
|
|
const close = pipe(resetDeleteShortUrl, toggle);
|
2022-11-22 21:39:07 +03:00
|
|
|
const handleDeleteUrl = handleEventPreventingDefault(() => deleteShortUrl(shortUrl).then(toggle));
|
2018-09-16 11:47:17 +03:00
|
|
|
|
2020-05-31 11:23:13 +03:00
|
|
|
return (
|
2022-11-22 21:39:07 +03:00
|
|
|
<Modal isOpen={isOpen} toggle={close} centered onClosed={() => deleted && shortUrlDeleted(shortUrl)}>
|
2020-05-31 11:23:13 +03:00
|
|
|
<form onSubmit={handleDeleteUrl}>
|
|
|
|
<ModalHeader toggle={close}>
|
|
|
|
<span className="text-danger">Delete short URL</span>
|
|
|
|
</ModalHeader>
|
|
|
|
<ModalBody>
|
|
|
|
<p><b className="text-danger">Caution!</b> You are about to delete a short URL.</p>
|
|
|
|
<p>This action cannot be undone. Once you have deleted it, all the visits stats will be lost.</p>
|
2020-12-21 19:54:05 +03:00
|
|
|
<p>Write <b>{shortUrl.shortCode}</b> to confirm deletion.</p>
|
2018-09-16 10:35:39 +03:00
|
|
|
|
2020-05-31 11:23:13 +03:00
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
className="form-control"
|
2020-12-21 19:54:05 +03:00
|
|
|
placeholder={`Insert the short code (${shortUrl.shortCode})`}
|
2020-05-31 11:23:13 +03:00
|
|
|
value={inputValue}
|
|
|
|
onChange={(e) => setInputValue(e.target.value)}
|
|
|
|
/>
|
2018-09-16 10:35:39 +03:00
|
|
|
|
2020-12-21 23:19:02 +03:00
|
|
|
{error && (
|
|
|
|
<Result type={isInvalidDeletionError(errorData) ? 'warning' : 'error'} small className="mt-2">
|
|
|
|
<ShlinkApiError errorData={errorData} fallbackMessage="Something went wrong while deleting the URL :(" />
|
2020-12-21 19:54:05 +03:00
|
|
|
</Result>
|
2020-05-31 11:23:13 +03:00
|
|
|
)}
|
|
|
|
</ModalBody>
|
|
|
|
<ModalFooter>
|
|
|
|
<button type="button" className="btn btn-link" onClick={close}>Cancel</button>
|
|
|
|
<button
|
|
|
|
type="submit"
|
|
|
|
className="btn btn-danger"
|
2022-11-06 14:46:29 +03:00
|
|
|
disabled={inputValue !== shortUrl.shortCode || loading}
|
2020-05-31 11:23:13 +03:00
|
|
|
>
|
2022-11-06 14:46:29 +03:00
|
|
|
{loading ? 'Deleting...' : 'Delete'}
|
2020-05-31 11:23:13 +03:00
|
|
|
</button>
|
|
|
|
</ModalFooter>
|
|
|
|
</form>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
};
|