Skip to content
Snippets Groups Projects
main.js 1.74 KiB
Newer Older
  • Learn to ignore specific revisions
  • import * as Auth from "/components/auth/auth.js";
    import * as Matcher from "/components/matcher/matcher.js";
    
    Nicolas Pernoud's avatar
    Nicolas Pernoud committed
    import * as Info from "/components/info/info.js";
    
    import * as Login from "./components/login/login.js";
    
    
    const mountPoint = document.getElementById("main");
    
    Nicolas PAGNY's avatar
    Nicolas PAGNY committed
    let user = {};
    
    
    document.addEventListener("DOMContentLoaded", async () => {
    
    Nicolas PAGNY's avatar
    Nicolas PAGNY committed
      user = await Auth.getUser()
    
      navigate();
    });
    
    async function navigate() {
    
      console
      if(user == undefined){
        console.log("test")
        load(mountPoint, async function() {
          await Login.mount("main");
        })
      }else{
        switch (location.pathname) {
          case "/auth":
            load(mountPoint, async function () {
              await Auth.mount("main");
            });
            break;
          case "/matcher":
            load(mountPoint, async function () {
              await Matcher.mount("main");
            });
            break;
          case "/mandatecreated":
            load(mountPoint, async function () {
              await Info.mount("main", "Le mandat a été créé avec succès !");
            });
            break;
          case "/callback":
            // Redirect to server callback according to cookie
            try {
              const callbackRoute = document.cookie
                .split("; ")
                .find((row) => row.startsWith("callbackRoute="))
                .split("=")[1];
              if (callbackRoute != "" && callbackRoute != null) {
                location.pathname = callbackRoute;
              }
            } catch (e) {
              console.error("no callback route was gotten from the cookie");
    
            break;
          default:
            location.pathname = "auth";
            break;
        }
    
      }
    }
    
    async function load(element, domAlteration) {
      // Start the alteration
      const alteration = domAlteration();
      await alteration; // Await for alteration end
    }