Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import 'bootstrap/dist/css/bootstrap.css'
  2. document.addEventListener("DOMContentLoaded", function () {
  3.     /*---------- Begin Get Person By Name ---------*/
  4.     /*----- Should be moved to NavBar function ----*/
  5.     fillViewPersonWithDataDiv();
  6.     fillViewAllPersonsWithDataDiv()
  7.     document.getElementById("viewPersonWithDataButtonTAG").addEventListener('click', function (event) {
  8.         event.preventDefault();
  9.         singleuser();
  10.     });
  11.     document.getElementById("viewAllPersonsWithDataButtonTAG").addEventListener('click', function (event) {
  12.         event.preventDefault();
  13.         allUsers();
  14.     });
  15. });
  16.  
  17. const url = 'https://maltemagnussen.com/CA2/api/search/';
  18. const testurl = 'http://localhost:8080/CA2/api/search/';
  19.  
  20. function handleHttpErrors(res) {
  21.     if (!res.ok) {
  22.         return Promise.reject({ status: res.status, fullError: res.json() })
  23.     }
  24.     return res.json();
  25. }
  26.  
  27. /*---------------------------------------------*/
  28. /*---------- Begin Get Person By Name ---------*/
  29. /*---------------------------------------------*/
  30.  
  31. function fillViewPersonWithDataDiv() {
  32.     emptyDiv('viewPersonWithData');
  33.     let ptag = document.createElement('p');
  34.     ptag.setAttribute('id', 'viewPersonWithDataPTAG');
  35.  
  36.     let inputtag = document.createElement('input');
  37.     inputtag.setAttribute('id', 'viewPersonWithDataInputTAG');
  38.     inputtag.setAttribute('type', 'text');
  39.     inputtag.setAttribute('placeholder', 'UserName');
  40.  
  41.     let buttontag = document.createElement('button');
  42.     buttontag.innerHTML = 'Get User';
  43.     buttontag.setAttribute('id', 'viewPersonWithDataButtonTAG');
  44.  
  45.     let div = document.getElementById('viewPersonWithData');
  46.     div.appendChild(inputtag);
  47.     div.appendChild(buttontag);
  48.     div.appendChild(ptag);
  49. }
  50.  
  51. /*---- To clear the div of data ---*/
  52. function emptyDiv(divID) {
  53.     let div = document.getElementById(divID);
  54.     div.innerHTML = "";
  55. }
  56.  
  57. function singleuser() {
  58.     let username = document.getElementById('viewPersonWithDataInputTAG').value;
  59.     if (!username) {
  60.         document.getElementById('viewPersonWithDataPTAG').innerHTML = 'Type in a name'
  61.     }
  62.     else {
  63.         let urlName = url + 'person/' + username;
  64.         fetch(urlName)
  65.             .then(handleHttpErrors)
  66.             .then(fetchedData => {
  67.                 document.getElementById('viewPersonWithDataPTAG').innerHTML = writeToPTagPrPerson(fetchedData[0]);
  68.             })
  69.             .catch(err => {
  70.                 if (err.status) {
  71.                     err.fullError.then(e => console.log(e.detail))
  72.                 }
  73.                 else { console.log("Network error"); }
  74.             });
  75.     }
  76. }
  77.  
  78. function writeToPTagPrPerson(jsondata) {
  79.     let hobbies = '';
  80.     jsondata['hobbies'].forEach(element => {
  81.         hobbies = hobbies + '<br>' + element.name + ' - ' + element.description;
  82.     });
  83.     let phones = '';
  84.     jsondata['phones'].forEach(element => {
  85.         phones = phones + '<br>' + element.description + ': ' + element.number;
  86.     });
  87.  
  88.     let stringToWrite =
  89.         "<br>Firstname: " + jsondata['firstName'] + ' ' + jsondata['lastName']
  90.         + "<br>e-mail: " + jsondata['email']
  91.         + "<br>Address: " + jsondata['address']['street'] + ', '
  92.         + jsondata['address']['additionalInfo'] + ', ' + jsondata['address']['cityInfo']['zipCode']
  93.         + ' ' + jsondata['address']['cityInfo']['city']
  94.         + "<br>Hobbies: " + hobbies
  95.         + "<br>Phones: " + phones;
  96.     return stringToWrite;
  97. }
  98.  
  99. /*---------------------------------------------*/
  100. /*----------- End Get Person By Name ----------*/
  101. /*---------------------------------------------*/
  102.  
  103.  
  104. /*---------------------------------------------*/
  105. /*----------- Begin Get All Persons -----------*/
  106. /*---------------------------------------------*/
  107.  
  108. function fillViewAllPersonsWithDataDiv() {
  109.     emptyDiv('viewAllPersonsWithData');
  110.     let ptag = document.createElement('p');
  111.     ptag.setAttribute('id', 'viewAllPersonsWithDataPTAG');
  112.  
  113.     let buttontag = document.createElement('button');
  114.     buttontag.innerHTML = 'Get All Users';
  115.     buttontag.setAttribute('id', 'viewAllPersonsWithDataButtonTAG');
  116.  
  117.     let div = document.getElementById('viewAllPersonsWithData');
  118.     div.appendChild(buttontag);
  119.     div.appendChild(ptag);
  120. }
  121.  
  122. function allUsers() {
  123.     let urlAll = url + 'allpersons';
  124.     fetch(urlAll)
  125.         .then(handleHttpErrors)
  126.         .then(jsondata => {
  127.             let allPersonsToWrite = '';
  128.             for (let element in jsondata) {
  129.                 allPersonsToWrite = allPersonsToWrite + writeToPTagPrPerson(jsondata[element]);
  130.             }
  131.             document.getElementById('viewAllPersonsWithDataPTAG').innerHTML = allPersonsToWrite;
  132.         })
  133.         .catch(err => {
  134.             if (err.status) {
  135.                 err.fullError.then(e => console.log(e.detail))
  136.             }
  137.             else { console.log("Network error"); }
  138.         });
  139.  
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement