Skip to content
Snippets Groups Projects
Select Git revision
  • 61af79547c9effcdbb8ca73d397b9df4c4d06497
  • main default protected
2 results

user.client.js

Blame
  • user.client.js 6.50 KiB
    let userlogin;
    let userpassword;
    let username;
    
    //const displayShowList = require('./utils');
    
    const setup = () => {
      username = document.getElementById('username');
      getUser();
      // document.getElementById('update').addEventListener('click', update);
      displayShowList();
      displayTicketsList();
      document.getElementById('logout').addEventListener('click', logout);
    }
    window.addEventListener('DOMContentLoaded', setup);
    
    const getUser = async () => {
      const requestOptions = {
                               method :'GET',
                             };
      const response = await fetch('/me', requestOptions);
      if (response.ok) {
        const user = await response.json();
        username.textContent = user.name;
      }
      else {
        const error = await response.json();
        handleError(error);
      }
    }
    
    const displayShowList = async () => {
      const requestOptions = {
                               method : 'GET'
                             };
      const response = await fetch('/admin/items', requestOptions)
      const allShows = await response.json();
    
      //(ici : code  exploitation de allShows)
      const list = document.getElementById('list');
      list.textContent = '';
      allShows.forEach( show => addToList(show, list) );
    }
    
    const addToList = (show,list) => {
    const node = document.createElement('div');
    node.id = show._id;
    const span = document.createElement('span');
    span.className = "places";
    span.textContent = `${show.places} places`
    node.textContent = `${show.description} : `;
    node.appendChild(span);
    //
    //node.addEventListener('mouseover', () => getTask(show._id));
    //
    const ticketsButton = document.createElement('button');
    ticketsButton.className = 'ticket';
    ticketsButton.addEventListener('click', addTicketShow);
    ticketsButton.textContent = '+1 tickets';
    node.appendChild(ticketsButton);
    //
    list.appendChild(node);
    }
    
    const update =  async (data) => {
      const body = JSON.stringify(data);
      const requestOptions = {
                             method :'PUT',
                             headers : { "Content-Type": "application/json" },
                             body : body
                           };
      const response = await fetch('/tickets', requestOptions);
      if (response.ok) {
        const updatedUser = await response.json();
        console.log(`show infos updated : ${JSON.stringify(updatedUser)}`);
      }
      else {
        const error = await response.json();
        handleError(error);
      }
    }
    
    const displayTicketsList = async () => {
      const requestOptions = {
                               method : 'GET'
                             };
      const response = await fetch('/tickets', requestOptions)
      const allTickets = await response.json();
      allTickets.forEach( ticket => addTicketsToList(ticket) );
    }
    
    const addTicketsToList = (ticket) => {
      const list = document.getElementById("showTickets");
      createDivTickets(ticket, list);
    } 
    
    const addTicketShow = async (event) => {
      const divShow = event.target.parentElement;
      const showId = divShow.id;
      const description = divShow.textContent.split(':')[0];
      const ticket = {show : showId, description : description, tickets : 1};
      const list = document.getElementById("showTickets");
    
      const nbTickets = addToTicketsList(ticket, list);
      ticket.tickets = nbTickets;
      await update(ticket);
    };
    
    const addToTicketsList = (ticket, list) => {
      let nbTickets;
      if(document.getElementById(ticket.show+"-t") === null) {
        createDivTickets(ticket, list);
        nbTickets = 1;
      }else {
        const sp = document.getElementById(ticket.show+"-t").querySelector(".tickets");
        sp.textContent = 1 + parseInt(sp.textContent);
        nbTickets = parseInt(sp.textContent);
      }
      return nbTickets;
    };
    
    
    const createDivTickets = (ticket, list) => {
      const showId = ticket.show + "-t";
      const node = document.createElement('div');
      node.id = showId;
      node.textContent = `${ticket.description}`;
      const span = document.createElement('span');
      span.className = "tickets";
      span.textContent = ticket.tickets;
      node.appendChild(span);
      const annulationButton = document.createElement('button');
      annulationButton.className = 'annulation';
      annulationButton.textContent = "Annuler";
      annulationButton.addEventListener("click", cancelTicketShow);
      node.appendChild(annulationButton);
      list.appendChild(node);
    }
    
    const cancelTicketShow = async (event) => {
      const list = document.getElementById("showTickets");
      const ticketDiv = event.target.parentElement;
      list.removeChild(ticketDiv);
      const showId = ticketDiv.id.split('-')[0];
      const body = JSON.stringify(showId);
      const requestOptions = {
                             method :'DELETE',
                           };
      const response = await fetch(`/${showId}`, requestOptions);
      if (response.ok) {
        const updatedUser = await response.json();
        console.log(`deleted ticket id : ${JSON.stringify(updatedUser)}`);
      }
      else {
        const error = await response.json();
        handleError(error);
      }
    }
    
    const logout = async () => {
      const requestOptions = {
                             method :'GET',
                           };
      const response = await fetch(`/access/logout`, requestOptions);
      if (response.ok) {
        window.location.href= '/';
      }
    }
    
    const handleError = error => {
      if (error.redirectTo)
        window.location.href= error.redirectTo;
      else
        console.log(`erreur : ${error.message}`);
    }
    
    
    
    
    
    
    
    
    // const update =  async (data) => {
    //   const body = JSON.stringify(data);
    //   const requestOptions = {
    //                          method :'PUT',
    //                          headers : { "Content-Type": "application/json" },
    //                          body : body
    //                        };
    //   const response = await fetch('/tickets', requestOptions);
    //   if (response.ok) {
    //     const updatedUser = await response.json();
    //     console.log(`show infos updated : ${JSON.stringify(updatedUser)}`);
    //   }
    //   else {
    //     const error = await response.json();
    //     handleError(error);
    //   }
    // }
    
    // const displayTicketsList = async () => {
    //   const requestOptions = {
    //                            method : 'GET'
    //                          };
    //   const response = await fetch('/tickets', requestOptions)
    //   const allTickets = await response.json();
    //   allTickets.forEach( ticket => addTicketsToList(ticket) );
    // }
    
    // const addTicketsToList = (ticket) => {
    //   const list = document.getElementById("showTickets");
    //   createDivTickets(ticket, list);
    // } 
    
    // const addTicketShow = async (event) => {
    //   const divShow = event.target.parentElement;
    //   const nbTickets = addToTicketsList(divShow);
    //   const ticketsData = {description : divShow.textContent.split(':')[0], tickets : nbTickets, show : divShow.id};
    //   await update(ticketsData);
    // };