Skip to content
Snippets Groups Projects
Select Git revision
  • 5287344ded933ddedb2d3186c0d2e8bfb66e3bfc
  • main default protected
2 results

user.client.js

Blame
  • user.client.js 2.97 KiB
    let userlogin;
    let userpassword;
    let username;
    
    //const displayList = require('./utils');
    
    const setup = () => {
      username = document.getElementById('username');
      getUser();
      // document.getElementById('update').addEventListener('click', update);
      displayList();
      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 displayList = 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;
    node.textContent = `${show.description} (${show.places})`;
    //
    //node.addEventListener('mouseover', () => getTask(show._id));
    //
    const ticketsButton = document.createElement('button');
    ticketsButton.className = 'ticket';
    ticketsButton.addEventListener('click', ajoutTicketShow);
    ticketsButton.textContent = '+1 tickets';
    node.appendChild(ticketsButton);
    //
    list.appendChild(node);
    }
    
    const update =  async () => {
      const userData = { name : username.value };
      const body = JSON.stringify(userData);
      const requestOptions = {
                             method :'PUT',
                             headers : { "Content-Type": "application/json" },
                             body : body
                           };
      const response = await fetch('/user/me', requestOptions);
      if (response.ok) {
        const updatedUser = await response.json();