Skip to content
Snippets Groups Projects
users.js 2.81 KiB
Newer Older
  • Learn to ignore specific revisions
  • Alexis Poyen's avatar
    Alexis Poyen committed
    // Imports
    import * as Auth from "/services/auth/auth.js";
    import { Delete } from "/services/common/delete.js";
    import * as HandleUser from "/components/users/handleUser.js";
    
    // DOM elements
    
    // local variables
    let current_user;
    
    export async function mount(where) {
      const userPage = new Users();
      await userPage.mount(where);
    }
    
    class Users {
      constructor() {
      }
    
      async mount(where) {
        const mountpoint = where;
        document.getElementById(mountpoint).innerHTML = /* HTML */ `
          <div class="table-container">
            <table class="table is-bordered is-narrow is-hoverable is-fullwidth">
              <thead>
                <tr class="is-selected">
                  <th>Id</th>
                  <th>IdOAuth</th>
                  <th>Identifiant</th>
                  <th>Nom</th>
                  <th>Rôle</th>
                  <th>Actions</th>
                </tr>
              </thead>
              <tbody id="users"></tbody>
            </table>
          </div>
          <button id="users-new" class="button is-primary">
            <span class="icon is-small">
              <i class="fas fa-plus"></i>
            </span>
          </button>
    
          <div class="modal" id="users-modal"></div>
        `;
        current_user = await Auth.GetUser();
        this.HandleUser = await HandleUser.mount("users-modal", this);
        await this.displayUsers();
      }
    
      async displayUsers() {
        let users = await this.HandleUser.updateUsers();
        const markup = users.map((user) => this.userTemplate(user)).join("");
        document.getElementById("users").innerHTML = markup;
        let userHandler = this.HandleUser;
        users.map((user) => {
          document.getElementById(`users-user-edit-${user.id}`).addEventListener("click", function () {
            userHandler.editUser(user);
          });
          document.getElementById(`users-user-delete-${user.id}`).addEventListener("click", function () {
            new Delete(() => {
              userHandler.deleteUser(user);
            });
          });
        });
      }
    
      cleanUser(user) {
    
        let props = ["password", "name", "role"];
    
    Alexis Poyen's avatar
    Alexis Poyen committed
        for (const prop of props) {
          user[prop] = user[prop] === undefined ? "" : user[prop];
        }
      }
    
      userTemplate(user) {
        this.cleanUser(user);
        return /* HTML */ `
          <tr id="users-user-${user.id}">
            <td>${user.id}</td>
            <td>${user.idOAuth}</td>
            <td>${user.login}</td>
            <td>${user.name}</td>
            <td>${user.role}</td>
            <td>
              <a id="users-user-edit-${user.id}" class="button is-link is-small">
                <span>Modifier</span>
                <span class="icon is-small">
                  <i class="fas fa-pen"></i>
                </span>
              </a>
              <a id="users-user-delete-${user.id}" class="button is-danger is-small">
                <span>Supprimer</span>
                <span class="icon is-small">
                  <i class="fas fa-times"></i>
                </span>
              </a>
            </td>
          </tr>
        `;
      }