|
1 | | -import React, { useState, useEffect } from 'react'; |
2 | | -import { |
3 | | - Modal, |
4 | | - } from 'antd'; |
5 | | -import 'typeface-montserrat'; |
6 | | -import './styles.scss'; |
7 | | -import FindMatchContent from './modalContent/FindMatchContent'; |
8 | | -import MatchingInProgressContent from './modalContent/MatchingInProgressContent'; |
9 | | -import MatchFoundContent from './modalContent/MatchFoundContent'; |
10 | | -import JoinedMatchContent from './modalContent/JoinedMatchContent'; |
11 | | -import MatchNotFoundContent from './modalContent/MatchNotFoundContent'; |
12 | | -import MatchCancelledContent from './modalContent/MatchCancelledContent'; |
13 | | -import useMatching from '../services/use-matching'; |
| 1 | +import React, { useState, useEffect } from "react"; |
| 2 | +import { Modal } from "antd"; |
| 3 | +import "typeface-montserrat"; |
| 4 | +import "./styles.scss"; |
| 5 | +import FindMatchContent from "./modalContent/FindMatchContent"; |
| 6 | +import MatchingInProgressContent from "./modalContent/MatchingInProgressContent"; |
| 7 | +import MatchFoundContent from "./modalContent/MatchFoundContent"; |
| 8 | +import JoinedMatchContent from "./modalContent/JoinedMatchContent"; |
| 9 | +import MatchNotFoundContent from "./modalContent/MatchNotFoundContent"; |
| 10 | +import MatchCancelledContent from "./modalContent/MatchCancelledContent"; |
| 11 | +import useMatching from "../services/use-matching"; |
| 12 | +import { useRouter } from "next/navigation"; |
14 | 13 |
|
15 | 14 | interface MatchingModalProps { |
16 | | - isOpen: boolean; |
17 | | - close: () => void; |
| 15 | + isOpen: boolean; |
| 16 | + close: () => void; |
18 | 17 | } |
19 | 18 |
|
20 | | -const MatchingModal: React.FC<MatchingModalProps> = ({ isOpen, close: _close }) => { |
21 | | - const matchingState = useMatching(); |
22 | | - const [closedType, setClosedType] = useState<"finding" | "cancelled" | "joined">("finding"); |
23 | | - const [timeoutAfter, setTimeoutAfter] = useState<number>(9999); |
24 | | - const isClosable = ["timeout", "closed"].includes(matchingState.state); |
| 19 | +const MatchingModal: React.FC<MatchingModalProps> = ({ |
| 20 | + isOpen, |
| 21 | + close: _close, |
| 22 | +}) => { |
| 23 | + const router = useRouter(); |
| 24 | + const matchingState = useMatching(); |
| 25 | + const [closedType, setClosedType] = useState< |
| 26 | + "finding" | "cancelled" | "joined" |
| 27 | + >("finding"); |
| 28 | + const [timeoutAfter, setTimeoutAfter] = useState<number>(9999); |
| 29 | + const isClosable = ["timeout", "closed"].includes(matchingState.state); |
25 | 30 |
|
26 | | - function close() { |
27 | | - // clean up matching and closedType State |
28 | | - if (matchingState.state === "timeout") { |
29 | | - matchingState.ok(); |
30 | | - } |
31 | | - setClosedType("finding"); |
32 | | - _close(); |
| 31 | + function close() { |
| 32 | + // clean up matching and closedType State |
| 33 | + if (matchingState.state === "timeout") { |
| 34 | + matchingState.ok(); |
33 | 35 | } |
| 36 | + setClosedType("finding"); |
| 37 | + _close(); |
| 38 | + } |
34 | 39 |
|
35 | | - const renderModalContent = () => { |
36 | | - switch (matchingState.state) { |
37 | | - case 'closed': |
38 | | - switch (closedType) { |
39 | | - case "finding": |
40 | | - return <FindMatchContent beginMatch={matchingState.start}/>; |
41 | | - case "cancelled": |
42 | | - return <MatchCancelledContent |
43 | | - reselect={() => { |
44 | | - setClosedType("finding"); |
45 | | - }} |
46 | | - retry={() => {}} |
47 | | - canceledIn={timeoutAfter} |
48 | | - />; |
49 | | - case "joined": |
50 | | - return <JoinedMatchContent |
51 | | - cancel={() => { |
52 | | - setClosedType("cancelled"); |
53 | | - }} |
54 | | - name1={matchingState.info?.myName || ""} |
55 | | - name2={matchingState.info?.partnerName || ""} |
56 | | - />; |
57 | | - } |
58 | | - case 'matching': |
59 | | - return <MatchingInProgressContent |
60 | | - cancelMatch={(timeoutAfter: number) => { |
61 | | - setClosedType("cancelled"); |
62 | | - setTimeoutAfter(timeoutAfter); |
63 | | - matchingState.cancel(); |
64 | | - }} |
65 | | - timeout={(timeoutAfter: number) => { |
66 | | - matchingState.timeout() |
67 | | - setTimeoutAfter(timeoutAfter); |
68 | | - }} |
69 | | - />; |
70 | | - case 'cancelling': |
71 | | - return <MatchingInProgressContent cancelMatch={() => {}} timeout={() => {}}/>; |
72 | | - case 'starting': |
73 | | - return <FindMatchContent beginMatch={() => {}}/> |
74 | | - case 'found': |
75 | | - return <MatchFoundContent |
76 | | - cancel={() => { |
77 | | - matchingState.ok(); |
78 | | - setClosedType("cancelled"); |
79 | | - }} |
80 | | - join={() => { |
81 | | - matchingState.ok(); |
82 | | - setClosedType("joined"); |
83 | | - }} |
84 | | - name1={matchingState.info.myName} |
85 | | - name2={matchingState.info.partnerName} |
86 | | - /> |
87 | | - case 'timeout': |
88 | | - return <MatchNotFoundContent reselect={matchingState.ok} retry={() => {}} timedOutIn={10}/>; |
89 | | - default: |
90 | | - throw new Error('Invalid matching state.'); |
| 40 | + const renderModalContent = () => { |
| 41 | + switch (matchingState.state) { |
| 42 | + case "closed": |
| 43 | + switch (closedType) { |
| 44 | + case "finding": |
| 45 | + return <FindMatchContent beginMatch={matchingState.start} />; |
| 46 | + case "cancelled": |
| 47 | + return ( |
| 48 | + <MatchCancelledContent |
| 49 | + reselect={() => { |
| 50 | + setClosedType("finding"); |
| 51 | + }} |
| 52 | + retry={() => {}} |
| 53 | + canceledIn={timeoutAfter} |
| 54 | + /> |
| 55 | + ); |
| 56 | + case "joined": |
| 57 | + return ( |
| 58 | + <JoinedMatchContent |
| 59 | + cancel={() => { |
| 60 | + setClosedType("cancelled"); |
| 61 | + }} |
| 62 | + name1={matchingState.info?.myName || ""} |
| 63 | + name2={matchingState.info?.partnerName || ""} |
| 64 | + /> |
| 65 | + ); |
91 | 66 | } |
92 | | - }; |
| 67 | + case "matching": |
| 68 | + return ( |
| 69 | + <MatchingInProgressContent |
| 70 | + cancelMatch={(timeoutAfter: number) => { |
| 71 | + setClosedType("cancelled"); |
| 72 | + setTimeoutAfter(timeoutAfter); |
| 73 | + matchingState.cancel(); |
| 74 | + }} |
| 75 | + timeout={(timeoutAfter: number) => { |
| 76 | + matchingState.timeout(); |
| 77 | + setTimeoutAfter(timeoutAfter); |
| 78 | + }} |
| 79 | + /> |
| 80 | + ); |
| 81 | + case "cancelling": |
| 82 | + return ( |
| 83 | + <MatchingInProgressContent |
| 84 | + cancelMatch={() => {}} |
| 85 | + timeout={() => {}} |
| 86 | + /> |
| 87 | + ); |
| 88 | + case "starting": |
| 89 | + return <FindMatchContent beginMatch={() => {}} />; |
| 90 | + case "found": |
| 91 | + return ( |
| 92 | + <MatchFoundContent |
| 93 | + cancel={() => { |
| 94 | + matchingState.ok(); |
| 95 | + setClosedType("cancelled"); |
| 96 | + }} |
| 97 | + join={() => { |
| 98 | + matchingState.ok(); |
| 99 | + setClosedType("joined"); |
| 100 | + localStorage.setItem("user", matchingState.info.myName); |
| 101 | + localStorage.setItem( |
| 102 | + "matchedUser", |
| 103 | + matchingState.info.partnerName |
| 104 | + ); |
| 105 | + localStorage.setItem("collabId", matchingState.info.matchId); |
| 106 | + localStorage.setItem("docRefId", "CV480GUbjk15eWOrWrA0"); // TODO: remove temporary placeholder for quesiton testing |
| 107 | + // localStorage.setItem("docRefId", matchingState.info.docRefId); // TODO: Update the response from backend when match_found |
93 | 108 |
|
94 | | - return ( |
95 | | - <Modal open={isOpen} |
96 | | - onCancel={close} |
97 | | - footer={null} |
98 | | - closable={false} |
99 | | - maskClosable={false} |
100 | | - className="modal" |
101 | | - > |
102 | | - {renderModalContent()} |
103 | | - {isClosable && ( |
104 | | - <button className="close-button" onClick={close}>Close</button> |
105 | | - )} |
106 | | - </Modal> |
107 | | - ) |
108 | | -} |
| 109 | + // Redirect to collaboration page |
| 110 | + router.push(`/collaboration/${matchingState.info.matchId}`); |
| 111 | + }} |
| 112 | + name1={matchingState.info.myName} |
| 113 | + name2={matchingState.info.partnerName} |
| 114 | + /> |
| 115 | + ); |
| 116 | + case "timeout": |
| 117 | + return ( |
| 118 | + <MatchNotFoundContent |
| 119 | + reselect={matchingState.ok} |
| 120 | + retry={() => {}} |
| 121 | + timedOutIn={10} |
| 122 | + /> |
| 123 | + ); |
| 124 | + default: |
| 125 | + throw new Error("Invalid matching state."); |
| 126 | + } |
| 127 | + }; |
| 128 | + |
| 129 | + return ( |
| 130 | + <Modal |
| 131 | + open={isOpen} |
| 132 | + onCancel={close} |
| 133 | + footer={null} |
| 134 | + closable={false} |
| 135 | + maskClosable={false} |
| 136 | + className="modal" |
| 137 | + > |
| 138 | + {renderModalContent()} |
| 139 | + {isClosable && ( |
| 140 | + <button className="close-button" onClick={close}> |
| 141 | + Close |
| 142 | + </button> |
| 143 | + )} |
| 144 | + </Modal> |
| 145 | + ); |
| 146 | +}; |
109 | 147 |
|
110 | 148 | export default MatchingModal; |
0 commit comments