@@ -6,66 +6,98 @@ import 'typeface-montserrat';
6
6
import './styles.scss' ;
7
7
import FindMatchContent from './modalContent/FindMatchContent' ;
8
8
import MatchingInProgressContent from './modalContent/MatchingInProgressContent' ;
9
- import MatchFound from './modalContent/MatchFoundContent' ;
9
+ import MatchFoundContent from './modalContent/MatchFoundContent' ;
10
10
import JoinedMatchContent from './modalContent/JoinedMatchContent' ;
11
11
import MatchNotFoundContent from './modalContent/MatchNotFoundContent' ;
12
12
import MatchCancelledContent from './modalContent/MatchCancelledContent' ;
13
+ import useMatching from '../services/use-matching' ;
13
14
14
15
interface MatchingModalProps {
15
16
isOpen : boolean ;
16
- onClose : ( ) => void ;
17
+ close : ( ) => void ;
17
18
}
18
19
19
- const MatchingModal : React . FC < MatchingModalProps > = ( { isOpen, onClose } ) => {
20
- // TODO: placehoder for now, to be replaced my useContext
21
- const [ matchingState , setMatchingState ] = useState ( 'finding' ) ;
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 ) ;
22
25
23
- // TODO: remove this after testing
24
- useEffect ( ( ) => {
25
- // Uncomment the following lines to test the different matching states
26
- // setMatchingState('finding');
27
- // setMatchingState('matching');
28
- // setMatchingState('found');
29
- // setMatchingState('joined');
30
- // setMatchingState('notFound');
31
- // setMatchingState('cancelled');
32
- } , [ ] ) ;
33
-
34
- // TODO: modify by using matchingState via useContext
35
- const isClosableMatchingState = ( ) => {
36
- return matchingState === 'finding' || matchingState === 'notFound' || matchingState === 'cancelled' ;
37
- } ;
26
+ function close ( ) {
27
+ // clean up matching and closedType State
28
+ if ( matchingState . state === "timeout" ) {
29
+ matchingState . ok ( ) ;
30
+ }
31
+ setClosedType ( "finding" ) ;
32
+ _close ( ) ;
33
+ }
38
34
39
35
const renderModalContent = ( ) => {
40
- switch ( matchingState ) {
41
- case 'finding' :
42
- return < FindMatchContent /> ;
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 cancel = { ( ) => {
51
+ setClosedType ( "cancelled" ) ;
52
+ } } /> ;
53
+ }
43
54
case 'matching' :
44
- return < MatchingInProgressContent /> ;
55
+ return < MatchingInProgressContent
56
+ cancelMatch = { ( timeoutAfter : number ) => {
57
+ setClosedType ( "cancelled" ) ;
58
+ setTimeoutAfter ( timeoutAfter ) ;
59
+ matchingState . cancel ( ) ;
60
+ } }
61
+ timeout = { ( timeoutAfter : number ) => {
62
+ matchingState . timeout ( )
63
+ setTimeoutAfter ( timeoutAfter ) ;
64
+ } }
65
+ /> ;
66
+ case 'cancelling' :
67
+ return < MatchingInProgressContent cancelMatch = { ( ) => { } } timeout = { ( ) => { } } /> ;
68
+ case 'starting' :
69
+ return < FindMatchContent beginMatch = { ( ) => { } } />
45
70
case 'found' :
46
- return < MatchFound /> ;
47
- case 'joined' :
48
- return < JoinedMatchContent /> ;
49
- case 'notFound' :
50
- return < MatchNotFoundContent /> ;
51
- case 'cancelled' :
52
- return < MatchCancelledContent /> ;
71
+ return < MatchFoundContent
72
+ cancel = { ( ) => {
73
+ matchingState . ok ( ) ;
74
+ setClosedType ( "cancelled" ) ;
75
+ } }
76
+ join = { ( ) => {
77
+ matchingState . ok ( ) ;
78
+ setClosedType ( "joined" ) ;
79
+ } }
80
+ name1 = { matchingState . info . myName }
81
+ name2 = { matchingState . info . partnerName }
82
+ />
83
+ case 'timeout' :
84
+ return < MatchNotFoundContent reselect = { matchingState . ok } retry = { ( ) => { } } timedOutIn = { 10 } /> ;
53
85
default :
54
86
throw new Error ( 'Invalid matching state.' ) ;
55
87
}
56
88
} ;
57
89
58
90
return (
59
91
< Modal open = { isOpen }
60
- onCancel = { onClose }
92
+ onCancel = { close }
61
93
footer = { null }
62
94
closable = { false }
63
95
maskClosable = { false }
64
96
className = "modal"
65
97
>
66
98
{ renderModalContent ( ) }
67
- { isClosableMatchingState ( ) && (
68
- < button className = "close-button" onClick = { onClose } > Close</ button >
99
+ { isClosable && (
100
+ < button className = "close-button" onClick = { close } > Close</ button >
69
101
) }
70
102
</ Modal >
71
103
)
0 commit comments