Completed
Pull Request — master (#238)
by Alejandro
09:25
created

src/short-urls/helpers/EditShortUrlModal.js   A

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 58
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 1
eloc 52
mnd 1
bc 1
fnc 0
dl 0
loc 58
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
ccs 9
cts 10
cp 0.9
rs 10
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