// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: MIT-0 import React, { useEffect, useCallback } from 'react'; import PropTypes from 'prop-types'; import * as config from '../../config'; // Stylesheets import "./Modal.css"; // Assets import close_icon from '../../assets/cancel-24px.svg'; const Modal = ({ productId, closeModal, products, }) => { // only show details of selected product const details = products.filter(product => product.id === productId); const escFunction = useCallback((event) => { if(event.keyCode === 27) { // keyCode 27 is Esc char closeModal(); } }, [closeModal]); useEffect(() => { // add listener for Esc to close modal document.addEventListener("keydown", escFunction); return () => { // upon closing modal, remove listener document.removeEventListener("keydown", escFunction); }; }, []); const handleModalOnClick = (e) => { e.preventDefault(); e.stopPropagation(); } const { id, name, price, discountedPrice, longDescription } = details[0]; let { imageLargeUrl } = details[0]; // if using mock data, reference the image in the public folder if (config.USE_MOCK_DATA) { imageLargeUrl = `${process.env.PUBLIC_URL}/${imageLargeUrl}`; } const buyNowPrice = discountedPrice || price; return (
{id}
{name}
{longDescription}
close
) } Modal.propTypes = { productId: PropTypes.string, closeModal: PropTypes.func, products: PropTypes.array, }; export default Modal;