Advertisement
didkoslawow

Untitled

Apr 9th, 2023 (edited)
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function attachEvents() {
  2.   document.getElementById('btnLoad').addEventListener('click', onLoad);
  3.   document.getElementById('phonebook').addEventListener('click', onDelete);
  4.   document.getElementById('btnCreate').addEventListener('click', onCreate);
  5.   onLoad();
  6. }
  7.  
  8. attachEvents();
  9.  
  10. function onCreate() {
  11.   const personInput = document.getElementById('person');
  12.   const phoneInput = document.getElementById('phone');
  13.  
  14.   const person = personInput.value;
  15.   const phone = phoneInput.value;
  16.  
  17.   if (person && phone) {
  18.     addContact({ person, phone });
  19.     onLoad();
  20.  
  21.     personInput.value = '';
  22.     phoneInput.value = '';
  23.   }
  24. }
  25.  
  26. async function onDelete(e) {
  27.   if (e.target.tagName === 'BUTTON') {
  28.    await deleteContact(e.target.parentElement.id);
  29.     e.target.parentElement.remove();
  30.   }
  31. }
  32.  
  33. async function onLoad() {
  34.   const ulElement = document.getElementById('phonebook');
  35.  
  36.   const contacts = await loadContacts();
  37.  
  38.   const res = Object.entries(contacts).map(([id, contact]) =>
  39.     createElement(id, contact)
  40.   );
  41.  
  42.   ulElement.replaceChildren(...res);
  43. }
  44.  
  45. function createElement(id, contact) {
  46.   const li = document.createElement('li');
  47.   const btn = document.createElement('button');
  48.  
  49.   li.id = id;
  50.   li.textContent = `${contact.person}: ${contact.phone}`;
  51.   btn.textContent = 'Delete';
  52.  
  53.   li.appendChild(btn);
  54.  
  55.   return li;
  56. }
  57.  
  58. async function request(url, options) {
  59.   if (options && options.body !== undefined) {
  60.     Object.assign(options, {
  61.       headers: {
  62.         'Content-Type': 'application/json',
  63.       },
  64.     });
  65.   }
  66.  
  67.     const response = await fetch(url, options);
  68.  
  69.     if (response.ok !== true) {
  70.       const error = await response.json();
  71.       alert(error.message);
  72.       throw new Error(error.message);
  73.     }
  74.  
  75.     const data = await response.json();
  76.  
  77.     return data;
  78. }
  79.  
  80. async function loadContacts() {
  81.   const phones = await request('http://localhost:3030/jsonstore/phonebook/');
  82.  
  83.   return phones;
  84. }
  85.  
  86. async function addContact(contact) {
  87.   const res = await request('http://localhost:3030/jsonstore/phonebook', {
  88.     method: 'POST',
  89.     body: JSON.stringify(contact),
  90.   });
  91.  
  92.   return res;
  93. }
  94.  
  95. async function deleteContact(id) {
  96.   const res = await request('http://localhost:3030/jsonstore/phonebook/' + id, {
  97.     method: 'DELETE',
  98.   });
  99.  
  100.   return res;
  101. }
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement