shlink-web-client/src/short-urls/helpers/DeleteShortUrlModal.js

102 lines
3.3 KiB
JavaScript
Raw Normal View History

2018-09-16 10:35:39 +03:00
import React, { Component } from 'react';
2018-09-16 11:47:17 +03:00
import { connect } from 'react-redux';
2018-09-16 10:35:39 +03:00
import { Modal, ModalBody, ModalFooter, ModalHeader } from 'reactstrap';
import PropTypes from 'prop-types';
2018-09-16 11:47:17 +03:00
import { pick, identity } from 'ramda';
2018-09-16 10:35:39 +03:00
import { shortUrlType } from '../reducers/shortUrlsList';
2018-09-16 11:47:17 +03:00
import {
deleteShortUrl,
resetDeleteShortUrl,
shortUrlDeleted,
shortUrlDeletionType,
} from '../reducers/shortUrlDeletion';
2018-09-16 10:35:39 +03:00
import './QrCodeModal.scss';
2018-09-16 11:47:17 +03:00
export class DeleteShortUrlModalComponent extends Component {
2018-09-16 10:35:39 +03:00
static propTypes = {
shortUrl: shortUrlType,
toggle: PropTypes.func,
isOpen: PropTypes.bool,
2018-09-16 11:47:17 +03:00
shortUrlDeletion: shortUrlDeletionType,
deleteShortUrl: PropTypes.func,
resetDeleteShortUrl: PropTypes.func,
shortUrlDeleted: PropTypes.func,
2018-09-16 10:35:39 +03:00
};
state = { inputValue: '' };
2018-09-16 11:47:17 +03:00
handleDeleteUrl = (e) => {
e.preventDefault();
const { deleteShortUrl, shortUrl, toggle, shortUrlDeleted } = this.props;
const { shortCode } = shortUrl;
deleteShortUrl(shortCode)
.then(() => {
shortUrlDeleted(shortCode);
toggle();
})
.catch(identity);
};
componentWillUnmount() {
const { resetDeleteShortUrl } = this.props;
resetDeleteShortUrl();
}
2018-09-16 10:35:39 +03:00
render() {
2018-09-16 11:47:17 +03:00
const { shortUrl, toggle, isOpen, shortUrlDeletion } = this.props;
const THRESHOLD_REACHED = 'INVALID_SHORTCODE_DELETION';
2018-09-16 10:35:39 +03:00
return (
<Modal isOpen={isOpen} toggle={toggle} centered>
2018-09-16 11:47:17 +03:00
<form onSubmit={this.handleDeleteUrl}>
2018-09-16 10:35:39 +03:00
<ModalHeader toggle={toggle}>
<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>
<input
type="text"
className="form-control"
placeholder="Insert the short code of the URL"
value={this.state.inputValue}
onChange={(e) => this.setState({ inputValue: e.target.value })}
/>
2018-09-16 11:47:17 +03:00
{shortUrlDeletion.error && shortUrlDeletion.errorData.error === THRESHOLD_REACHED && (
<div className="p-2 mt-2 bg-warning text-center">
This short URL has received too many visits and therefore, it cannot be deleted
</div>
)}
{shortUrlDeletion.error && shortUrlDeletion.errorData.error !== THRESHOLD_REACHED && (
<div className="p-2 mt-2 bg-danger text-white text-center">
Something went wrong while deleting the URL :(
</div>
)}
2018-09-16 10:35:39 +03:00
</ModalBody>
<ModalFooter>
<button type="button" className="btn btn-link" onClick={toggle}>Cancel</button>
<button
type="submit"
className="btn btn-danger"
2018-09-16 11:47:17 +03:00
disabled={this.state.inputValue !== shortUrl.shortCode || shortUrlDeletion.loading}
2018-09-16 10:35:39 +03:00
>
2018-09-16 11:47:17 +03:00
{shortUrlDeletion.loading ? 'Deleting...' : 'Delete'}
2018-09-16 10:35:39 +03:00
</button>
</ModalFooter>
</form>
</Modal>
);
}
}
2018-09-16 11:47:17 +03:00
const DeleteShortUrlModal = connect(
pick([ 'shortUrlDeletion' ]),
{ deleteShortUrl, resetDeleteShortUrl, shortUrlDeleted }
)(DeleteShortUrlModalComponent);
export default DeleteShortUrlModal;