-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathApp.js
More file actions
219 lines (210 loc) · 6.6 KB
/
Copy pathApp.js
File metadata and controls
219 lines (210 loc) · 6.6 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
import "./app.css";
import "mapbox-gl/dist/mapbox-gl.css";
import Map, { Marker, Popup } from "react-map-gl";
import { useEffect, useState } from "react";
import { Room, Star, StarBorder } from "@material-ui/icons";
import axios from "axios";
import { format } from "timeago.js";
import Register from "./components/Register";
import Login from "./components/Login";
function App() {
const myStorage = window.localStorage;
const [currentUsername, setCurrentUsername] = useState(myStorage.getItem("user"));
const [pins, setPins] = useState([]);
const [currentPlaceId, setCurrentPlaceId] = useState(null);
const [newPlace, setNewPlace] = useState(null);
const [title, setTitle] = useState(null);
const [desc, setDesc] = useState(null);
const [star, setStar] = useState(0);
const [viewport, setViewport] = useState({
latitude: 47.040182,
longitude: 17.071727,
zoom: 4,
});
const [showRegister, setShowRegister] = useState(false);
const [showLogin, setShowLogin] = useState(false);
const handleMarkerClick = (id, lat, long) => {
setCurrentPlaceId(id);
setViewport({ ...viewport, latitude: lat, longitude: long });
};
const handleAddClick = (e) => {
const {lng: longitude, lat: latitude} = e.lngLat;
setNewPlace({
lat: latitude,
long: longitude,
});
};
const handleSubmit = async (e) => {
e.preventDefault();
const newPin = {
username: currentUsername,
title,
desc,
rating: star,
lat: newPlace.lat,
long: newPlace.long,
};
try {
const res = await axios.post("/pins", newPin);
setPins([...pins, res.data]);
setNewPlace(null);
} catch (err) {
console.log(err);
}
};
useEffect(() => {
const getPins = async () => {
try {
const allPins = await axios.get("/pins");
setPins(allPins.data);
} catch (err) {
console.log(err);
}
};
getPins();
}, []);
const handleLogout = () => {
setCurrentUsername(null);
myStorage.removeItem("user");
};
return (
<div>
<Map
initialViewState={{ ...viewport }}
mapboxAccessToken=""
style={{ width: "100vw", height: "100vh" }}
transitionDuration="200"
mapStyle="mapbox://styles/safak/cknndpyfq268f17p53nmpwira"
onViewportChange={(viewport) => setViewport(viewport)}
onDblClick={currentUsername && handleAddClick}
>
{pins.map((p) => (
<>
<Marker
latitude={p.lat}
longitude={p.long}
offsetLeft={-3.5 * viewport.zoom}
offsetTop={-7 * viewport.zoom}
>
<Room
style={{
fontSize: 7 * viewport.zoom,
color:
currentUsername === p.username ? "tomato" : "slateblue",
cursor: "pointer",
}}
onClick={() => handleMarkerClick(p._id, p.lat, p.long)}
/>
</Marker>
{p._id === currentPlaceId && (
<Popup
key={p._id}
latitude={p.lat}
longitude={p.long}
closeButton={true}
closeOnClick={false}
onClose={() => setCurrentPlaceId(null)}
anchor="left"
>
<div className="card">
<label>Place</label>
<h4 className="place">{p.title}</h4>
<label>Review</label>
<p className="desc">{p.desc}</p>
<label>Rating</label>
<div className="stars">
{Array(p.rating).fill(<Star className="star" />)}
</div>
<label>Information</label>
<span className="username">
Created by <b>{p.username}</b>
</span>
<span className="date">{format(p.createdAt)}</span>
</div>
</Popup>
)}
</>
))}
{newPlace && (
<>
<Marker
latitude={newPlace.lat}
longitude={newPlace.long}
offsetLeft={-3.5 * viewport.zoom}
offsetTop={-7 * viewport.zoom}
>
<Room
style={{
fontSize: 7 * viewport.zoom,
color: "tomato",
cursor: "pointer",
}}
/>
</Marker>
<Popup
latitude={newPlace.lat}
longitude={newPlace.long}
closeButton={true}
closeOnClick={false}
onClose={() => setNewPlace(null)}
anchor="left"
>
<div>
<form onSubmit={handleSubmit}>
<label>Title</label>
<input
placeholder="Enter a title"
autoFocus
onChange={(e) => setTitle(e.target.value)}
/>
<label>Description</label>
<textarea
placeholder="Tell us something about this place."
onChange={(e) => setDesc(e.target.value)}
/>
<label>Rating</label>
<select onChange={(e) => setStar(e.target.value)}>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<button type="submit" className="submitButton">
Add Pin
</button>
</form>
</div>
</Popup>
</>
)}
{currentUsername ? (
<button className="button logout" onClick={handleLogout}>
Log out
</button>
) : (
<div className="buttons">
<button className="button login" onClick={() => setShowLogin(true)}>
Log in
</button>
<button
className="button register"
onClick={() => setShowRegister(true)}
>
Register
</button>
</div>
)}
{showRegister && <Register setShowRegister={setShowRegister} />}
{showLogin && (
<Login
setShowLogin={setShowLogin}
setCurrentUsername={setCurrentUsername}
myStorage={myStorage}
/>
)}
</Map>
</div>
);
}
export default App;