kstoyanov

3.Phonebook

Nov 2nd, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function attachEvents() {
  2.     const btnLoad = document.getElementById('btnLoad');
  3.     const btnCreate = document.getElementById('btnCreate');
  4.     const ul = document.getElementById('phonebook');
  5.     const inputPerson = document.getElementById('person');
  6.     const inputPhone = document.getElementById('phone');
  7.  
  8.     btnLoad.addEventListener('click', loadData);
  9.     btnCreate.addEventListener('click', addNewInfo);
  10.  
  11.     function deleteData() {
  12.         let headers = {
  13.             method: 'DELETE'
  14.         }
  15.         fetch(`https://phonebook-nakov.firebaseio.com/phonebook/${this.value}.json`, headers)
  16.             .then(loadData)
  17.             .catch(handleError)
  18.     }
  19.     function loadData() {
  20.         fetch('https://phonebook-nakov.firebaseio.com/phonebook.json')
  21.             .then(checkForErrors)
  22.             .then(res => res.json())
  23.             .then(checkForErrors)
  24.             .then(printInfo)
  25.             .catch(handleError)
  26.         function printInfo(data) {
  27.             ul.innerHTML = '';
  28.             Object.entries(data)
  29.                 .forEach(([id, info]) => {
  30.                     let li = document.createElement('li');
  31.                     li.textContent = `${info.person}: ${info.phone}`;
  32.  
  33.                     let btn = document.createElement('button');
  34.                     btn.textContent = 'Delete';
  35.                     btn.value = id;
  36.                     btn.addEventListener('click', deleteData)
  37.  
  38.                     li.appendChild(btn);
  39.                     ul.appendChild(li);
  40.                 })
  41.         }
  42.     }
  43.     function addNewInfo() {
  44.         let headers = {
  45.             method: 'POST',
  46.             headers: { 'Content-type': 'application/json' },
  47.             body: JSON.stringify({
  48.                 person: inputPerson.value,
  49.                 phone: inputPhone.value
  50.             })
  51.         }
  52.         fetch('https://phonebook-nakov.firebaseio.com/phonebook.json', headers)
  53.             .then(() => {
  54.                 inputPerson.value = '';
  55.                 inputPhone.value = '';
  56.                 loadData();
  57.             })
  58.             .catch(handleError)
  59.     }
  60.     function checkForErrors(res) {
  61.         if (res === null || res === undefined) {
  62.             throw new Error('Invalid JSON data!');
  63.         }
  64.         else if (res.ok === false) {
  65.             throw new Error(`${res.status} - ${res.statusText}`);
  66.         }
  67.         return res;
  68.     }
  69.     function handleError(err) {
  70.         console.error(err);
  71.     }
  72. }
  73. attachEvents();
Add Comment
Please, Sign In to add comment