11import React , { useState , useEffect , useRef } from 'react'
22import PropTypes from 'prop-types'
33import axios from '../../../api/axios.js'
4- import io from 'socket.io-client'
54import 'leaflet/dist/leaflet.css'
65import ShipmentInfo from '../ShipmentInfo/ShipmentInfo.js'
76import { GoogleMap , useJsApiLoader , Marker , DirectionsRenderer } from '@react-google-maps/api'
87import { VITE_APP_GOOGLE_MAP , VITE_SOCKET_URL } from '../../../config.js'
98
109const GOOGLE_MAPS_LIBRARIES = [ 'places' ]
10+
1111const MapCenterUpdater = ( { lat, lng, map } ) => {
1212 useEffect ( ( ) => {
1313 if ( map && lat && lng ) {
@@ -32,40 +32,57 @@ const TrackingForm = () => {
3232 const [ socket , setSocket ] = useState ( null )
3333 const markerRef = useRef ( null )
3434 const [ directions , setDirections ] = useState ( null )
35- const isSocketInitialized = useRef ( false )
3635 const { isLoaded } = useJsApiLoader ( {
3736 googleMapsApiKey : VITE_APP_GOOGLE_MAP ,
3837 libraries : GOOGLE_MAPS_LIBRARIES ,
3938 } )
4039
4140 useEffect ( ( ) => {
42- const newSocket = io ( VITE_SOCKET_URL , { autoConnect : false } )
43- setSocket ( newSocket )
41+ const ws = new WebSocket ( VITE_SOCKET_URL )
42+ setSocket ( ws )
43+
4444 return ( ) => {
45- newSocket . disconnect ( )
45+ ws . close ( )
4646 }
4747 } , [ ] )
4848
4949 useEffect ( ( ) => {
50- if ( ! trackingNumber || ! socket || isSocketInitialized . current ) return
51-
52- socket . connect ( )
53- socket . emit ( 'joinTracking' , trackingNumber )
54- isSocketInitialized . current = true
55-
56- socket . on ( 'shipmentLocationUpdate' , ( updatedShipment ) => {
57- setShipmentData ( ( prevData ) => ( {
58- ...prevData ,
59- latitude : updatedShipment . latitude ,
60- longitude : updatedShipment . longitude ,
61- updated_at : new Date ( updatedShipment . updated_at ) ,
62- } ) )
50+ if ( ! socket || socket . readyState !== WebSocket . OPEN ) return
51+
52+ const joinTracking = ( ) => {
53+ const joinMessage = JSON . stringify ( {
54+ action : 'joinTracking' ,
55+ trackingNumber,
56+ } )
57+ socket . send ( joinMessage )
58+ }
59+
60+ socket . addEventListener ( 'open' , joinTracking )
61+
62+ socket . addEventListener ( 'message' , ( event ) => {
63+ const message = JSON . parse ( event . data )
64+
65+ if ( message . type === 'shipmentLocationUpdate' ) {
66+ const updatedShipment = message . data
67+ setShipmentData ( ( prevData ) => ( {
68+ ...prevData ,
69+ latitude : updatedShipment . latitude ,
70+ longitude : updatedShipment . longitude ,
71+ updated_at : new Date ( updatedShipment . updated_at ) ,
72+ } ) )
73+ }
6374 } )
6475
6576 return ( ) => {
66- socket . emit ( 'leaveTracking' , trackingNumber )
67- socket . off ( 'shipmentLocationUpdate' )
68- isSocketInitialized . current = false
77+ if ( socket . readyState === WebSocket . OPEN ) {
78+ const leaveMessage = JSON . stringify ( {
79+ action : 'leaveTracking' ,
80+ trackingNumber,
81+ } )
82+ socket . send ( leaveMessage )
83+ }
84+ socket . removeEventListener ( 'open' , joinTracking )
85+ socket . removeEventListener ( 'message' , ( ) => { } )
6986 }
7087 } , [ trackingNumber , socket ] )
7188
0 commit comments