@@ -6,16 +6,27 @@ import { checkCorrect, getRandomItem, preloadImage } from "../lib/items";
6
6
import NextItemList from "./next-item-list" ;
7
7
import PlayedItemList from "./played-item-list" ;
8
8
import styles from "../styles/board.module.scss" ;
9
+ import Hearts from "./hearts" ;
10
+ import GameOver from "./game-over" ;
9
11
10
12
interface Props {
13
+ resetGame : ( ) => void ;
11
14
state : GameState ;
12
15
setState : ( state : GameState ) => void ;
13
16
}
14
17
15
18
export default function Board ( props : Props ) {
16
- const { state, setState } = props ;
19
+ const { resetGame, state, setState } = props ;
20
+
21
+ const [ isDragging , setIsDragging ] = React . useState ( false ) ;
22
+
23
+ async function onDragStart ( ) {
24
+ setIsDragging ( true ) ;
25
+ }
17
26
18
27
async function onDragEnd ( result : DropResult ) {
28
+ setIsDragging ( false ) ;
29
+
19
30
const { source, destination } = result ;
20
31
21
32
if (
@@ -52,6 +63,7 @@ export default function Board(props: Props) {
52
63
next : newNext ,
53
64
nextButOne : newNextButOne ,
54
65
played : newPlayed ,
66
+ lives : correct ? state . lives : state . lives - 1 ,
55
67
badlyPlaced : correct
56
68
? null
57
69
: {
@@ -93,9 +105,14 @@ export default function Board(props: Props) {
93
105
94
106
console . log ( state ) ;
95
107
108
+ const score = React . useMemo ( ( ) => {
109
+ return state . played . filter ( ( item ) => item . played . correct ) . length - 1 ;
110
+ } , [ state . played ] ) ;
111
+
96
112
return (
97
113
< DragDropContext
98
114
onDragEnd = { onDragEnd }
115
+ onDragStart = { onDragStart }
99
116
onDragUpdate = { ( ) => {
100
117
setTimeout ( ( ) => {
101
118
// debugger;
@@ -105,13 +122,19 @@ export default function Board(props: Props) {
105
122
>
106
123
< div className = { styles . wrapper } >
107
124
< div className = { styles . top } >
108
- < NextItemList next = { state . next } />
125
+ < Hearts lives = { state . lives } />
126
+ { state . lives > 0 ? (
127
+ < NextItemList next = { state . next } />
128
+ ) : (
129
+ < GameOver resetGame = { resetGame } score = { score } />
130
+ ) }
109
131
</ div >
110
132
< div id = "bottom" className = { styles . bottom } >
111
133
< PlayedItemList
112
134
badlyPlacedIndex = {
113
135
state . badlyPlaced === null ? null : state . badlyPlaced . index
114
136
}
137
+ isDragging = { isDragging }
115
138
items = { state . played }
116
139
/>
117
140
</ div >
0 commit comments