index.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <main class="container" v-if="page === 'host'" v-scope @vue:mounted="mounted">
  2. <h1>Conectar un HOST</h1>
  3. <form action="action/conectar_moodle.php" method="post" v-scope="{host: null}">
  4. <div class="grid">
  5. <?php
  6. if ($db->count('moodle_host') > 0) : ?>
  7. <label for="moodle-host">
  8. Moodle host
  9. <input list="moodle-hosts" name="moodle-host" placeholder="Moodle host" required v-model="host">
  10. </label>
  11. <?php else : ?>
  12. <p>No hay hosts registrados</p>
  13. <?php endif ?>
  14. <label for="agregar-host">
  15. Agregar host
  16. <button id="agregar-host" type="button" @click="page = 'new_host'">
  17. <i class="fas fa-plus"></i>
  18. </button>
  19. </label>
  20. </div>
  21. <?php if ($db->count('moodle_host') > 0) : ?>
  22. <datalist id="moodle-hosts" v-for="host in hosts">
  23. <option :value="host.host">
  24. {{ host.etiqueta }}
  25. </option>
  26. </datalist>
  27. <?php endif ?>
  28. <div class="grid">
  29. <button type="button" :disabled="!host" @click="editHost(host)">Editar <i class="fas fa-edit"></i></button>
  30. <button type="submit" :disabled="!host">Conectar <i class="fas fa-database"></i></button>
  31. </div>
  32. </form>
  33. </main>
  34. <div v-else-if="page === 'new_host'">
  35. <?php require 'new_host.php' ?>
  36. </div>
  37. <div v-else>
  38. <?php require 'edit_host.php' ?>
  39. </div>
  40. <script>
  41. PetiteVue.createApp({
  42. // state
  43. loading: false,
  44. page: 'host',
  45. // data
  46. hosts: [],
  47. current_host: null,
  48. async editHost(host) {
  49. this.loading = true
  50. const hosts = await fetch(`http://www.localhost:3000/moodle_host?etiqueta=eq.${host}`, {
  51. headers: {
  52. 'Prefer': 'plurality=singular'
  53. }
  54. })
  55. if (!hosts.ok) {
  56. this.loading = false
  57. alert('Error al obtener el host: ' + hosts.status)
  58. return
  59. }
  60. const data = await hosts.json()
  61. this.current_host = data[0]
  62. this.loading = false
  63. this.page = 'edit_host'
  64. },
  65. async mounted() {
  66. this.loading = true
  67. const hosts = await fetch('http://www.localhost:3000/moodle_host')
  68. const data = await hosts.json()
  69. this.loading = false
  70. this.hosts = data
  71. }
  72. }).mount()
  73. </script>