-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirectus.js
120 lines (118 loc) · 3.62 KB
/
directus.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
/* Currently, all of this is referenced in the getAsyncData() method of our pages
* Extremely basic API to fetch data from directus for the frontend components to easily work with
*/
import fetch from "isomorphic-fetch"
require("es6-promise")
const thumbnailUrl =
"https://admin.squaredlabs.uconn.edu/thumbnail/500/400/contain/best/"
const thumbnailUrlIcon =
"https://admin.squaredlabs.uconn.edu/thumbnail/80/80/contain/best/"
function loadPeople(url, endpoint) {
console.log("Loading people")
// Depth 2 is specified in order to get thumbnail image URL
return fetch(encodeURI(url + endpoint + "?depth=2"))
.then(function(response) {
if (response.status >= 400) {
throw new Error("Bad response from server")
}
return response.json()
})
.then(function(data) {
console.log("Loaded people")
let peopleData = data.data.map(personData => ({
id: personData.id,
order: personData.sort,
name: personData.name,
title: personData.title,
class: personData.class,
degree: personData.degree,
description: unescapeHTML(personData.description),
roles: personData.roles,
alumni: personData.alumni === 1,
imageURL:
personData.picture === null
? ""
: thumbnailUrl + personData.picture.data.name,
iconURL:
personData.picture === null
? ""
: thumbnailUrlIcon + personData.picture.data.name
}))
alphabetize(peopleData, "name")
let people = { people: peopleData }
return people
})
.catch(function(error) {
console.log(error)
})
}
function loadProjects(url, endpoint) {
console.log("Loading projects")
return fetch(encodeURI(url + endpoint + "?depth=2"))
.then(function(response) {
if (response.status >= 400) {
throw new Error("Bad response from server")
}
return response.json()
})
.then(function(data) {
console.log("Loaded projects")
let projectsData = data.data.map(projectData => ({
id: projectData.id,
order: projectData.sort,
name: projectData.name,
client: projectData.client,
timespan: projectData.timespan,
services: projectData.services,
url: projectData.url,
technologies: projectData.technologies,
large_summary: unescapeHTML(projectData.large_summary),
small_summary: unescapeHTML(projectData.small_summary),
imageURL: projectData.thumbnail
? thumbnailUrl + projectData.thumbnail.data.name
: "",
images: projectData.project_graphics.data.map(
image => thumbnailUrl + image.name
)
}))
let projects = { projects: projectsData }
return projects
})
.catch(function(error) {
throw error
})
}
function alphabetize(data, keyName) {
data.sort((a, b) => {
let nameA = a[keyName].toUpperCase()
let nameB = b[keyName].toUpperCase()
if (nameA < nameB) return -1
if (nameA > nameB) return 1
return 0
})
}
function unescapeHTML(str) {
let html = str
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
return removeHTML(html)
}
function removeHTML(str) {
return str
.replace("<p>", "")
.replace("</p>", "")
.replace("<br>", "")
}
function startLoad() {
let peoplePromise = loadPeople(
"https://admin.squaredlabs.uconn.edu",
"/api/1.1/tables/people/rows"
)
let projectPromise = loadProjects(
"https://admin.squaredlabs.uconn.edu",
"/api/1.1/tables/projects/rows"
)
return Promise.all([peoplePromise, projectPromise])
}
export default startLoad