1 | import {useState} from "react";
|
---|
2 |
|
---|
3 | export default function useConnections(map,shapeInfo,setShapeInfo){
|
---|
4 | const [connections,setConnections] = useState([])
|
---|
5 |
|
---|
6 | const addConnection = () => {
|
---|
7 | console.log("TOSKA",shapeInfo)
|
---|
8 | if (!shapeInfo.selectedPin || connections.includes(shapeInfo.selectedPin)) return;
|
---|
9 |
|
---|
10 | setConnections((prevConn) => {
|
---|
11 | const updatedConnections = [...prevConn, shapeInfo.selectedPin];
|
---|
12 |
|
---|
13 |
|
---|
14 | setShapeInfo((prevShapeInfo) => ({
|
---|
15 | ...prevShapeInfo,
|
---|
16 | ["selectedPin"]: "",
|
---|
17 | ["selectedPins"]: updatedConnections,
|
---|
18 | }));
|
---|
19 |
|
---|
20 | console.log(shapeInfo.name,shapeInfo.selectedPin,"TEST")
|
---|
21 |
|
---|
22 | map.drawConnection(shapeInfo.name,shapeInfo.selectedPin);
|
---|
23 | return updatedConnections;
|
---|
24 | });
|
---|
25 | };
|
---|
26 |
|
---|
27 | const removeConnection = (connToRemove) => {
|
---|
28 | setConnections((prevConn) => {
|
---|
29 | const updatedPins = prevConn.filter((pin) => pin !== connToRemove);
|
---|
30 | setShapeInfo((prevFormData) => ({
|
---|
31 | ...prevFormData,
|
---|
32 | selectedPins: updatedPins,
|
---|
33 | }));
|
---|
34 | return updatedPins;
|
---|
35 | });
|
---|
36 | map.removeConnection(shapeInfo.name, connToRemove);
|
---|
37 | };
|
---|
38 |
|
---|
39 |
|
---|
40 | return {
|
---|
41 | connectionState: {connections,setConnections},
|
---|
42 | handlers: {addConnection,removeConnection}
|
---|
43 |
|
---|
44 | }
|
---|
45 | } |
---|