Skip to content

Commit de0d9f6

Browse files
authored
Merge pull request #97 from daithihearn/separate-dummy
Separating the dummy and the normal cards
2 parents eb2760a + 71bcba9 commit de0d9f6

File tree

1 file changed

+89
-3
lines changed

1 file changed

+89
-3
lines changed

src/components/Game/MyCards.tsx

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ import { BLANK_CARD, SelectableCard } from "../../model/Cards"
1010

1111
import GameService from "../../services/GameService"
1212
import { RoundStatus } from "../../model/Round"
13-
import { getGameId, getIsMyGo, getRound } from "../../caches/GameSlice"
13+
import {
14+
getGameId,
15+
getIamGoer,
16+
getIsMyGo,
17+
getIsRoundCalled,
18+
getRound,
19+
} from "../../caches/GameSlice"
1420
import { useAppDispatch, useAppSelector } from "../../caches/hooks"
1521
import { useSnackbar } from "notistack"
1622
import {
@@ -37,8 +43,10 @@ const MyCards: React.FC = () => {
3743
const dispatch = useAppDispatch()
3844
const gameId = useAppSelector(getGameId)
3945
const round = useAppSelector(getRound)
46+
const isRoundCalled = useAppSelector(getIsRoundCalled)
4047
const myCards = useAppSelector(getMyCards)
4148
const autoPlayCard = useAppSelector(getAutoPlayCard)
49+
const iamGoer = useAppSelector(getIamGoer)
4250
const isMyGo = useAppSelector(getIsMyGo)
4351

4452
const { enqueueSnackbar } = useSnackbar()
@@ -71,6 +79,11 @@ const MyCards: React.FC = () => {
7179
[round],
7280
)
7381

82+
const showDummy = useMemo(
83+
() => isRoundCalled && iamGoer,
84+
[isRoundCalled, iamGoer],
85+
)
86+
7487
const handleSelectCard = useCallback(
7588
(card: SelectableCard) => {
7689
if (!cardsSelectable || card.name === BLANK_CARD.name) {
@@ -117,6 +130,24 @@ const MyCards: React.FC = () => {
117130
[myCards],
118131
)
119132

133+
const handleOnDragEndDummy = useCallback(
134+
(result: DropResult) => {
135+
if (!result.destination) return
136+
137+
const updatedCards = myCards.map(c => {
138+
return { ...c }
139+
})
140+
const [reorderedItem] = updatedCards.splice(
141+
result.source.index + 5,
142+
1,
143+
)
144+
updatedCards.splice(result.destination.index + 5, 0, reorderedItem)
145+
146+
dispatch(replaceMyCards(updatedCards))
147+
},
148+
[myCards],
149+
)
150+
120151
const getStyleForCard = useCallback(
121152
(card: SelectableCard) => {
122153
let classes = "thumbnail_size"
@@ -148,14 +179,14 @@ const MyCards: React.FC = () => {
148179
<div>
149180
<CardBody className="cardArea">
150181
<DragDropContext onDragEnd={handleOnDragEnd}>
151-
<Droppable droppableId="characters" direction="horizontal">
182+
<Droppable droppableId="myCards" direction="horizontal">
152183
{provided => (
153184
<div
154185
className="characters myCards"
155186
style={{ display: "inline-flex" }}
156187
{...provided.droppableProps}
157188
ref={provided.innerRef}>
158-
{myCards.map((card, index) => {
189+
{myCards.slice(0, 5).map((card, index) => {
159190
const draggableId = `${card.name}${
160191
card.name === BLANK_CARD.name
161192
? index
@@ -198,6 +229,61 @@ const MyCards: React.FC = () => {
198229
</DragDropContext>
199230
</CardBody>
200231

232+
{showDummy && (
233+
<CardBody className="cardArea">
234+
<DragDropContext onDragEnd={handleOnDragEndDummy}>
235+
<Droppable droppableId="dummy" direction="horizontal">
236+
{provided => (
237+
<div
238+
className="characters myCards"
239+
style={{ display: "inline-flex" }}
240+
{...provided.droppableProps}
241+
ref={provided.innerRef}>
242+
{myCards.slice(5, 10).map((card, index) => {
243+
const draggableId = `${card.name}${
244+
card.name === BLANK_CARD.name
245+
? index
246+
: ""
247+
}`
248+
return (
249+
<Draggable
250+
key={draggableId}
251+
draggableId={draggableId}
252+
index={index}
253+
isDragDisabled={
254+
card.name ===
255+
BLANK_CARD.name
256+
}>
257+
{provided => (
258+
<div
259+
ref={provided.innerRef}
260+
{...provided.draggableProps}
261+
{...provided.dragHandleProps}>
262+
<CardImg
263+
alt={card.name}
264+
onClick={() =>
265+
handleSelectCard(
266+
card,
267+
)
268+
}
269+
src={`/cards/thumbnails/${card.name}.png`}
270+
className={getStyleForCard(
271+
card,
272+
)}
273+
/>
274+
</div>
275+
)}
276+
</Draggable>
277+
)
278+
})}
279+
{provided.placeholder}
280+
</div>
281+
)}
282+
</Droppable>
283+
</DragDropContext>
284+
</CardBody>
285+
)}
286+
201287
{round?.status === RoundStatus.PLAYING ? (
202288
<CardBody className="buttonArea">
203289
<ButtonGroup size="lg">

0 commit comments

Comments
 (0)