Skip to content

Commit b575e50

Browse files
albsbzalbsbz
andauthored
th-369: Map render on reload (#374)
* th-369: * map render on reload * th-369: * types --------- Co-authored-by: albsbz <[email protected]>
1 parent cbd9c08 commit b575e50

File tree

4 files changed

+38
-36
lines changed

4 files changed

+38
-36
lines changed

frontend/src/libs/hooks/use-app-map/use-app-map.hook.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,25 @@ const useAppMap = ({
3131
const mapService = useRef<MapService | null>(null);
3232

3333
const dispatch = useAppDispatch();
34+
useEffect(() => {
35+
MapConnector.dropMap();
36+
}, []);
3437

3538
useEffect(() => {
3639
const configMap = async (): Promise<void> => {
3740
await MapConnector.getInstance();
38-
window.addEventListener('load', function () {
39-
mapService.current = new MapConnector().getMapService({
40-
mapElement: mapReference.current,
41-
center: center ?? DEFAULT_CENTER,
42-
zoom,
43-
});
41+
mapService.current = new MapConnector().getMapService({
42+
mapElement: mapReference.current,
43+
center: center ?? DEFAULT_CENTER,
44+
zoom,
45+
});
4446

45-
if (center && destination) {
46-
mapService.current.removeMarkers();
47-
mapService.current.addMarker(destination);
47+
if (center && destination) {
48+
mapService.current.removeMarkers();
49+
mapService.current.addMarker(destination);
4850

49-
void mapService.current.calculateRouteAndTime(center, destination);
50-
}
51-
});
51+
void mapService.current.calculateRouteAndTime(center, destination);
52+
}
5253
};
5354
void configMap();
5455
}, [center, destination, mapReference, zoom]);

frontend/src/libs/packages/map/map-connector.package.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class MapConnector implements IMapConnector {
4646
}
4747

4848
public getMapService(parameters: MapServiceParameters): MapService {
49-
const setMap = (map: google.maps.Map): void => {
49+
const setMap = (map: google.maps.Map | null): void => {
5050
MapConnector.map = map;
5151
};
5252

@@ -58,6 +58,11 @@ class MapConnector implements IMapConnector {
5858
setMap,
5959
});
6060
}
61+
62+
public static dropMap(): void {
63+
this.map = null;
64+
this.markers = [];
65+
}
6166
}
6267

6368
export { MapConnector };

frontend/src/pages/homepage/homepage.tsx

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
1-
import { type Libraries, LoadScript } from '@react-google-maps/api';
2-
3-
import { Map } from '~/libs/components/map/map.js';
41
import {
2+
useAppMap,
53
useAppSelector,
64
useEffect,
75
useHomePageSocketService,
8-
useMemo,
6+
useRef,
97
} from '~/libs/hooks/hooks.js';
10-
import { config } from '~/libs/packages/config/config.js';
118
import { selectTrucks } from '~/slices/trucks/selectors.js';
129

1310
import { TruckList } from './components/truck-list/truck-list.js';
14-
import { getTruckLocations } from './libs/helpers/get-truck-locations.helper.js';
1511
import styles from './styles.module.scss';
1612

17-
const libraries: Libraries = ['places'];
18-
1913
const HomePage: React.FC = () => {
2014
const trucks = useAppSelector(selectTrucks);
2115

22-
const truckMarkers = useMemo(() => getTruckLocations(trucks), [trucks]);
16+
const mapReference = useRef<HTMLDivElement | null>(null);
17+
18+
useAppMap({
19+
center: null,
20+
destination: null,
21+
className: styles.map,
22+
mapReference: mapReference,
23+
});
2324

2425
const { connectToHomeRoom, disconnectFromHomeRoom } =
2526
useHomePageSocketService();
@@ -33,19 +34,14 @@ const HomePage: React.FC = () => {
3334
}, [connectToHomeRoom, disconnectFromHomeRoom]);
3435

3536
return (
36-
<LoadScript
37-
googleMapsApiKey={config.ENV.API.GOOGLE_MAPS_API_KEY}
38-
libraries={libraries}
39-
>
40-
<div className={styles.container}>
41-
<section className={styles.trucks}>
42-
<TruckList trucks={trucks} />
43-
</section>
44-
<section className={styles.map}>
45-
<Map className={styles['map-component']} markers={truckMarkers} />
46-
</section>
47-
</div>
48-
</LoadScript>
37+
<div className={styles.container}>
38+
<section className={styles.trucks}>
39+
<TruckList trucks={trucks} />
40+
</section>
41+
<section className={styles.map}>
42+
<div ref={mapReference} id="map" className={styles.map} />
43+
</section>
44+
</div>
4945
);
5046
};
5147

frontend/src/pages/order-status/order-status.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const OrderStatusPage: React.FC = () => {
6060
const mapReference = useRef<HTMLDivElement>(null);
6161

6262
useAppMap({
63-
center: truckLocation ?? DEFAULT_CENTER, //FIXME: When the page refreshes, the truck data goes down. In addition, this value always comes as null
63+
center: truckLocation ?? DEFAULT_CENTER,
6464
destination: order ? jsonToLatLngLiteral(order.startPoint) : null,
6565
className: styles.map,
6666
mapReference: mapReference,

0 commit comments

Comments
 (0)