Advertisement
miroLLL

State [1] to CATCHES

Mar 13th, 2020
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (() => {
  2.  
  3.     const elements = {
  4.         anglerInput: document.querySelector('#addForm input.angler'),
  5.         weightInput: document.querySelector('#addForm input.weight'),
  6.         speciesInput: document.querySelector('#addForm input.species'),
  7.         locationInput: document.querySelector('#addForm input.location'),
  8.         baitInput: document.querySelector('#addForm input.bait'),
  9.         captureTimeInput: document.querySelector('#addForm input.captureTime'),
  10.         addBtn: document.querySelector('#addForm button.add')
  11.     };
  12.  
  13.     const CREATE_URL = "https://fisher-game.firebaseio.com/catches.json";
  14.     const DELETE_URL = "https://fisher-game.firebaseio.com/catches/{catchId}.json";
  15.  
  16.     elements.addBtn.addEventListener('click', deleteCatch);
  17.  
  18.     async function addCatch() {
  19.  
  20.         let myCatch = {
  21.             angler: elements.anglerInput.value,
  22.             weight: elements.weightInput.value,
  23.             species: elements.speciesInput.value,
  24.             location: elements.locationInput.value,
  25.             bait: elements.baitInput.value,
  26.             captureTime: elements.captureTimeInput.value,
  27.         };
  28.  
  29.         const options = {
  30.             headers: {
  31.                 'Content-Type': 'application/json',
  32.             },
  33.             body: JSON.stringify(myCatch),
  34.             method: "POST"
  35.         };
  36.  
  37.         try {
  38.             const response = await fetch(CREATE_URL, options);
  39.             const data = await response.json();
  40.  
  41.             console.log(data);
  42.         } catch (e) {
  43.             console.error(e);
  44.         }
  45.  
  46.  
  47.  
  48.  
  49.     }
  50.  
  51.     async function deleteCatch() {
  52.  
  53.         const catchId = "-M2JVy1q_oxtLLgBhzhY";
  54.         const url = DELETE_URL.replace('{catchId}', catchId);
  55.  
  56.         try {
  57.  
  58.             const options = {
  59.                 headers: {
  60.                     'Content-Type': 'application/json'
  61.                 },
  62.                 method: 'DELETE'
  63.             };
  64.  
  65.             const response = await fetch(url, options); // return a PROMISE
  66.             const data = await response.json();
  67.            
  68.             console.log(data);
  69.  
  70.         } catch (e) {
  71.             console.error(e);
  72.         }
  73.  
  74.     }
  75.  
  76. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement