import React, { Component } from 'react'; import PropTypes from 'prop-types'; import ReactModal from 'react-modal'; import classnames from 'classnames'; import { R_URL_REQUIRES_PROTOCOL } from '../../helpers/constants'; import './Modal.css'; ReactModal.setAppElement('#root'); const initialState = { url: '', name: '', isUrlValid: false, }; export default class Modal extends Component { state = initialState; // eslint-disable-next-line isUrlValid = url => { return R_URL_REQUIRES_PROTOCOL.test(url); }; handleUrlChange = async (e) => { const { value: url } = e.currentTarget; if (this.isUrlValid(url)) { this.setState(...this.state, { url, isUrlValid: true }); } else { this.setState(...this.state, { url, isUrlValid: false }); } }; handleNameChange = (e) => { const { value: name } = e.currentTarget; this.setState({ ...this.state, name }); }; handleNext = () => { this.props.addFilter(this.state.url, this.state.name); setTimeout(() => { if (this.props.isFilterAdded) { this.closeModal(); } }, 2000); }; closeModal = () => { this.props.toggleModal(); this.setState({ ...this.state, ...initialState }); } render() { const { isOpen, title, inputDescription, } = this.props; const { isUrlValid, url, name } = this.state; const inputUrlClass = classnames({ 'form-control mb-2': true, 'is-invalid': url.length > 0 && !isUrlValid, 'is-valid': url.length > 0 && isUrlValid, }); const inputNameClass = classnames({ 'form-control mb-2': true, 'is-valid': name.length > 0, }); const renderBody = () => { if (!this.props.isFilterAdded) { return ( {inputDescription &&
{inputDescription}
}
); } return (
Url added successfully
); }; const isValidForSubmit = !(url.length > 0 && isUrlValid && name.length > 0); return (

{title}

{ renderBody()}
{ !this.props.isFilterAdded &&
}
); } } Modal.propTypes = { toggleModal: PropTypes.func.isRequired, isOpen: PropTypes.bool.isRequired, title: PropTypes.string.isRequired, inputDescription: PropTypes.string, addFilter: PropTypes.func.isRequired, isFilterAdded: PropTypes.bool, };