@@ -3,25 +3,36 @@ import { usePathFinding } from "../hooks/usePathFinding";
3
3
import { MAX_COLS , MAX_ROWS } from "../utils/constants" ;
4
4
import { Tile } from "./Tile" ;
5
5
import { MutableRefObject , useState } from "react" ;
6
- import { checkIfStartOrEnd , createNewGrid , resetCurrentStart , setNewStartTile } from "../utils/helpers" ;
6
+ import {
7
+ checkIfStartOrEnd ,
8
+ createNewGrid ,
9
+ resetCurrentEnd ,
10
+ resetCurrentStart ,
11
+ setNewEndTile ,
12
+ setNewStartTile ,
13
+ } from "../utils/helpers" ;
7
14
import { useTile } from "../hooks/useTile" ;
8
15
9
16
export function Grid ( {
10
17
isVisualizationRunningRef,
11
18
isChangeStartSelectedRef,
19
+ isChangeEndSelectedRef,
12
20
} : {
13
21
isVisualizationRunningRef : MutableRefObject < boolean > ;
14
22
isChangeStartSelectedRef : MutableRefObject < boolean > ;
23
+ isChangeEndSelectedRef : MutableRefObject < boolean > ;
15
24
} ) {
16
25
const { grid, setGrid } = usePathFinding ( ) ;
17
26
const [ isMouseDown , setIsMouseDown ] = useState < boolean > ( false ) ;
18
- const { startTile, setStartTile} = useTile ( ) ;
27
+ const { startTile, setStartTile, endTile, setEndTile } = useTile ( ) ;
28
+
19
29
20
30
const handleMouseDown = ( row : number , col : number ) => {
21
31
if (
22
32
isVisualizationRunningRef . current ||
23
33
checkIfStartOrEnd ( row , col ) ||
24
- isChangeStartSelectedRef . current
34
+ isChangeStartSelectedRef . current ||
35
+ isChangeEndSelectedRef . current
25
36
) {
26
37
return ;
27
38
}
@@ -35,7 +46,8 @@ export function Grid({
35
46
if (
36
47
isVisualizationRunningRef . current ||
37
48
checkIfStartOrEnd ( row , col ) ||
38
- isChangeStartSelectedRef . current
49
+ isChangeStartSelectedRef . current ||
50
+ isChangeEndSelectedRef . current
39
51
) {
40
52
return ;
41
53
}
@@ -47,7 +59,8 @@ export function Grid({
47
59
if (
48
60
isVisualizationRunningRef . current ||
49
61
checkIfStartOrEnd ( row , col ) ||
50
- isChangeStartSelectedRef . current
62
+ isChangeStartSelectedRef . current ||
63
+ isChangeEndSelectedRef . current
51
64
) {
52
65
return ;
53
66
}
@@ -59,14 +72,19 @@ export function Grid({
59
72
} ;
60
73
61
74
const handleMouseClick = ( row : number , col : number ) => {
62
- if ( ! isChangeStartSelectedRef . current ) {
63
- return ;
75
+ if ( isChangeStartSelectedRef . current ) {
76
+ const newSelectedStartTile = setNewStartTile ( grid , row , col ) ;
77
+ setGrid ( newSelectedStartTile ) ;
78
+ setGrid ( resetCurrentStart ( grid , startTile , row , col , setStartTile ) ) ;
79
+ isChangeStartSelectedRef . current = false ;
80
+ //setIsDisabled(false);
81
+ }
82
+ if ( isChangeEndSelectedRef . current ) {
83
+ setGrid ( setNewEndTile ( grid , row , col ) ) ;
84
+ setGrid ( resetCurrentEnd ( grid , endTile , row , col , setEndTile ) ) ;
85
+ isChangeEndSelectedRef . current = false ;
86
+ // setIsDisabled(false);
64
87
}
65
-
66
- const newSelectedStartTile = setNewStartTile ( grid , row , col ) ;
67
- setGrid ( newSelectedStartTile ) ;
68
- setGrid ( resetCurrentStart ( grid , startTile , row , col , setStartTile ) )
69
- isChangeStartSelectedRef . current = false
70
88
} ;
71
89
72
90
return (
@@ -102,7 +120,7 @@ export function Grid({
102
120
handleMouseDown = { ( ) => handleMouseDown ( row , col ) }
103
121
handleMouseUp = { ( ) => handleMouseUp ( row , col ) }
104
122
handleMouseEnter = { ( ) => handleMouseEnter ( row , col ) }
105
- handleMouseClick = { ( ) => handleMouseClick ( row , col ) }
123
+ handleMouseClick = { ( ) => handleMouseClick ( row , col ) }
106
124
/>
107
125
) ;
108
126
} ) }
0 commit comments