Skip to content

Commit

Permalink
feat: double click to play card
Browse files Browse the repository at this point in the history
  • Loading branch information
daithihearn committed Apr 21, 2024
1 parent 3439a6b commit aca31a3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 57 deletions.
33 changes: 0 additions & 33 deletions src/caches/PlayCardSlice.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/caches/caches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import {
} from "@reduxjs/toolkit"

import { gameSlice } from "./GameSlice"
import { playCardSlice } from "./PlayCardSlice"

const combinedReducer = combineReducers({
game: gameSlice.reducer,
playCard: playCardSlice.reducer,
})

export type RootState = ReturnType<typeof combinedReducer>
Expand Down
37 changes: 25 additions & 12 deletions src/components/Game/MyCards.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
import React, { useCallback, useMemo } from "react"
import React, { useCallback, useEffect, useMemo, useState } from "react"
import {
DragDropContext,
Draggable,
Droppable,
DropResult,
} from "react-beautiful-dnd"
import { EMPTY, Card } from "model/Cards"
import { EMPTY, Card, CardName } from "model/Cards"
import { RoundStatus } from "model/Round"
import { useAppDispatch, useAppSelector } from "caches/hooks"
import {
getCardToPlay,
togglePlayCard,
clearAutoPlay,
} from "caches/PlayCardSlice"
import { CardContent, CardMedia, useTheme } from "@mui/material"
import {
clearSelectedCards,
getCardsFull,
getGameId,
getIamGoer,
getIsMyGo,
getIsRoundCalled,
getRound,
replaceMyCards,
toggleSelect,
toggleUniqueSelect,
} from "caches/GameSlice"
import { useGameActions } from "components/Hooks/useGameActions"

const EMPTY_HAND = [
{ ...EMPTY, selected: false },
Expand All @@ -36,12 +34,15 @@ const EMPTY_HAND = [
const MyCards = () => {
const dispatch = useAppDispatch()
const theme = useTheme()
const { playCard } = useGameActions()

const gameId = useAppSelector(getGameId)
const round = useAppSelector(getRound)
const isRoundCalled = useAppSelector(getIsRoundCalled)
const myCards = useAppSelector(getCardsFull)
const autoPlayCard = useAppSelector(getCardToPlay)
const [autoPlayCard, setAutoPlayCard] = useState<CardName>(CardName.EMPTY)
const iamGoer = useAppSelector(getIamGoer)
const isMyGo = useAppSelector(getIsMyGo)

const cardsSelectable = useMemo(
() =>
Expand All @@ -59,22 +60,34 @@ const MyCards = () => {
[isRoundCalled, iamGoer],
)

useEffect(() => {
if (
autoPlayCard !== CardName.EMPTY &&
round &&
round.status === RoundStatus.PLAYING &&
isMyGo
) {
playCard({ gameId: gameId!, card: autoPlayCard })
setAutoPlayCard(CardName.EMPTY)
}
}, [autoPlayCard, round, gameId, isMyGo])

const handleSelectCard = useCallback(
(card: Card, event: React.MouseEvent<HTMLImageElement, MouseEvent>) => {
if (!cardsSelectable || card.name === EMPTY.name) {
return
}

// If the round status is PLAYING then only allow one card to be selected
if (round && round.status === RoundStatus.PLAYING) {
if (gameId && round && round.status === RoundStatus.PLAYING) {
if (autoPlayCard === card.name) {
dispatch(clearAutoPlay())
setAutoPlayCard(CardName.EMPTY)
dispatch(clearSelectedCards())
} else if (event.detail === 2) {
dispatch(togglePlayCard(card))
setAutoPlayCard(card.name)
} else {
dispatch(toggleUniqueSelect(card))
dispatch(clearAutoPlay())
setAutoPlayCard(CardName.EMPTY)
}
} else {
dispatch(toggleSelect(card))
Expand Down
23 changes: 14 additions & 9 deletions src/components/Hooks/useGameActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ export const useGameActions = () => {
)
return response.data
},
onSuccess: data => {
onSuccess: (data: GameStateResponse) => {
queryClient.setQueryData(["gameState"], data)
dispatch(updateGame(data))
},
onError: e => enqueueSnackbar(parseError(e), { variant: "error" }),
onError: (e: any) =>
enqueueSnackbar(parseError(e), { variant: "error" }),
})

const buyCards = useMutation({
Expand All @@ -59,11 +60,12 @@ export const useGameActions = () => {
)
return response.data
},
onSuccess: data => {
onSuccess: (data: GameStateResponse) => {
queryClient.setQueryData(["gameState"], data)
dispatch(updateGame(data))
},
onError: e => enqueueSnackbar(parseError(e), { variant: "error" }),
onError: (e: any) =>
enqueueSnackbar(parseError(e), { variant: "error" }),
})

const selectSuit = useMutation({
Expand All @@ -89,11 +91,12 @@ export const useGameActions = () => {
)
return response.data
},
onSuccess: data => {
onSuccess: (data: GameStateResponse) => {
queryClient.setQueryData(["gameState"], data)
dispatch(updateGame(data))
},
onError: e => enqueueSnackbar(parseError(e), { variant: "error" }),
onError: (e: any) =>
enqueueSnackbar(parseError(e), { variant: "error" }),
})

const playCard = useMutation({
Expand All @@ -114,11 +117,12 @@ export const useGameActions = () => {
)
return response.data
},
onSuccess: data => {
onSuccess: (data: GameStateResponse) => {
queryClient.setQueryData(["gameState"], data)
dispatch(updateGame(data))
},
onError: e => enqueueSnackbar(parseError(e), { variant: "error" }),
onError: (e: any) =>
enqueueSnackbar(parseError(e), { variant: "error" }),
})

const deleteGame = useMutation({
Expand All @@ -134,7 +138,8 @@ export const useGameActions = () => {
queryClient.invalidateQueries({
queryKey: ["myGames", "gameState"],
}),
onError: e => enqueueSnackbar(parseError(e), { variant: "error" }),
onError: (e: any) =>
enqueueSnackbar(parseError(e), { variant: "error" }),
})

return {
Expand Down
2 changes: 1 addition & 1 deletion src/components/Hooks/useGameState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const useGameState = (gameId?: string) => {
}
return res.data
},
// Refetch the data every 2 seconds
// Refetch the data every 5 seconds
refetchInterval: 5_000,
enabled: !!accessToken && !!gameId,
})
Expand Down

0 comments on commit aca31a3

Please sign in to comment.