Advertisement
Silviya7

VacationSchedule

May 14th, 2024
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const baseURL='http://localhost:3030/jsonstore/tasks';
  2. const btnLoad= document.getElementById('load-vacations');
  3.  
  4. const listList= document.getElementById('list');
  5. const BtnAddVacation= document.getElementById('add-vacation');
  6. const BtnEditVacation= document.getElementById('edit-vacation');
  7.  
  8. const Elementname= document.getElementById('name');
  9. const Elementdays=document.getElementById('num-days');
  10. const Elementdate=document.getElementById('from-date');
  11.  
  12. const DoneBtn= document.getElementById('done-btn');
  13.  
  14. let idvacation;
  15.  
  16.     const LoadVacations= async()=> {
  17.         const response = await fetch(baseURL);
  18.         const data= await response.json();
  19.  
  20.         console.log(Object.values(data));
  21.         listList.innerHTML='';
  22.        for (const vac of Object.values(data)) {
  23.  
  24.         const Elementh1=document.createElement('h2');
  25.         Elementh1.textContent=vac.name;
  26.         const Elementh2=document.createElement('h3');
  27.         Elementh2.textContent=vac.date;
  28.         const Elementh3=document.createElement('h3');
  29.         Elementh3.textContent=vac.days;
  30.  
  31.         const btnChange=document.createElement('button');
  32.         btnChange.classList.add('change-btn');
  33.         btnChange.textContent='Change';
  34.  
  35.         const btnDone=document.createElement('button');
  36.         btnDone.classList.add('done-btn');
  37.         btnDone.textContent='Done';
  38.  
  39.         const Containerdiv=document.createElement('div');
  40.         Containerdiv.classList.add('container');
  41.  
  42.         Containerdiv.appendChild(Elementh1);
  43.         Containerdiv.appendChild(Elementh2);
  44.         Containerdiv.appendChild(Elementh3);
  45.         Containerdiv.appendChild(btnChange);
  46.         Containerdiv.appendChild(btnDone);
  47.  
  48.         listList.appendChild(Containerdiv);
  49.  
  50.         btnChange.addEventListener('click',()=>{
  51.             Containerdiv.remove();
  52.  
  53.             Elementname.value=vac.name;
  54.             Elementdays.value=vac.days;
  55.             Elementdate.value= vac.date;
  56.  
  57.             BtnEditVacation.removeAttribute('disabled');
  58.             BtnAddVacation.setAttribute('disabled','disabled');
  59.             idvacation=vac._id;
  60.         });
  61.  
  62.        
  63.  
  64.         btnDone.addEventListener('click',async()=>{
  65.  
  66.       const response= await fetch(`${baseURL}/${vac._id}`,{
  67.         method:'DELETE'
  68.      });
  69.  
  70.      Containerdiv.remove();
  71. })      
  72.    }
  73.     }
  74.  
  75.  
  76.     btnLoad.addEventListener('click',LoadVacations);
  77.  
  78.     BtnAddVacation.addEventListener('click',async(e)=>{
  79.         e.preventDefault();
  80.         const name= Elementname.value;
  81.         const days= Elementdays.value;
  82.         const date= Elementdate.value;
  83.  
  84.         const response= await fetch(baseURL, {
  85.             method :'POST',
  86.             headers:{
  87.             'content-type':'application/json',
  88.             },
  89.             body: JSON.stringify({name,days,date }),
  90.             });
  91.  
  92.             await  LoadVacations();
  93.  
  94.             Elementname.value="";
  95.             Elementdays.value="";
  96.             Elementdate.value="";
  97.     })
  98.  
  99.     BtnEditVacation.addEventListener('click',async(e)=>{
  100.         e.preventDefault();
  101.         const name= Elementname.value;
  102.         const days= Elementdays.value;
  103.         const date= Elementdate.value;
  104.      
  105.        
  106.     //make put request
  107.     const response= await fetch(`${baseURL}/${idvacation}`,{
  108.         method:'PUT',
  109.         headers:{
  110.           'content-type':'application/json',
  111.         },
  112.    
  113.         body:JSON.stringify({
  114.           _id: idvacation,
  115.          name,
  116.          days,
  117.          date
  118.    
  119.       })
  120.     });
  121.  
  122.  
  123.     LoadVacations();
  124.     BtnAddVacation.removeAttribute('disabled');
  125.     BtnEditVacation.setAttribute('disabled','disabled');
  126. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement