-
Notifications
You must be signed in to change notification settings - Fork 5
/
MapContainer.js
251 lines (220 loc) · 8.5 KB
/
MapContainer.js
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
import React, {Component} from 'react';
import {Map, Marker, GoogleApiWrapper} from 'google-maps-react';
import Modal from 'react-modal';
import close from './../images/close.png';
import restaurant_icon from './../images/restaurant_icon.png';
import sports_icon from './../images/sports_icon.png';
import star_icon from './../images/star_icon.png';
import InfoWindowEx from './InfoWindowEx'
import AddLocation from './AddLocation';
import Restaurant from './Restaurant';
import SportsVenue from './SportsVenue';
import UpdateLastVisit from './UpdateLastVisit';
require('dotenv').config();
export class MapContainer extends Component {
constructor(props) {
super(props);
this.state = {
showingInfoWindow: false,
activeMarker: {},
selectedPlace: {},
locations: []
}
this.onMarkerClick = this.onMarkerClick.bind(this);
this.onMapClicked = this.onMapClicked.bind(this);
this.getIcon = this.getIcon.bind(this);
this.toggleViewLocationModal = this.toggleViewLocationModal.bind(this);
this.toggleAddLocationModal = this.toggleAddLocationModal.bind(this);
this.toggleUpdateLastVisitModal = this.toggleUpdateLastVisitModal.bind(this);
this.loadLocations = this.loadLocations.bind(this);
}
componentDidMount() {
this.loadLocations();
}
async loadLocations() {
await this.getLocations()
.then(res => this.setState({ locations: res }))
.catch(err => console.log(err));
}
async getLocations() {
const response = await fetch('/api/locations');
const body = await response.json();
if (response.status !== 200) {
throw Error(body.message)
}
return body;
};
onMarkerClick(props, marker, e) {
this.setState({
selectedPlace: props,
activeMarker: marker,
showingInfoWindow: true
});
}
onMapClicked(props) {
if (this.state.showingInfoWindow) {
this.setState({
showingInfoWindow: false,
activeMarker: null
})
}
}
getIcon(type) {
if (type === 'R') {
return restaurant_icon;
}
else if (type === 'S') {
return sports_icon;
}
else {
return star_icon;
}
}
openModal(location,target) {
this.setState({
locationid: location.id,
locationtype: location.type,
locationname: location.name
});
target();
}
toggleViewLocationModal() {
this.setState({
isViewLocationOpen: !this.state.isViewLocationOpen
});
}
toggleAddLocationModal(reload) {
this.setState({
isAddLocationOpen: !this.state.isAddLocationOpen
})
if (reload) {
this.loadLocations();
}
}
toggleUpdateLastVisitModal(reload) {
this.setState({
isUpdateLastVisitOpen: !this.state.isUpdateLastVisitOpen
})
if (reload) {
this.loadLocations();
}
}
viewLocation() {
if (this.state.locationtype === 'R') {
return(<Restaurant id={this.state.locationid} onUpdate={this.loadLocations}></Restaurant>);
}
else if (this.state.locationtype === 'S') {
return(<SportsVenue id={this.state.locationid} onUpdate={this.loadLocations}></SportsVenue>);
}
}
getInfoWindowContents(selectedPlace) {
if (selectedPlace !== undefined &&
selectedPlace.type !== undefined ) {
var location = this.state.locations.filter(function(location) { return location.id === selectedPlace.id })[0];
if (selectedPlace.type === 'R') {
return(
<div>
<p className="actionable" onClick={() => this.openModal(location, this.toggleViewLocationModal)}>View Details</p>
<p className="small-text-title">{location.description}</p>
</div>
);
}
else if (selectedPlace.type === 'S') {
return(
<div>
<p className="actionable" onClick={() => this.openModal(location, this.toggleViewLocationModal)}>View Details</p>
<p className="small-text-title">{location.description}</p>
</div>
);
}
else {
return(
<div>
<div>
<p className="small-text-title">Last visited</p>
{location.description}
</div>
<p className="actionable" onClick={() => this.openModal(location, this.toggleUpdateLastVisitModal)}>Update Last Visit</p>
</div>
);
}
}
}
renderLocations() {
return this.state.locations.map(location => (
<Marker onClick={this.onMarkerClick}
id={location.id}
name={location.name}
type={location.type}
description={location.description}
position={{ lat: location.latitude, lng: location.longitude }}
icon={{ url: this.getIcon(location.type) }}/>
))
}
render() {
return (
<div>
<Map google={this.props.google}
zoom={14}
onClick={this.onMapClicked}
initialCenter={{
lat: 41.8781,
lng: -87.6298
}}>
{this.renderLocations()}
<InfoWindowEx
marker={this.state.activeMarker}
visible={this.state.showingInfoWindow}>
<div className="centered location-details">
<h3>{this.state.selectedPlace.name}</h3>
{this.getInfoWindowContents(this.state.selectedPlace)}
</div>
</InfoWindowEx>
</Map>
<button className="add-location-actionable" onClick={this.toggleAddLocationModal}>Add Location</button>
<Modal
isOpen={this.state.isViewLocationOpen}
onRequestClose={this.toggleViewLocationModal}
className="modal"
overlayClassName="overlay">
<div>
<img src={close} className="modal-close" alt="close" onClick={this.toggleViewLocationModal} />
<div className="modal-title">
<h3>{this.state.locationname}</h3>
</div>
{this.viewLocation()}
</div>
</Modal>
<Modal
isOpen={this.state.isAddLocationOpen}
onRequestClose={() => this.toggleAddLocationModal(false)}
className="modal"
overlayClassName="overlay">
<div>
<img src={close} className="modal-close" alt="close" onClick={() => this.toggleAddLocationModal(false)} />
<div className="modal-title">
<h3>Add Location</h3>
</div>
<AddLocation onSave={() => this.toggleAddLocationModal(true)} />
</div>
</Modal>
<Modal
isOpen={this.state.isUpdateLastVisitOpen}
onRequestClose={this.toggleUpdateLastVisitModal}
className="modal"
overlayClassName="overlay">
<div>
<img src={close} className="modal-close" alt="close" onClick={() => this.toggleUpdateLastVisitModal(false)} />
<div className="modal-title">
<h3>Update Last Visit</h3>
</div>
<UpdateLastVisit id={this.state.locationid} name={this.state.locationname} onSave={() => this.toggleUpdateLastVisitModal(true)} />
</div>
</Modal>
</div>
);
}
}
export default GoogleApiWrapper({
apiKey: (process.env.REACT_APP_GOOGLE_API_KEY)
})(MapContainer)