1 | import React, { useContext, useState, useEffect, useRef } from 'react';
|
---|
2 | import { useNavigate } from 'react-router-dom';
|
---|
3 | import { OrderContext } from './OrderContext';
|
---|
4 | import { FaBiking } from 'react-icons/fa';
|
---|
5 | import styles from '../../css/ShoppingCartCss/order-notification.module.css';
|
---|
6 |
|
---|
7 | const OrderNotification = () => {
|
---|
8 | const { isOrderInProgress, itemsWithQuantities } = useContext(OrderContext);
|
---|
9 | const [showPopup, setShowPopup] = useState(false);
|
---|
10 | const navigate = useNavigate();
|
---|
11 | const popupRef = useRef(null);
|
---|
12 |
|
---|
13 | const handleNotificationClick = () => {
|
---|
14 | setShowPopup(!showPopup);
|
---|
15 | };
|
---|
16 |
|
---|
17 | const handleGoToOrder = () => {
|
---|
18 | navigate('/delivery-details', { state: { itemsWithQuantities } });
|
---|
19 | };
|
---|
20 |
|
---|
21 | useEffect(() => {
|
---|
22 | const handleClickOutside = (event) => {
|
---|
23 | if (popupRef.current && !popupRef.current.contains(event.target)) {
|
---|
24 | setShowPopup(false);
|
---|
25 | }
|
---|
26 | };
|
---|
27 |
|
---|
28 | if (showPopup) {
|
---|
29 | document.addEventListener('mousedown', handleClickOutside);
|
---|
30 | } else {
|
---|
31 | document.removeEventListener('mousedown', handleClickOutside);
|
---|
32 | }
|
---|
33 |
|
---|
34 | return () => {
|
---|
35 | document.removeEventListener('mousedown', handleClickOutside);
|
---|
36 | };
|
---|
37 | }, [showPopup]);
|
---|
38 |
|
---|
39 | return (
|
---|
40 | <>
|
---|
41 | {isOrderInProgress && (
|
---|
42 | <div className={styles.notificationCircle} onClick={handleNotificationClick}>
|
---|
43 | <FaBiking className={styles.notificationIcon} />
|
---|
44 | <span className={styles.exclamationMark}>!</span>
|
---|
45 | </div>
|
---|
46 | )}
|
---|
47 |
|
---|
48 | {showPopup && (
|
---|
49 | <div ref={popupRef} className={styles.notificationPopup}>
|
---|
50 | <p>Active Order</p>
|
---|
51 | <button onClick={handleGoToOrder}>Go to Order</button>
|
---|
52 | </div>
|
---|
53 | )}
|
---|
54 | </>
|
---|
55 | );
|
---|
56 | };
|
---|
57 |
|
---|
58 | export default OrderNotification;
|
---|