Completed
Push — master ( 158ed8...34f194 )
by Alejandro
04:53 queued 02:19
created

DeleteShortUrlModal.render   B

Complexity

Conditions 2

Size

Total Lines 48
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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