|
1 |
| -import React from 'react'; |
2 |
| -import {Oval} from 'react-loader-spinner'; |
3 |
| -import './LoadPopup.css'; |
| 1 | +import React, { useState, useEffect } from 'react'; |
| 2 | +import { Oval } from 'react-loader-spinner'; |
| 3 | +import axios from 'axios'; |
| 4 | +import './LoadPopup.css'; |
4 | 5 |
|
5 |
| -export const LoadPopup= ({isOpen, isClose}) => { |
6 |
| - if (isOpen) { |
7 |
| - return ( |
8 |
| - <div className="load-overlay"> |
9 |
| - <div className="load-popup"> |
10 |
| - <h3>Finding you a match...</h3> |
11 |
| - <div className="spinner"> |
12 |
| - <Oval className = "spinner"/> |
13 |
| - </div> |
14 |
| - <div > |
15 |
| - <button onClick={isClose} className="cancel-match">Cancel Matching</button> |
16 |
| - </div> |
17 |
| - </div> |
18 |
| - </div> |
19 |
| - ); |
20 |
| - } |
| 6 | +export const LoadPopup = ({ isOpen, isClose, userId, onMatchCancelled }) => { |
| 7 | + const [buttonEnabled, setButtonEnabled] = useState(false); |
| 8 | + |
| 9 | + useEffect(() => { |
| 10 | + setButtonEnabled(false); |
| 11 | + const timer = setTimeout(() => { |
| 12 | + setButtonEnabled(true); |
| 13 | + }, 5000); |
| 14 | + |
| 15 | + return () => clearTimeout(timer); |
| 16 | + }, [isOpen]); |
| 17 | + |
| 18 | + const handleCancelMatching = () => { |
| 19 | + setButtonEnabled(false); |
| 20 | + |
| 21 | + const URL = `http://localhost:3004/home/${userId}/matching`; |
| 22 | + axios.delete(URL) |
| 23 | + .then(response => { |
| 24 | + if (response.status !== 200) { |
| 25 | + throw new Error(response.data.message); |
| 26 | + } |
| 27 | + console.log(response.data.message); |
| 28 | + onMatchCancelled(); // Inform the parent component that the match is cancelled |
| 29 | + }) |
| 30 | + .catch(error => { |
| 31 | + console.error("Error canceling the match: ", error); |
| 32 | + setButtonEnabled(true); |
| 33 | + }) |
| 34 | + }; |
| 35 | + |
| 36 | + if (isOpen) { |
| 37 | + return ( |
| 38 | + <div className="load-overlay"> |
| 39 | + <div className="load-popup"> |
| 40 | + <h3>Finding you a match...</h3> |
| 41 | + <div className="spinner"> |
| 42 | + <Oval className="spinner"/> |
| 43 | + </div> |
| 44 | + <div> |
| 45 | + <button onClick={handleCancelMatching} disabled={!buttonEnabled} className="cancel-match">Cancel Matching</button> |
| 46 | + </div> |
| 47 | + </div> |
| 48 | + </div> |
| 49 | + ); |
| 50 | + } |
| 51 | + return null; |
21 | 52 | };
|
0 commit comments