1
|
|
|
import React, { useEffect, useState } from 'react'; |
2
|
|
|
import { Modal, ModalBody, ModalFooter, ModalHeader } from 'reactstrap'; |
3
|
|
|
import PropTypes from 'prop-types'; |
4
|
|
|
import { identity, pipe } from 'ramda'; |
5
|
|
|
import { shortUrlType } from '../reducers/shortUrlsList'; |
6
|
|
|
import { shortUrlDeletionType } from '../reducers/shortUrlDeletion'; |
7
|
|
|
|
8
|
1 |
|
const THRESHOLD_REACHED = 'INVALID_SHORTCODE_DELETION'; |
9
|
|
|
|
10
|
1 |
|
const propTypes = { |
11
|
|
|
shortUrl: shortUrlType, |
12
|
|
|
toggle: PropTypes.func, |
13
|
|
|
isOpen: PropTypes.bool, |
14
|
|
|
shortUrlDeletion: shortUrlDeletionType, |
15
|
|
|
deleteShortUrl: PropTypes.func, |
16
|
|
|
resetDeleteShortUrl: PropTypes.func, |
17
|
|
|
}; |
18
|
|
|
|
19
|
1 |
|
const DeleteShortUrlModal = ({ shortUrl, toggle, isOpen, shortUrlDeletion, resetDeleteShortUrl, deleteShortUrl }) => { |
20
|
10 |
|
const [ inputValue, setInputValue ] = useState(''); |
21
|
|
|
|
22
|
10 |
|
useEffect(() => resetDeleteShortUrl, []); |
23
|
|
|
|
24
|
10 |
|
const { error, errorData } = shortUrlDeletion; |
25
|
10 |
|
const errorCode = error && (errorData.type || errorData.error); |
26
|
10 |
|
const hasThresholdError = errorCode === THRESHOLD_REACHED; |
27
|
10 |
|
const hasErrorOtherThanThreshold = error && errorCode !== THRESHOLD_REACHED; |
28
|
10 |
|
const close = pipe(resetDeleteShortUrl, toggle); |
29
|
10 |
|
const handleDeleteUrl = (e) => { |
30
|
1 |
|
e.preventDefault(); |
31
|
|
|
|
32
|
1 |
|
const { shortCode, domain } = shortUrl; |
33
|
|
|
|
34
|
1 |
|
deleteShortUrl(shortCode, domain) |
35
|
|
|
.then(toggle) |
36
|
|
|
.catch(identity); |
37
|
|
|
}; |
38
|
|
|
|
39
|
10 |
|
return ( |
40
|
|
|
<Modal isOpen={isOpen} toggle={close} centered> |
41
|
|
|
<form onSubmit={handleDeleteUrl}> |
42
|
|
|
<ModalHeader toggle={close}> |
43
|
|
|
<span className="text-danger">Delete short URL</span> |
44
|
|
|
</ModalHeader> |
45
|
|
|
<ModalBody> |
46
|
|
|
<p><b className="text-danger">Caution!</b> You are about to delete a short URL.</p> |
47
|
|
|
<p>This action cannot be undone. Once you have deleted it, all the visits stats will be lost.</p> |
48
|
|
|
|
49
|
|
|
<input |
50
|
|
|
type="text" |
51
|
|
|
className="form-control" |
52
|
|
|
placeholder="Insert the short code of the URL" |
53
|
|
|
value={inputValue} |
54
|
2 |
|
onChange={(e) => setInputValue(e.target.value)} |
55
|
|
|
/> |
56
|
|
|
|
57
|
|
|
{hasThresholdError && ( |
58
|
|
|
<div className="p-2 mt-2 bg-warning text-center"> |
59
|
|
|
{errorData.threshold && `This short URL has received more than ${errorData.threshold} visits, and therefore, it cannot be deleted.`} |
60
|
|
|
{!errorData.threshold && 'This short URL has received too many visits, and therefore, it cannot be deleted.'} |
61
|
|
|
</div> |
62
|
|
|
)} |
63
|
|
|
{hasErrorOtherThanThreshold && ( |
64
|
|
|
<div className="p-2 mt-2 bg-danger text-white text-center"> |
65
|
|
|
Something went wrong while deleting the URL :( |
66
|
|
|
</div> |
67
|
|
|
)} |
68
|
|
|
</ModalBody> |
69
|
|
|
<ModalFooter> |
70
|
|
|
<button type="button" className="btn btn-link" onClick={close}>Cancel</button> |
71
|
|
|
<button |
72
|
|
|
type="submit" |
73
|
|
|
className="btn btn-danger" |
74
|
|
|
disabled={inputValue !== shortUrl.shortCode || shortUrlDeletion.loading} |
75
|
|
|
> |
76
|
|
|
{shortUrlDeletion.loading ? 'Deleting...' : 'Delete'} |
77
|
|
|
</button> |
78
|
|
|
</ModalFooter> |
79
|
|
|
</form> |
80
|
|
|
</Modal> |
81
|
|
|
); |
82
|
|
|
}; |
83
|
|
|
|
84
|
1 |
|
DeleteShortUrlModal.propTypes = propTypes; |
85
|
|
|
|
86
|
|
|
export default DeleteShortUrlModal; |
87
|
|
|
|