-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap.js
More file actions
303 lines (267 loc) · 8.1 KB
/
Copy pathMap.js
File metadata and controls
303 lines (267 loc) · 8.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import React, { useRef, useState, useEffect } from 'react';
import {
View,
StyleSheet
} from 'react-native';
import MapView, { Marker, Callout } from 'react-native-maps';
import Geolocation from '@react-native-community/geolocation';
import { getPins } from './server';
import Pin from './Pin';
import CreatePinView from './CreatePinView';
import CreateEventView from './CreateEventView';
import PinDetail from './PinDetail';
import MenuButton from './MenuButton';
import OptionsMenu from './OptionsMenu';
import AgentsMenu from './AgentsMenu';
import NotesMenu from './NotesMenu';
import PlacesMenu from './PlacesMenu';
const Map = () => {
const map = useRef();
const [currentRegion, setCurrentRegion] = useState(undefined);
const [allowRegionChange, setAllowRegionChange] = useState(true);
const [isDraggingMap, setIsDraggingMap] = useState(false);
const [pins, setPins] = useState([]);
const [focusedPin, setFocusedPin] = useState(undefined);
const [showPinDetail, setShowPinDetail] = useState(false);
const [showCreatePin, setShowCreatePin] = useState(false);
const [showCreateEvent, setShowCreateEvent] = useState(false);
const [showMenuButton, setShowMenuButton] = useState(true);
const [showOptionsMenu, setShowOptionsMenu] = useState(false);
const [showAgentsMenu, setShowAgentsMenu] = useState(false);
const [showNotesMenu, setShowNotesMenu] = useState(false);
const [showPlacesMenu, setShowPlacesMenu] = useState(false);
const [isCreatingNote, setIsCreatingNote] = useState(false);
// Use user position to set initial map region
useEffect(() => {
const setUserRegion = () => {
Geolocation.getCurrentPosition((userPosition) => {
const userRegion = {
latitude: userPosition.coords.latitude,
longitude: userPosition.coords.longitude,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421
};
setCurrentRegion(userRegion);
});
}
setUserRegion();
}, []);
// Fetch pins
useEffect(() => {
const getPinsData = async () => {
const pinsData = await getPins();
return pinsData;
}
getPinsData()
.then((pinsData) => setPins(pinsData))
.catch((error) => alert(error.message));
}, []);
// hide create view and pin detail
const onTouchMapStart = () => {
setIsDraggingMap(true);
setShowPinDetail(false);
}
const onTouchMapEnd = () => {
setIsDraggingMap(false);
}
// open pin detail with selected pin
const onPressPin = (pin) => {
setFocusedPin(pin);
setShowPinDetail(true);
}
// open agents menu
const onPressAgentsButton = () => {
setShowOptionsMenu(false);
setShowAgentsMenu(true);
}
const onCloseAgentsMenu = () => {
setShowAgentsMenu(false);
}
const onCreateAgentNote = () => {
setShowAgentsMenu(false);
setIsCreatingNote(true);
onOpenNotesMenu();
}
const onOpenNotesMenu = () => {
setShowNotesMenu(true);
}
// open places menu
const onPressPlacesButton = () => {
setShowOptionsMenu(false);
setShowPlacesMenu(true);
}
// close places menu and scroll map to selected pin
const onSelectPlacesMenuItem = (pin) => {
if (!map || !map.current) return;
const pinRegion = {
latitude: pin.coordinate.latitude,
longitude: pin.coordinate.longitude,
latitudeDelta: pin.coordinate.latitudeDelta,
longitudeDelta: pin.coordinate.longitudeDelta
};
// close places menu
setShowPlacesMenu(false);
// animate scroll to the pin on map
map.current.animateToRegion(pinRegion, 255);
// set the focused pin and display pin detail
setFocusedPin(pin);
setShowPinDetail(true);
}
const onClosePlacesMenu = () => {
setShowPlacesMenu(false);
}
// begin pin creation flow
const onSelectCreatePin = () => {
setShowCreatePin(true);
setShowOptionsMenu(false);
setShowMenuButton(false);
}
// cancel pin creation flow
const onCancelCreatePin = () => {
setAllowRegionChange(true);
setShowCreatePin(false);
setShowMenuButton(true);
}
// finalize new pin creation
const onPressConfirmButton = (pinForm) => {
createPin(pinForm);
// console.log(currentRegion);
setAllowRegionChange(true);
setShowCreatePin(false);
setShowMenuButton(true);
}
// begin event creation flow
const onSelectCreateEvent = () => {
setShowPinDetail(false);
setShowCreateEvent(true);
setShowMenuButton(false);
}
const onCancelCreateEvent = () => {
setShowCreateEvent(false);
setShowMenuButton(true);
}
const createPin = (pinForm) => {
const pinCoordinate = {
latitude: currentRegion.latitude,
longitude: currentRegion.longitude,
latitudeDelta: currentRegion.latitudeDelta,
longitudeDelta: currentRegion.longitudeDelta
};
const pin = {
title: pinForm.title,
description: pinForm.description,
pinColor: pinForm.pinColor,
id: pins.length,
coordinate: pinCoordinate,
};
pins.push(pin);
// TODO POST new pin
}
if (!currentRegion) {
return <></>;
}
return (
<View style={styles.container}>
<MapView
ref={map}
style={styles.map}
showsUserLocation={true} // TODO handle false case
mapType={'hybrid'}
initialRegion={currentRegion}
onRegionChangeComplete={(region) => setCurrentRegion(region)}
scrollEnabled={allowRegionChange}
zoomEnabled={allowRegionChange}
rotateEnabled={allowRegionChange}
pitchEnabled={allowRegionChange}
onTouchStart={() => onTouchMapStart()}
onTouchEnd={() => onTouchMapEnd()}
>
{pins.map((pin, index) => (
<Marker
key={index}
title={pin.title}
description={pin.description}
coordinate={pin.coordinate}
tracksViewChanges={false}
stopPropagation={true}
onPress={() => onPressPin(pin)}
>
<Pin
color={pin.pinColor}
style={styles.pin}
/>
<Callout tooltip />
</Marker>
))}
</MapView>
<MenuButton
shouldShow={showMenuButton}
onPress={() => setShowOptionsMenu(true)}
/>
<CreatePinView
shouldShow={showCreatePin}
isDraggingMap={isDraggingMap}
currentRegion={currentRegion}
onConfirmLocation={() => setAllowRegionChange(false)}
onRepositionPin={() => setAllowRegionChange(true)}
onPressCancelButton={() => onCancelCreatePin()}
onPressConfirmButton={(pinForm) => onPressConfirmButton(pinForm)}
/>
<CreateEventView
shouldShow={showCreateEvent}
pin={focusedPin}
onPressCancelButton={() => onCancelCreateEvent()}
/>
<OptionsMenu
shouldShow={showOptionsMenu}
onPressActivityButton={() => console.log('activity')}
onPressExploreButton={() => console.log('explore')}
onPressPlacesButton={() => onPressPlacesButton()}
onPressUserButton={() => onPressAgentsButton()}
onPressAddButton={() => onSelectCreatePin()}
onCloseOptionsMenu={() => setShowOptionsMenu(false)}
/>
<AgentsMenu
shouldShow={showAgentsMenu}
onClose={() => onCloseAgentsMenu()}
onCreateAgentNote={() => onCreateAgentNote()}
/>
<NotesMenu
shouldShow={showNotesMenu}
showCreate={isCreatingNote}
onClose={() => setShowNotesMenu(false)}
/>
<PlacesMenu
shouldShow={showPlacesMenu}
pins={pins}
onClose={() => onClosePlacesMenu()}
onSelectItem={(pin) => onSelectPlacesMenuItem(pin)}
/>
<PinDetail
shouldShow={showPinDetail}
showPreview={true}
pin={focusedPin ?? {}}
onPressAdd={() => onSelectCreateEvent()}
/>
</View>
);
};
const styles = StyleSheet.create({
container: {
height: '100%',
width: '100%',
display: 'flex',
justifyContent: 'center',
alignItems: 'center'
},
map: {
...StyleSheet.absoluteFillObject
},
pin: {
bottom: '16%',
display: 'flex',
flexDirection: 'row',
justifyContent: 'center'
}
});
export default Map;