Ver código fonte

Refactor page assignment and add error handling in index.php

Alejandro Rosales 1 ano atrás
pai
commit
5dd35ee337
1 arquivos alterados com 65 adições e 16 exclusões
  1. 65 16
      index.php

+ 65 - 16
index.php

@@ -2,16 +2,13 @@
 require_once "dependencies.php";
 
 // Simplify the assignment of $page
-$page = 'host/';
-if (isset($_SESSION['page'])) {
-    $page = $_SESSION['page'];
-} elseif (isset($_SESSION['moodle_db'])) {
-    $page = 'menu/';
+$page = 'login';
+if (isset($_SESSION['moodle_db'])) {
+    $page = 'menu';
+} else if (isset($_SESSION['user'])) {
+    $page = 'host';
 }
-
-$moodle_db = isset($_SESSION['moodle_db']) ? connect($_SESSION['moodle_db']) : null;
-
-print_r($_SESSION);
+$moodle_db = isset($_SESSION['moodle_db']) ? makeConnection($_SESSION['moodle_db']) : null;
 ?>
 <!DOCTYPE html>
 <html lang="en">
@@ -23,6 +20,36 @@ print_r($_SESSION);
     <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha512-DTOQO9RWCH3ppGqcWaEA1BIZOC6xxalwEsw9c2QQeAIftl+Vegovlnee1c9QX4TctnWMn13TZye+giMm8e2LwA==" crossorigin="anonymous" referrerpolicy="no-referrer" />
     <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@1/css/pico.min.css">
     <script src="https://unpkg.com/petite-vue"></script>
+    <script>
+        const store = PetiteVue.reactive({
+            error: null,
+            loading: false,
+
+            async fetch(url, options = {}, custom_error = null) {
+                store.loading = true
+                const response = await fetch(url, options)
+                store.loading = false
+
+                if (!response.ok) {
+                    store.error = custom_error ?? {
+                        title: 'Error en la autorización',
+                        message: 'Puede ser que tu token haya expirado o que no tengas permisos para realizar esta acción',
+                        avoidable: false,
+                        actions: [{
+                            label: 'Cerrar sesión',
+                            handler: () => {
+                                window.location.href = '/action/desconectar.php?action=sign-out'
+                            },
+                            class: 'primary'
+                        }]
+                    }
+                    return
+                }
+
+                return response.json()
+            },
+        });
+    </script>
 </head>
 
 <body>
@@ -50,18 +77,40 @@ print_r($_SESSION);
         </div>
     </nav>
 
-    <dialog :open="loading ?? false">
-        <div class="grid">
-            <button aria-busy="true" class="secondary"></button>
-        </div>
-    </dialog>
 
-    <div class="container">
+
+    <div class="container" v-scope>
+        <dialog :open="store.loading">
+            <div class="grid">
+                <button aria-busy="true" class="secondary"></button>
+            </div>
+        </dialog>
+
+        <dialog :open="store.error !== null" v-if="store.error">
+            <article>
+                <header>
+                    <a href="#close" aria-label="Close" class="close" @click="store.error = null" v-if="store.error.avoidable"></a>
+                    <strong>
+                        {{ store.error.title }}
+                    </strong>
+                </header>
+                <p>
+                    {{ store.error.message }}
+
+                </p>
+                <br>
+                <div class="grid">
+                    <button v-for="action in store.error.actions" @click="action.handler" :class="action.class">
+                        {{ action.label }}
+                    </button>
+                </div>
+            </article>
+        </dialog>
         <?php
         if (!isset($page)) {
             throw new Exception('No se ha definido la variable $page');
         }
-        require "{$_SERVER['DOCUMENT_ROOT']}/pages/$page/index.php";
+        require "{$_SERVER['DOCUMENT_ROOT']}/pages/$page.html";
         ?>
     </div>