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