From 5021491503252a84accb5d936297cbec0960952f Mon Sep 17 00:00:00 2001 From: Valik Date: Tue, 23 Jan 2024 10:05:20 +0100 Subject: [PATCH] feat(ui): replace alert with modal window --- src/components/ContactForm.jsx | 126 +++++++++++++++++++-------------- src/components/ModalAlert.jsx | 32 +++++++++ 2 files changed, 106 insertions(+), 52 deletions(-) create mode 100644 src/components/ModalAlert.jsx diff --git a/src/components/ContactForm.jsx b/src/components/ContactForm.jsx index 9df1854..f901668 100644 --- a/src/components/ContactForm.jsx +++ b/src/components/ContactForm.jsx @@ -2,6 +2,8 @@ import { Component } from 'react'; import { nanoid } from 'nanoid'; import PropTypes from 'prop-types'; +import { ModalAlert } from './ModalAlert'; + import { Form, Row, Col, Button, FloatingLabel } from 'react-bootstrap'; /** @@ -14,10 +16,12 @@ export class ContactForm extends Component { this.state = { name: '', number: '', + modalShow: false, }; this.handleChange = this.handleChange.bind(this); this.handleFormSubmit = this.handleFormSubmit.bind(this); + this.setModalShow = this.setModalShow.bind(this); } /** @@ -28,6 +32,16 @@ export class ContactForm extends Component { this.setState({ [e.target.name]: e.target.value }); } + /** + * Sets the state for displaying or hiding the modal. + * @param {boolean} value - A flag indicating whether to show (true) or hide (false) the modal. + * @param {string} name - The name of the contact to be displayed in the modal. + */ + setModalShow = (value, name) => { + // Set the new state with the provided values + this.setState({ modalShow: value, name }); + }; + /** * Handle form submission for adding a new contact. * @param {Object} e - The event object. @@ -39,7 +53,7 @@ export class ContactForm extends Component { const { contacts, addContact } = this.props; if (contacts.some(contact => contact.name === name)) { - alert(`Contact with name "${name}" already exists!`); + this.setModalShow(true, name, number); return; } @@ -55,59 +69,67 @@ export class ContactForm extends Component { } render() { - const { name, number } = this.state; + const { name, number, modalShow } = this.state; return ( -
- - - - - - - - - - - - - - - - - - -
+ <> +
+ + + + + + + + + + + + + + + + + + +
+ + this.setModalShow(false, '')} + /> + ); } } diff --git a/src/components/ModalAlert.jsx b/src/components/ModalAlert.jsx new file mode 100644 index 0000000..b51abc8 --- /dev/null +++ b/src/components/ModalAlert.jsx @@ -0,0 +1,32 @@ +import PropTypes from 'prop-types'; +import { Modal, Button } from 'react-bootstrap'; + +/** + * Functional component for rendering an alert modal. + * @param {Object} props - Component properties. + * @param {string} props.name - The name of the contact displayed in the modal. + * @param {Function} props.onHide - Callback function to handle modal hiding. + * @param {boolean} props.show - A flag indicating whether the modal should be visible or hidden. + * @returns {JSX.Element} - The rendered ModalAlert component. + */ +export const ModalAlert = ({ name, onHide, show }) => { + return ( + + + + Contact with name {name} already + exists! + + + + + + ); +}; + +// PropTypes +ModalAlert.propTypes = { + name: PropTypes.string.isRequired, + onHide: PropTypes.func.isRequired, + show: PropTypes.bool.isRequired, +};