Guest User

Untitled

a guest
Mar 22nd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. /* --- Get data form API and display it in DOM --- */
  2.  
  3. // URL to get all launches from SpaceX API
  4. const allLaunchesURL = 'https://api.spacexdata.com/v2/launches/all';
  5.  
  6. // Get launch data from API
  7. const getLaunchData = async (url) => {
  8. let response = await fetch(url);
  9.  
  10. // Check if response is ok, if not throw an error
  11. if(!response.ok) {
  12. throw Error(`Error fetching API, response status: ${response.statusText}`);
  13. }
  14.  
  15. let data = await response.json();
  16. data = data.slice(-10);
  17. displayData(data);
  18. }
  19.  
  20. getLaunchData(allLaunchesURL);
  21.  
  22. // Display data on DOM
  23. function displayData(data) {
  24. const results = document.querySelector('.results');
  25. data.map(launch => {
  26. results.innerHTML += `
  27. <tr>
  28. <td>${launch.flight_number}</td>
  29. <td>${formatDate(launch.launch_date_utc)}</td>
  30. <td>${launch.rocket.rocket_name}</td>
  31. <td>${checkPastOrFuture(launch.launch_date_utc)}
  32. ${checkPastOrFuture(launch.launch_date_utc) === 'Launched' ? ' - ' + launchSuccess(launch) : ''}
  33. </td>
  34. <td>
  35. <button class="infoButton" id="${launch.flight_number}" data-rocket=${launch.rocket.rocket_id} onclick="getFlightDetails(this)">Click</button>
  36. </td>
  37. </tr>
  38. `
  39. })
  40. }
  41.  
  42. // Check if launch date is upcoming or in the past
  43. function checkPastOrFuture(date) {
  44. let currentDate = new Date();
  45. let dateToCheck = new Date(date);
  46. return currentDate < dateToCheck ? 'Upcoming' : 'Launched';
  47. }
  48.  
  49. // Check success of launch
  50. function launchSuccess(flight) {
  51. return flight.launch_success ? 'Successful' : 'Failure'
  52. }
  53.  
  54. // Format the date
  55. function formatDate(date) {
  56. const d = new Date(date);
  57. return d.toUTCString();
  58. }
  59.  
  60.  
  61. /* --- Get more info on individual launch when triggered by button click --- */
  62.  
  63. // Endpoint stub for API queries
  64. // Individual flight
  65. const flightURL = 'https://api.spacexdata.com/v2/launches?flight_number=';
  66.  
  67. // Rocket information
  68. const rocketURL = 'https://api.spacexdata.com/v2/rockets/';
  69.  
  70. // Get flight number from button id
  71. async function getFlightDetails(ele) {
  72.  
  73. // Get data for flight
  74. let response = await fetch(`${flightURL}${ele.id}`);
  75.  
  76. // Error checking
  77. if(!response.ok) {
  78. throw Error(`Error fetching flight details, response status: ${response.statusText}`);
  79. }
  80.  
  81. let data = await response.json();
  82. displayFlightData(data[0]);
  83.  
  84. // Get rocket data
  85. let rocketResponse = await fetch(`${rocketURL}${ele.dataset.rocket}`);
  86.  
  87. // Error checking
  88. if(!response.ok) {
  89. throw Error(`Error fetching rocket details, response status: ${response.statusText}`);
  90. }
  91.  
  92. let rocketData = await rocketResponse.json();
  93. displayRocketInfo(rocketData);
  94. }
  95.  
  96. function displayFlightData(flight) {
  97. const flightDiv = document.querySelector('.flightDetails');
  98. if(!flight) {
  99. flightDiv.innerHTML = `
  100. <h4>Flight details</h4>
  101. <p>Sorry, this flight has no further details</p>
  102. `;
  103. return;
  104. }
  105.  
  106. flightDiv.innerHTML = `
  107. <h4>Flight details</h4>
  108. <p>${flight.details}</p>
  109. <p><strong>Launch site:</strong> ${flight.launch_site.site_name_long}</p>
  110. <figure>
  111. <img src="${flight.links.mission_patch}" alt="Flight ${flight.flight_number} Mission Patch" title="Flight ${flight.flight_number} Mission Patch">
  112. <figcaption>Flight ${flight.flight_number} Mission Patch</figcaption>
  113. </figure>
  114. <br>
  115. `
  116. }
  117.  
  118. function displayRocketInfo(rocket) {
  119. const rocketDiv = document.querySelector('.rocketInfo');
  120. if(!rocketDiv) {
  121. rocketDiv.innerHTML = `
  122. <h4>Rocket Details</h4>
  123. <p>Sorry, this flight has no further details</p>
  124. `
  125. ;
  126. return;
  127. }
  128. rocketDiv.innerHTML = `
  129. <h4>Rocket Details</h4>
  130. <p><strong>Name:</strong> ${rocket.name}, <strong>ID:</strong> ${rocket.id}</p>
  131. <p><strong>Description:</strong> ${rocket.description}</p>
  132. <p><strong>Height:</strong> ${rocket.height.meters} metres</p>
  133. <p><strong>Mass:</strong> ${rocket.mass.kg} kg</p>
  134. <p><strong>Number of stages:</strong> ${rocket.stages}</p>
  135.  
  136. `
  137. }
Add Comment
Please, Sign In to add comment