-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
84 lines (73 loc) · 2.2 KB
/
Copy pathserver.js
File metadata and controls
84 lines (73 loc) · 2.2 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
export const getPins = async () => {
const pinsData = await fetchPins();
const pins = pinsData.map((pinData) => parsePin(pinData));
return pins;
}
export const getAgents = async () => {
const agentsData = await fetchAgents();
const agents = agentsData.map((agentData) => parseAgent(agentData));
return agents;
}
export const getPhotoUrlById = async (photoId) => {
if (photoId === undefined || photoId[0] === undefined) {
return getPlaceholderPhotoUrl();
}
const photo = await fetchPhotoById(photoId[0]);
const photoUrl = photo.url;
return photoUrl;
}
// prepend to all fetch request URLs
const rootUrl = 'https://my-json-server.typicode.com/perfectsimulation/based';
// fetch all pins
const fetchPins = async () => {
return fetch(`${rootUrl}/pins`)
.then((response) => response.json())
.then((json) => json);
}
const fetchAgents = async () => {
return fetch(`${rootUrl}/agents`)
.then((response) => response.json())
.then((json) => json);
}
// fetch photo by ID
const fetchPhotoById = async (photoId) => {
return fetch(`${rootUrl}/photos/${photoId}`)
.then((response) => response.json())
.then((json) => json);
}
// use placeholder image url for pins without any uploaded photos
const getPlaceholderPhotoUrl = () => {
return 'https://vtskiandride.com/wp-content/uploads/2015/08/placeholder.jpg';
}
// construct pin object from response
const parsePin = (pinData) => {
const pin = {
'id': pinData.id,
'authorUserId': pinData.authorUserId,
'privateUserIds': pinData.privateUserIds,
'photoIds': pinData.photoIds,
'eventIds': pinData.eventIds,
'publishTime': pinData.publishTime,
'pinColor': pinData.pinColor,
'title': pinData.title,
'description': pinData.description,
'isPublic': pinData.isPublic,
'coordinate': {
'latitude': pinData.latitude,
'longitude': pinData.longitude,
'latitudeDelta': pinData.latitudeDelta,
'longitudeDelta': pinData.longitudeDelta,
}
};
return pin;
}
const parseAgent = (agentData) => {
const agent = {
'id': agentData.id,
'name': agentData.name,
'photoId': agentData.photoId,
'noteIds': agentData.noteIds,
'eventIds': agentData.eventIds
};
return agent;
}