1
1
import { useEffect , useRef , useState } from "react" ;
2
2
import createMarkerElement from "../components/map/mapMarkers" ;
3
3
import { Markers } from "../constant/enum/markerEnum" ;
4
- import { RouteEdge } from "../data/types/route" ;
5
4
import useMap from "../hooks/useMap" ;
6
5
import createAdvancedMarker from "../utils/markers/createAdvanedMarker" ;
7
- import { mockNavigationRoute } from "../data/mock/hanyangRoute" ;
8
6
import { ClickEvent } from "../data/types/event" ;
9
7
import createSubNodes from "../utils/polylines/createSubnodes" ;
10
8
import { LatLngToLiteral } from "../utils/coordinates/coordinateTransform" ;
@@ -17,6 +15,10 @@ import toggleMarkers from "../utils/markers/toggleMarkers";
17
15
import BackButton from "../components/map/backButton" ;
18
16
import useUniversityInfo from "../hooks/useUniversityInfo" ;
19
17
import useRedirectUndefined from "../hooks/useRedirectUndefined" ;
18
+ import { University } from "../data/types/university" ;
19
+ import { useSuspenseQuery } from "@tanstack/react-query" ;
20
+ import { getAllRoutes } from "../api/route" ;
21
+ import { CoreRoutesList } from "../data/types/route" ;
20
22
21
23
const colors = [
22
24
"#f1a2b3" ,
@@ -50,7 +52,12 @@ export default function ReportRoutePage() {
50
52
const [ isCautionAcitve , setIsCautionActive ] = useState < boolean > ( true ) ;
51
53
52
54
const { university } = useUniversityInfo ( ) ;
53
- useRedirectUndefined < string | undefined > ( [ university ] ) ;
55
+ useRedirectUndefined < University | undefined > ( [ university ] ) ;
56
+
57
+ const { data } = useSuspenseQuery ( {
58
+ queryKey : [ "routes" , university ?. id ] ,
59
+ queryFn : ( ) => getAllRoutes ( university ?. id ?? 1001 ) ,
60
+ } ) ;
54
61
55
62
const addHazardMarker = ( ) => {
56
63
if ( AdvancedMarker === null || map === null ) return ;
@@ -119,97 +126,98 @@ export default function ReportRoutePage() {
119
126
newPolyLine . current . setPath ( [ ] ) ;
120
127
} ;
121
128
122
- const drawRoute = ( routes : RouteEdge [ ] ) => {
129
+ const drawRoute = ( coreRouteList : CoreRoutesList ) => {
123
130
if ( ! Polyline || ! AdvancedMarker || ! map ) return ;
124
131
125
- for ( const route of routes ) {
126
- const coreNode = route . endNode ;
132
+ for ( const coreRoutes of coreRouteList ) {
133
+ // 가장 끝쪽 Core Node 그리기
134
+ const endNode = coreRoutes . routes [ coreRoutes . routes . length - 1 ] . node2 ;
127
135
128
136
createAdvancedMarker (
129
137
AdvancedMarker ,
130
138
map ,
131
- coreNode ,
139
+ endNode ,
132
140
createMarkerElement ( { type : Markers . WAYPOINT , className : "translate-waypoint" } ) ,
133
141
) ;
142
+ for ( const coreRoute of coreRoutes . routes ) {
143
+ const routePolyLine = new Polyline ( {
144
+ map : map ,
145
+ path : [ coreRoute . node1 , coreRoute . node2 ] ,
146
+ strokeColor : "#808080" ,
147
+ } ) ;
148
+ routePolyLine . addListener ( "click" , ( e : ClickEvent ) => {
149
+ const subNodes = createSubNodes ( routePolyLine ) ;
134
150
135
- const routePolyLine = new Polyline ( {
136
- map : map ,
137
- path : [ route . startNode , route . endNode ] ,
138
- strokeColor : "#808080" ,
139
- } ) ;
140
-
141
- routePolyLine . addListener ( "click" , ( e : ClickEvent ) => {
142
- const subNodes = createSubNodes ( routePolyLine ) ;
143
-
144
- const edges = subNodes
145
- . map (
146
- ( node , idx ) =>
147
- [ node , subNodes [ idx + 1 ] ] as [ google . maps . LatLngLiteral , google . maps . LatLngLiteral ] ,
148
- )
149
- . slice ( 0 , - 1 ) ;
150
-
151
- const point = LatLngToLiteral ( e . latLng ) ;
151
+ const edges = subNodes
152
+ . map (
153
+ ( node , idx ) =>
154
+ [ node , subNodes [ idx + 1 ] ] as [ google . maps . LatLngLiteral , google . maps . LatLngLiteral ] ,
155
+ )
156
+ . slice ( 0 , - 1 ) ;
152
157
153
- const { edge : nearestEdge , point : nearestPoint } = findNearestSubEdge ( edges , point ) ;
158
+ const point = LatLngToLiteral ( e . latLng ) ;
154
159
155
- const tempWaypointMarker = createAdvancedMarker (
156
- AdvancedMarker ,
157
- map ,
158
- nearestPoint ,
159
- createMarkerElement ( {
160
- type : Markers . WAYPOINT ,
161
- className : "translate-waypoint" ,
162
- } ) ,
163
- ) ;
160
+ const { edge : nearestEdge , point : nearestPoint } = findNearestSubEdge ( edges , point ) ;
164
161
165
- if ( originPoint . current ) {
166
- setNewPoints ( ( prevPoints ) => {
167
- if ( prevPoints . element ) {
168
- prevPoints . element . position = nearestPoint ;
169
- return {
170
- ...prevPoints ,
171
- coords : [ ...prevPoints . coords , nearestPoint ] ,
172
- } ;
173
- } else {
174
- setIsActive ( true ) ;
175
- return {
176
- element : new AdvancedMarker ( {
177
- map : map ,
178
- position : nearestPoint ,
179
- content : createMarkerElement ( {
180
- type : Markers . DESTINATION ,
162
+ const tempWaypointMarker = createAdvancedMarker (
163
+ AdvancedMarker ,
164
+ map ,
165
+ nearestPoint ,
166
+ createMarkerElement ( {
167
+ type : Markers . WAYPOINT ,
168
+ className : "translate-waypoint" ,
169
+ } ) ,
170
+ ) ;
171
+ if ( originPoint . current ) {
172
+ setNewPoints ( ( prevPoints ) => {
173
+ if ( prevPoints . element ) {
174
+ prevPoints . element . position = nearestPoint ;
175
+ return {
176
+ ...prevPoints ,
177
+ coords : [ ...prevPoints . coords , nearestPoint ] ,
178
+ } ;
179
+ } else {
180
+ setIsActive ( true ) ;
181
+ return {
182
+ element : new AdvancedMarker ( {
183
+ map : map ,
184
+ position : nearestPoint ,
185
+ content : createMarkerElement ( {
186
+ type : Markers . DESTINATION ,
187
+ } ) ,
181
188
} ) ,
182
- } ) ,
183
- coords : [ ...prevPoints . coords , nearestPoint ] ,
184
- } ;
185
- }
186
- } ) ;
187
- } else {
188
- const originMarker = new AdvancedMarker ( {
189
- map : map ,
190
- position : nearestPoint ,
191
- content : createMarkerElement ( { type : Markers . ORIGIN } ) ,
192
- } ) ;
189
+ coords : [ ...prevPoints . coords , nearestPoint ] ,
190
+ } ;
191
+ }
192
+ } ) ;
193
+ } else {
194
+ const originMarker = new AdvancedMarker ( {
195
+ map : map ,
196
+ position : nearestPoint ,
197
+ content : createMarkerElement ( { type : Markers . ORIGIN } ) ,
198
+ } ) ;
193
199
194
- originPoint . current = {
195
- point : nearestPoint ,
196
- element : originMarker ,
197
- } ;
200
+ originPoint . current = {
201
+ point : nearestPoint ,
202
+ element : originMarker ,
203
+ } ;
198
204
199
- setNewPoints ( {
200
- element : null ,
201
- coords : [ nearestPoint ] ,
202
- } ) ;
203
- }
204
- } ) ;
205
- }
205
+ setNewPoints ( {
206
+ element : null ,
207
+ coords : [ nearestPoint ] ,
208
+ } ) ;
209
+ }
210
+ } ) ;
211
+ }
212
+ const startNode = coreRoutes . routes [ 0 ] . node1 ;
206
213
207
- createAdvancedMarker (
208
- AdvancedMarker ,
209
- map ,
210
- routes [ 0 ] . startNode ,
211
- createMarkerElement ( { type : Markers . WAYPOINT , className : "translate-waypoint" } ) ,
212
- ) ;
214
+ createAdvancedMarker (
215
+ AdvancedMarker ,
216
+ map ,
217
+ startNode ,
218
+ createMarkerElement ( { type : Markers . WAYPOINT , className : "translate-waypoint" } ) ,
219
+ ) ;
220
+ }
213
221
} ;
214
222
215
223
useEffect ( ( ) => {
@@ -219,7 +227,7 @@ export default function ReportRoutePage() {
219
227
} , [ newPoints ] ) ;
220
228
221
229
useEffect ( ( ) => {
222
- drawRoute ( mockNavigationRoute . route ) ;
230
+ drawRoute ( data ) ;
223
231
addHazardMarker ( ) ;
224
232
if ( Polyline ) {
225
233
newPolyLine . current = new Polyline ( { map : map , path : [ ] , strokeColor : "#0367FB" } ) ;
0 commit comments