mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2025-01-11 02:37:22 +03:00
Updated DeleteShortUrlModal to be a functional component
This commit is contained in:
parent
2268b85ade
commit
d0b3edaa2f
1 changed files with 64 additions and 68 deletions
|
@ -1,4 +1,4 @@
|
||||||
import React from 'react';
|
import React, { useEffect, useState } from 'react';
|
||||||
import { Modal, ModalBody, ModalFooter, ModalHeader } from 'reactstrap';
|
import { Modal, ModalBody, ModalFooter, ModalHeader } from 'reactstrap';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import { identity, pipe } from 'ramda';
|
import { identity, pipe } from 'ramda';
|
||||||
|
@ -7,21 +7,28 @@ import { shortUrlDeletionType } from '../reducers/shortUrlDeletion';
|
||||||
|
|
||||||
const THRESHOLD_REACHED = 'INVALID_SHORTCODE_DELETION';
|
const THRESHOLD_REACHED = 'INVALID_SHORTCODE_DELETION';
|
||||||
|
|
||||||
export default class DeleteShortUrlModal extends React.Component {
|
const propTypes = {
|
||||||
static propTypes = {
|
shortUrl: shortUrlType,
|
||||||
shortUrl: shortUrlType,
|
toggle: PropTypes.func,
|
||||||
toggle: PropTypes.func,
|
isOpen: PropTypes.bool,
|
||||||
isOpen: PropTypes.bool,
|
shortUrlDeletion: shortUrlDeletionType,
|
||||||
shortUrlDeletion: shortUrlDeletionType,
|
deleteShortUrl: PropTypes.func,
|
||||||
deleteShortUrl: PropTypes.func,
|
resetDeleteShortUrl: PropTypes.func,
|
||||||
resetDeleteShortUrl: PropTypes.func,
|
};
|
||||||
};
|
|
||||||
|
|
||||||
state = { inputValue: '' };
|
const DeleteShortUrlModal = ({ shortUrl, toggle, isOpen, shortUrlDeletion, resetDeleteShortUrl, deleteShortUrl }) => {
|
||||||
handleDeleteUrl = (e) => {
|
const [ inputValue, setInputValue ] = useState('');
|
||||||
|
|
||||||
|
useEffect(() => resetDeleteShortUrl, []);
|
||||||
|
|
||||||
|
const { error, errorData } = shortUrlDeletion;
|
||||||
|
const errorCode = error && (errorData.type || errorData.error);
|
||||||
|
const hasThresholdError = errorCode === THRESHOLD_REACHED;
|
||||||
|
const hasErrorOtherThanThreshold = error && errorCode !== THRESHOLD_REACHED;
|
||||||
|
const close = pipe(resetDeleteShortUrl, toggle);
|
||||||
|
const handleDeleteUrl = (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
const { deleteShortUrl, shortUrl, toggle } = this.props;
|
|
||||||
const { shortCode, domain } = shortUrl;
|
const { shortCode, domain } = shortUrl;
|
||||||
|
|
||||||
deleteShortUrl(shortCode, domain)
|
deleteShortUrl(shortCode, domain)
|
||||||
|
@ -29,62 +36,51 @@ export default class DeleteShortUrlModal extends React.Component {
|
||||||
.catch(identity);
|
.catch(identity);
|
||||||
};
|
};
|
||||||
|
|
||||||
componentWillUnmount() {
|
return (
|
||||||
const { resetDeleteShortUrl } = this.props;
|
<Modal isOpen={isOpen} toggle={close} centered>
|
||||||
|
<form onSubmit={handleDeleteUrl}>
|
||||||
|
<ModalHeader toggle={close}>
|
||||||
|
<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>
|
||||||
|
|
||||||
resetDeleteShortUrl();
|
<input
|
||||||
}
|
type="text"
|
||||||
|
className="form-control"
|
||||||
|
placeholder="Insert the short code of the URL"
|
||||||
|
value={inputValue}
|
||||||
|
onChange={(e) => setInputValue(e.target.value)}
|
||||||
|
/>
|
||||||
|
|
||||||
render() {
|
{hasThresholdError && (
|
||||||
const { shortUrl, toggle, isOpen, shortUrlDeletion, resetDeleteShortUrl } = this.props;
|
<div className="p-2 mt-2 bg-warning text-center">
|
||||||
const { error, errorData } = shortUrlDeletion;
|
{errorData.threshold && `This short URL has received more than ${errorData.threshold} visits, and therefore, it cannot be deleted.`}
|
||||||
const errorCode = error && (errorData.type || errorData.error);
|
{!errorData.threshold && 'This short URL has received too many visits, and therefore, it cannot be deleted.'}
|
||||||
const hasThresholdError = errorCode === THRESHOLD_REACHED;
|
</div>
|
||||||
const hasErrorOtherThanThreshold = error && errorCode !== THRESHOLD_REACHED;
|
)}
|
||||||
const close = pipe(resetDeleteShortUrl, toggle);
|
{hasErrorOtherThanThreshold && (
|
||||||
|
<div className="p-2 mt-2 bg-danger text-white text-center">
|
||||||
|
Something went wrong while deleting the URL :(
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</ModalBody>
|
||||||
|
<ModalFooter>
|
||||||
|
<button type="button" className="btn btn-link" onClick={close}>Cancel</button>
|
||||||
|
<button
|
||||||
|
type="submit"
|
||||||
|
className="btn btn-danger"
|
||||||
|
disabled={inputValue !== shortUrl.shortCode || shortUrlDeletion.loading}
|
||||||
|
>
|
||||||
|
{shortUrlDeletion.loading ? 'Deleting...' : 'Delete'}
|
||||||
|
</button>
|
||||||
|
</ModalFooter>
|
||||||
|
</form>
|
||||||
|
</Modal>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
DeleteShortUrlModal.propTypes = propTypes;
|
||||||
<Modal isOpen={isOpen} toggle={close} centered>
|
|
||||||
<form onSubmit={this.handleDeleteUrl}>
|
|
||||||
<ModalHeader toggle={close}>
|
|
||||||
<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
|
export default DeleteShortUrlModal;
|
||||||
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 })}
|
|
||||||
/>
|
|
||||||
|
|
||||||
{hasThresholdError && (
|
|
||||||
<div className="p-2 mt-2 bg-warning text-center">
|
|
||||||
{errorData.threshold && `This short URL has received more than ${errorData.threshold} visits, and therefore, it cannot be deleted.`}
|
|
||||||
{!errorData.threshold && 'This short URL has received too many visits, and therefore, it cannot be deleted.'}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
{hasErrorOtherThanThreshold && (
|
|
||||||
<div className="p-2 mt-2 bg-danger text-white text-center">
|
|
||||||
Something went wrong while deleting the URL :(
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</ModalBody>
|
|
||||||
<ModalFooter>
|
|
||||||
<button type="button" className="btn btn-link" onClick={close}>Cancel</button>
|
|
||||||
<button
|
|
||||||
type="submit"
|
|
||||||
className="btn btn-danger"
|
|
||||||
disabled={this.state.inputValue !== shortUrl.shortCode || shortUrlDeletion.loading}
|
|
||||||
>
|
|
||||||
{shortUrlDeletion.loading ? 'Deleting...' : 'Delete'}
|
|
||||||
</button>
|
|
||||||
</ModalFooter>
|
|
||||||
</form>
|
|
||||||
</Modal>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue