divanov94

Untitled

Nov 1st, 2020
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function attachEvents() {
  2.     let adress = 'https://rest-messanger.firebaseio.com/messanger.json';
  3.     let refreshBtn = document.querySelector('#refresh');
  4.     refreshBtn.addEventListener('click', () => {
  5.         fetch(adress)
  6.             .then(response => response.json())
  7.             .then(data => {
  8.                 let reducedMsg = Object.values(data).reduce((a, x) => {
  9.                     a += `${x.author}: ${x.content}\n`;
  10.                 }, '')
  11.                 let msgElem = document.querySelector('#messages')
  12.                 msgElem.textContent = reducedMsg;
  13.  
  14.             })
  15.     })
  16.  
  17.     let enterBtn = document.querySelector('#submit');
  18.     enterBtn.addEventListener('click', () => {
  19.         let author = document.querySelector('#author');
  20.         let content = document.querySelector('#content');
  21.         fetch(adress, {
  22.             method: 'POST',
  23.             body: JSON.stringify({
  24.                 author: author.value,
  25.                 content: content.value,
  26.             })
  27.         });
  28.         author.value = '';
  29.         content.value = '';
  30.  
  31.     });
  32. }
  33.  
  34. attachEvents();
Add Comment
Please, Sign In to add comment