forked from garris/BackstopJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
148 lines (111 loc) · 3.21 KB
/
App.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
const React = require('react');
const Search = require('./Search');
const Map = require('./Map');
const CurrentLocation = require('./CurrentLocation');
const LocationList = require('./LocationList');
const App = React.createClass({
getInitialState() {
// Extract the favorite locations from local storage
let favorites = [];
if (localStorage.favorites) {
favorites = JSON.parse(localStorage.favorites);
}
// Nobody would get mad if we center it on Paris by default
return {
favorites: favorites,
currentAddress: 'Paris, France',
mapCoordinates: {
lat: 48.856614,
lng: 2.3522219
}
};
},
toggleFavorite(address) {
if (this.isAddressInFavorites(address)) {
this.removeFromFavorites(address);
} else {
this.addToFavorites(address);
}
},
addToFavorites(address) {
const favorites = this.state.favorites;
favorites.push({
address: address,
timestamp: Date.now()
});
this.setState({
favorites: favorites
});
localStorage.favorites = JSON.stringify(favorites);
},
removeFromFavorites(address) {
const favorites = this.state.favorites;
let index = -1;
for (let i = 0; i < favorites.length; i++) {
if (favorites[i].address == address) {
index = i;
break;
}
}
// If it was found, remove it from the favorites array
if (index !== -1) {
favorites.splice(index, 1);
this.setState({
favorites: favorites
});
localStorage.favorites = JSON.stringify(favorites);
}
},
isAddressInFavorites(address) {
const favorites = this.state.favorites;
for (let i = 0; i < favorites.length; i++) {
if (favorites[i].address == address) {
return true;
}
}
return false;
},
searchForAddress(address) {
const self = this;
// We will use GMaps' geocode functionality,
// which is built on top of the Google Maps API
GMaps.geocode({
address: address,
callback: function (results, status) {
if (status !== 'OK') return;
const latlng = results[0].geometry.location;
self.setState({
currentAddress: results[0].formatted_address,
mapCoordinates: {
lat: latlng.lat(),
lng: latlng.lng()
}
});
setTimeout(function () {
console.log('gmapResponded');
}, 1000);
}
});
},
componentDidMount() {
const that = this;
setTimeout(function () {
that.searchForAddress('San Francisco');
}, 2000);
},
render() {
return (
<div>
<h1>Your Google Maps Locations</h1>
<Search onSearch={this.searchForAddress}/>
<Map lat={this.state.mapCoordinates.lat} lng={this.state.mapCoordinates.lng}/>
<CurrentLocation address={this.state.currentAddress}
favorite={this.isAddressInFavorites(this.state.currentAddress)}
onFavoriteToggle={this.toggleFavorite}/>
<LocationList locations={this.state.favorites} activeLocationAddress={this.state.currentAddress}
onClick={this.searchForAddress}/>
</div>
);
}
});
module.exports = App;