-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
80 lines (65 loc) · 2.5 KB
/
index.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
//HACER UN BUCLE FOR EN EL REQUESTER PARA QUE SAQUE TODOS LOS NOMBRES DE LAS AGENCIAS
//Link: https://lienapuey.github.io/Project_AJAX_Oddity/
//https://launchlibrary.net/1.4/launch/next/5
function printHTML(datos) {
let cardBodyFirst = document.querySelectorAll('#mainRow')[0];
let claves = Object.keys(datos);
for (var i = 0; i < claves.length; i++) {
for (var j = 0; j < datos[claves[i]].length; j++) {
let stringText = datos[claves[i]][j]["missions"][0] != undefined ? datos[claves[i]][j]["missions"][0]["description"] : "No mission provided";
stringText.split(" ");
let stringTextArray = stringText.split(" ");
let textLimit = stringTextArray.slice(0, 31);
let textRead = textLimit.join(" ") + ' ...';
let imagURL = "";
switch (datos[claves[i]][j]["location"]["pads"][0]["agencies"][0]["name"]) {
case "SpaceX":
imagURL = "img/SpaceX.jpg";
break;
case "Rocket Lab Ltd":
imagURL = "img/roquetlab.png"
break;
case "Indian Space Research Organization":
imagURL = "img/ISRO.png"
break;
case "Russian Aerospace Defence Forces":
imagURL = "img/VVKO.png"
break;
}
cardBodyFirst.innerHTML +=
`<div class="col-4">
<img class="rounded-circle" src="${imagURL}" alt="Generic placeholder image" width="140" height="140">
<h4>${datos[claves[i]][j]["name"]}</h4>
<p>${datos[claves[i]][j]["missions"][0] != undefined ? textRead : "No mission provided"}</p>
<p><a class="btn btn-secondary" href="#" role="button">Read more »</a></p>
</div>
`;
}
}
}
function getData() {
var xhr = new XMLHttpRequest()
xhr.onreadystatechange = function() {
if (this.readyState != 4) {
return
}
if (this.status == 200) {
var data = JSON.parse(this.responseText);
var agencies = {};
for (var i = 0; i < data["launches"].length; i++) {
let nombre = data["launches"][i]["location"]["pads"][0]["agencies"][0]["name"];
let lanzamientoProp = data["launches"][i]; // datos de los lanzamientos
if (agencies[nombre] == undefined) {
agencies[nombre] = [lanzamientoProp]; // pasa los datos de los lanzamientos según el nombre de la empresa
} else {
agencies[nombre].push(lanzamientoProp);
}
}
printHTML(agencies);
}
}
xhr.open('GET', "https://launchlibrary.net/1.3/launch/next/5", true);
xhr.send();
}
getData();
//