1
|
|
|
import React, { useState } from 'react'; |
2
|
|
|
import PropTypes from 'prop-types'; |
3
|
|
|
import { Modal, ModalBody, ModalFooter, ModalHeader, FormGroup, Input, Button } from 'reactstrap'; |
4
|
|
|
import { ExternalLink } from 'react-external-link'; |
5
|
|
|
import { shortUrlType } from '../reducers/shortUrlsList'; |
6
|
|
|
import { ShortUrlEditionType } from '../reducers/shortUrlEdition'; |
7
|
|
|
import { hasValue } from '../../utils/utils'; |
8
|
|
|
|
9
|
1 |
|
const propTypes = { |
10
|
|
|
isOpen: PropTypes.bool.isRequired, |
11
|
|
|
toggle: PropTypes.func.isRequired, |
12
|
|
|
shortUrl: shortUrlType.isRequired, |
13
|
|
|
shortUrlEdition: ShortUrlEditionType, |
14
|
|
|
editShortUrl: PropTypes.func, |
15
|
|
|
}; |
16
|
|
|
|
17
|
1 |
|
const EditShortUrlModal = ({ isOpen, toggle, shortUrl, shortUrlEdition, editShortUrl }) => { |
18
|
10 |
|
const { saving, error } = shortUrlEdition; |
19
|
10 |
|
const url = shortUrl && (shortUrl.shortUrl || ''); |
20
|
10 |
|
const [ longUrl, setLongUrl ] = useState(shortUrl.longUrl); |
21
|
|
|
|
22
|
10 |
|
const doEdit = () => editShortUrl(shortUrl.shortCode, shortUrl.domain, longUrl).then(toggle); |
23
|
|
|
|
24
|
10 |
|
return ( |
25
|
|
|
<Modal isOpen={isOpen} toggle={toggle} centered> |
26
|
|
|
<ModalHeader toggle={toggle}> |
27
|
|
|
Edit long URL for <ExternalLink href={url} /> |
28
|
|
|
</ModalHeader> |
29
|
2 |
|
<form onSubmit={(e) => e.preventDefault() || doEdit()}> |
30
|
|
|
<ModalBody> |
31
|
|
|
<FormGroup className="mb-0"> |
32
|
|
|
<Input |
33
|
|
|
type="url" |
34
|
|
|
required |
35
|
|
|
placeholder="Long URL" |
36
|
|
|
value={longUrl} |
37
|
|
|
onChange={(e) => setLongUrl(e.target.value)} |
38
|
|
|
/> |
39
|
|
|
</FormGroup> |
40
|
|
|
{error && ( |
41
|
|
|
<div className="p-2 mt-2 bg-danger text-white text-center"> |
42
|
|
|
Something went wrong while saving the long URL :( |
43
|
|
|
</div> |
44
|
|
|
)} |
45
|
|
|
</ModalBody> |
46
|
|
|
<ModalFooter> |
47
|
|
|
<Button color="link" onClick={toggle}>Cancel</Button> |
48
|
|
|
<Button color="primary" disabled={saving || !hasValue(longUrl)}>{saving ? 'Saving...' : 'Save'}</Button> |
49
|
|
|
</ModalFooter> |
50
|
|
|
</form> |
51
|
|
|
</Modal> |
52
|
|
|
); |
53
|
|
|
}; |
54
|
|
|
|
55
|
1 |
|
EditShortUrlModal.propTypes = propTypes; |
56
|
|
|
|
57
|
|
|
export default EditShortUrlModal; |
58
|
|
|
|