1
- import React , { useCallback , useMemo } from "react"
1
+ import React , { useCallback , useEffect , useMemo , useState } from "react"
2
2
import {
3
3
DragDropContext ,
4
4
Draggable ,
5
5
Droppable ,
6
6
DropResult ,
7
7
} from "react-beautiful-dnd"
8
- import { EMPTY , Card } from "model/Cards"
8
+ import { EMPTY , Card , CardName } from "model/Cards"
9
9
import { RoundStatus } from "model/Round"
10
10
import { useAppDispatch , useAppSelector } from "caches/hooks"
11
- import {
12
- getCardToPlay ,
13
- togglePlayCard ,
14
- clearAutoPlay ,
15
- } from "caches/PlayCardSlice"
16
11
import { CardContent , CardMedia , useTheme } from "@mui/material"
17
12
import {
18
13
clearSelectedCards ,
19
14
getCardsFull ,
15
+ getGameId ,
20
16
getIamGoer ,
17
+ getIsMyGo ,
21
18
getIsRoundCalled ,
22
19
getRound ,
23
20
replaceMyCards ,
24
21
toggleSelect ,
25
22
toggleUniqueSelect ,
26
23
} from "caches/GameSlice"
24
+ import { useGameActions } from "components/Hooks/useGameActions"
27
25
28
26
const EMPTY_HAND = [
29
27
{ ...EMPTY , selected : false } ,
@@ -36,12 +34,15 @@ const EMPTY_HAND = [
36
34
const MyCards = ( ) => {
37
35
const dispatch = useAppDispatch ( )
38
36
const theme = useTheme ( )
37
+ const { playCard } = useGameActions ( )
39
38
39
+ const gameId = useAppSelector ( getGameId )
40
40
const round = useAppSelector ( getRound )
41
41
const isRoundCalled = useAppSelector ( getIsRoundCalled )
42
42
const myCards = useAppSelector ( getCardsFull )
43
- const autoPlayCard = useAppSelector ( getCardToPlay )
43
+ const [ autoPlayCard , setAutoPlayCard ] = useState < CardName > ( CardName . EMPTY )
44
44
const iamGoer = useAppSelector ( getIamGoer )
45
+ const isMyGo = useAppSelector ( getIsMyGo )
45
46
46
47
const cardsSelectable = useMemo (
47
48
( ) =>
@@ -59,22 +60,34 @@ const MyCards = () => {
59
60
[ isRoundCalled , iamGoer ] ,
60
61
)
61
62
63
+ useEffect ( ( ) => {
64
+ if (
65
+ autoPlayCard !== CardName . EMPTY &&
66
+ round &&
67
+ round . status === RoundStatus . PLAYING &&
68
+ isMyGo
69
+ ) {
70
+ playCard ( { gameId : gameId ! , card : autoPlayCard } )
71
+ setAutoPlayCard ( CardName . EMPTY )
72
+ }
73
+ } , [ autoPlayCard , round , gameId , isMyGo ] )
74
+
62
75
const handleSelectCard = useCallback (
63
76
( card : Card , event : React . MouseEvent < HTMLImageElement , MouseEvent > ) => {
64
77
if ( ! cardsSelectable || card . name === EMPTY . name ) {
65
78
return
66
79
}
67
80
68
81
// If the round status is PLAYING then only allow one card to be selected
69
- if ( round && round . status === RoundStatus . PLAYING ) {
82
+ if ( gameId && round && round . status === RoundStatus . PLAYING ) {
70
83
if ( autoPlayCard === card . name ) {
71
- dispatch ( clearAutoPlay ( ) )
84
+ setAutoPlayCard ( CardName . EMPTY )
72
85
dispatch ( clearSelectedCards ( ) )
73
86
} else if ( event . detail === 2 ) {
74
- dispatch ( togglePlayCard ( card ) )
87
+ setAutoPlayCard ( card . name )
75
88
} else {
76
89
dispatch ( toggleUniqueSelect ( card ) )
77
- dispatch ( clearAutoPlay ( ) )
90
+ setAutoPlayCard ( CardName . EMPTY )
78
91
}
79
92
} else {
80
93
dispatch ( toggleSelect ( card ) )
0 commit comments