2020-05-31 10:23:13 +02:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2018-09-16 09:35:39 +02:00
|
|
|
import { Modal, ModalBody, ModalFooter, ModalHeader } from 'reactstrap';
|
|
|
|
import PropTypes from 'prop-types';
|
2020-01-31 20:12:22 +01:00
|
|
|
import { identity, pipe } from 'ramda';
|
2018-09-16 09:35:39 +02:00
|
|
|
import { shortUrlType } from '../reducers/shortUrlsList';
|
2018-12-17 23:11:55 +01:00
|
|
|
import { shortUrlDeletionType } from '../reducers/shortUrlDeletion';
|
2018-09-16 09:35:39 +02:00
|
|
|
|
2020-01-11 12:24:45 +01:00
|
|
|
const THRESHOLD_REACHED = 'INVALID_SHORTCODE_DELETION';
|
|
|
|
|
2020-05-31 10:23:13 +02:00
|
|
|
const propTypes = {
|
|
|
|
shortUrl: shortUrlType,
|
|
|
|
toggle: PropTypes.func,
|
|
|
|
isOpen: PropTypes.bool,
|
|
|
|
shortUrlDeletion: shortUrlDeletionType,
|
|
|
|
deleteShortUrl: PropTypes.func,
|
|
|
|
resetDeleteShortUrl: PropTypes.func,
|
|
|
|
};
|
|
|
|
|
|
|
|
const DeleteShortUrlModal = ({ shortUrl, toggle, isOpen, shortUrlDeletion, resetDeleteShortUrl, deleteShortUrl }) => {
|
|
|
|
const [ inputValue, setInputValue ] = useState('');
|
2018-09-16 09:35:39 +02:00
|
|
|
|
2020-05-31 10:23:13 +02:00
|
|
|
useEffect(() => resetDeleteShortUrl, []);
|
|
|
|
|
|
|
|
const { error, errorData } = shortUrlDeletion;
|
2020-08-26 18:55:40 +02:00
|
|
|
const errorCode = error && errorData && (errorData.type || errorData.error);
|
2020-05-31 10:23:13 +02:00
|
|
|
const hasThresholdError = errorCode === THRESHOLD_REACHED;
|
|
|
|
const hasErrorOtherThanThreshold = error && errorCode !== THRESHOLD_REACHED;
|
|
|
|
const close = pipe(resetDeleteShortUrl, toggle);
|
|
|
|
const handleDeleteUrl = (e) => {
|
2018-09-16 10:47:17 +02:00
|
|
|
e.preventDefault();
|
|
|
|
|
2020-02-08 09:57:18 +01:00
|
|
|
const { shortCode, domain } = shortUrl;
|
2018-09-16 10:47:17 +02:00
|
|
|
|
2020-02-08 09:57:18 +01:00
|
|
|
deleteShortUrl(shortCode, domain)
|
2020-01-31 20:12:22 +01:00
|
|
|
.then(toggle)
|
2018-09-16 10:47:17 +02:00
|
|
|
.catch(identity);
|
|
|
|
};
|
|
|
|
|
2020-05-31 10:23:13 +02:00
|
|
|
return (
|
|
|
|
<Modal isOpen={isOpen} toggle={close} centered>
|
|
|
|
<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>
|
2018-09-16 09:35:39 +02:00
|
|
|
|
2020-05-31 10:23:13 +02:00
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
className="form-control"
|
|
|
|
placeholder="Insert the short code of the URL"
|
|
|
|
value={inputValue}
|
|
|
|
onChange={(e) => setInputValue(e.target.value)}
|
|
|
|
/>
|
2018-09-16 09:35:39 +02:00
|
|
|
|
2020-05-31 10:23:13 +02:00
|
|
|
{hasThresholdError && (
|
|
|
|
<div className="p-2 mt-2 bg-warning text-center">
|
|
|
|
{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.'}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{hasErrorOtherThanThreshold && (
|
|
|
|
<div className="p-2 mt-2 bg-danger text-white text-center">
|
|
|
|
Something went wrong while deleting the URL :(
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</ModalBody>
|
|
|
|
<ModalFooter>
|
|
|
|
<button type="button" className="btn btn-link" onClick={close}>Cancel</button>
|
|
|
|
<button
|
|
|
|
type="submit"
|
|
|
|
className="btn btn-danger"
|
|
|
|
disabled={inputValue !== shortUrl.shortCode || shortUrlDeletion.loading}
|
|
|
|
>
|
|
|
|
{shortUrlDeletion.loading ? 'Deleting...' : 'Delete'}
|
|
|
|
</button>
|
|
|
|
</ModalFooter>
|
|
|
|
</form>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
};
|
2018-09-16 09:35:39 +02:00
|
|
|
|
2020-05-31 10:23:13 +02:00
|
|
|
DeleteShortUrlModal.propTypes = propTypes;
|
2018-09-16 10:47:17 +02:00
|
|
|
|
2020-05-31 10:23:13 +02:00
|
|
|
export default DeleteShortUrlModal;
|