Pijomir

Untitled

Feb 28th, 2024
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const departButtonElement = document.getElementById('depart');
  2. const arriveButtonElement = document.getElementById('arrive');
  3. const infoElement = document.getElementById('info');
  4.  
  5. function solve() {
  6.     let busStopName = 'Depot';
  7.     let busStopId = 'depot'
  8.     const url = 'http://localhost:3030/jsonstore/bus/schedule';
  9.     async function depart() {
  10.         try {
  11.             const response = await fetch(`${url}/${busStopId}`);
  12.             if (!response.ok) {
  13.                 const errorMessage = await response.json();
  14.                 throw errorMessage;
  15.             }
  16.  
  17.             const data = await response.json();
  18.             busStopId = data.next;
  19.             infoElement.textContent = `Next stop ${busStopName}`;
  20.             departButtonElement.disabled = true;
  21.             arriveButtonElement.disabled = false;
  22.         } catch {
  23.             infoElement.textContent = 'Error';
  24.             departButtonElement.disabled = true;
  25.             arriveButtonElement.disabled = true;
  26.         }
  27.     }
  28.  
  29.     async function arrive() {
  30.         try {
  31.             const response = await fetch(`${url}/${busStopId}`);
  32.             if (!response.ok) {
  33.                 const errorMessage = await response.json();
  34.                 throw errorMessage;
  35.             }
  36.  
  37.             const data = await response.json();
  38.             infoElement.textContent = `Arriving at ${busStopName}`;
  39.             console.log(data);
  40.             busStopName = data.name;
  41.             departButtonElement.disabled = false;
  42.             arriveButtonElement.disabled = true;
  43.         } catch {
  44.             infoElement.textContent = 'Error';
  45.             departButtonElement.disabled = true;
  46.             arriveButtonElement.disabled = true;
  47.         }
  48.     }
  49.  
  50.     return {
  51.         depart,
  52.         arrive
  53.     };
  54. }
  55.  
  56. let result = solve();
Add Comment
Please, Sign In to add comment