|
1 | | -import React, { useEffect, useState } from "react"; |
| 1 | +import React, { useEffect, useRef, useState } from "react"; |
2 | 2 | import { useAuth } from "../contexts/AuthContext"; |
3 | 3 | import { doc, getDoc } from "firebase/firestore"; |
4 | 4 | import { db } from "../config/firebase"; |
5 | 5 |
|
6 | 6 | function NoUsagesModal({ isOpen, onClose }) { |
7 | 7 | const { currentUser } = useAuth(); |
8 | 8 | const [timeLeft, setTimeLeft] = useState(null); |
| 9 | + const modalRef = useRef(null); |
9 | 10 |
|
10 | 11 | useEffect(() => { |
11 | 12 | const fetchResetTime = async () => { |
@@ -41,10 +42,30 @@ function NoUsagesModal({ isOpen, onClose }) { |
41 | 42 | fetchResetTime(); |
42 | 43 | }, [currentUser.uid]); |
43 | 44 |
|
| 45 | + useEffect(() => { |
| 46 | + // Close modal if clicked outside |
| 47 | + const handleClickOutside = (event) => { |
| 48 | + if (modalRef.current && !modalRef.current.contains(event.target)) { |
| 49 | + onClose(); |
| 50 | + } |
| 51 | + }; |
| 52 | + |
| 53 | + if (isOpen) { |
| 54 | + document.addEventListener("mousedown", handleClickOutside); |
| 55 | + } |
| 56 | + |
| 57 | + return () => { |
| 58 | + document.removeEventListener("mousedown", handleClickOutside); |
| 59 | + }; |
| 60 | + }, [isOpen, onClose]); |
| 61 | + |
44 | 62 | if (!isOpen) return null; |
45 | 63 | return ( |
46 | 64 | <div className="fixed inset-0 z-50 flex items-center justify-center bg-black bg-opacity-40"> |
47 | | - <div className="bg-white border border-gray rounded-2xl shadow-md p-8 max-w-md w-[90%] text-center"> |
| 65 | + <div |
| 66 | + ref={modalRef} |
| 67 | + className="bg-white border border-gray rounded-2xl shadow-md p-8 max-w-md w-[90%] text-center" |
| 68 | + > |
48 | 69 | <h2 className="text-2xl font-bold text-text mb-3"> |
49 | 70 | No more free uses today |
50 | 71 | </h2> |
|
0 commit comments