host.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. <div v-if="hosts.length > 0">
  6. <label for="moodle-host">
  7. Moodle host
  8. <input list="moodle-hosts" name="moodle-host" placeholder="Moodle host" required
  9. v-model="selectedHost">
  10. </label>
  11. <datalist id="moodle-hosts" v-for="host in hosts">
  12. <option :value="host.host">
  13. {{ host.etiqueta }}
  14. </option>
  15. </datalist>
  16. </div>
  17. <div v-else>
  18. <p>No hay hosts registrados</p>
  19. </div>
  20. <label for="agregar-host">
  21. Agregar host
  22. <button id="agregar-host" type="button" @click="page = 'current_host'; selectedHost = null">
  23. <i class="fas fa-plus"></i>
  24. </button>
  25. </label>
  26. </div>
  27. <div class="grid">
  28. <button type="button" :disabled="hosts.filter(host => host.host === selectedHost).length === 0"
  29. @click="page = 'current_host'">Editar <i class="fas fa-edit"></i>
  30. </button>
  31. <button type="submit" :disabled="hosts.filter(host => host.host === selectedHost).length === 0">
  32. Conectar <i class="fas fa-database"></i>
  33. </button>
  34. </div>
  35. </form>
  36. </main>
  37. <div v-else @vue:mounted="selectHost">
  38. <main class="container">
  39. <h1>Registrar un nuevo HOST de Moodle</h1>
  40. <button class="btn btn-primary" @click="page = 'host'">
  41. Regresar
  42. <i class="fas fa-arrow-left"></i>
  43. </button>
  44. <form action="/action/new_host.php" method="post" @submit.prevent="newHost" id="new_host">
  45. <div class="grid">
  46. <label for="etiqueta">
  47. Etiqueta
  48. <input type="text" name="etiqueta" placeholder="Etiqueta para identificar el host" required
  49. :value="current_host.etiqueta" v-model="current_host.etiqueta" autocomplete="off">
  50. <small>Etiqueta para identificar: <code>Moodle2023A</code></small>
  51. </label>
  52. <label for="base_datos">
  53. Base de datos
  54. <input type="text" name="base_datos" placeholder="Nombre de la base de datos" required
  55. :value="current_host.postgres_dbname" v-model="current_host.postgres_dbname">
  56. <small>Ejemplo: <code>moodle42licdb</code></small>
  57. </label>
  58. </div>
  59. <div class="grid">
  60. <label for="host">
  61. Host de Moodle
  62. <input type="text" name="host" placeholder="200.13.89.000" required :value="current_host.host"
  63. v-model="current_host.host">
  64. <small>localhost, moodleXYZ.lci.ulsa.mx, 200.13.89.000</small>
  65. </label>
  66. <label for="puerto">
  67. Puerto de la base de datos
  68. <!-- validate only numbers -->
  69. <input type="text" name="puerto" placeholder="5432" required value="5432" pattern="[0-9]+"
  70. :value="current_host.puerto" v-model="current_host.puerto">
  71. </label>
  72. </div>
  73. <div class="grid">
  74. <label for="usuario">
  75. Usuario de Postgres
  76. <input type="text" name="usuario" placeholder="postgres" required value="postgres"
  77. :value="current_host.postgres_user" v-model="current_host.postgres_user">
  78. </label>
  79. <label for="password">
  80. Contraseña de Postgres
  81. <input type="password" name="password" placeholder="Contraseña del usuario postgres" required
  82. v-model="current_host.postgres_password">
  83. </label>
  84. </div>
  85. <div class="grid">
  86. <label for="periodos">Which periodos would you like to order?
  87. <select id="periodos" name="periodos" multiple required v-model="current_host.periodos_gema">
  88. <option v-for="periodo in periodos" :value="periodo.Periodo_id"
  89. :selected="current_host.periodos_gema.includes(periodo.Periodo_id)">
  90. {{ periodo.Periodo_desc }} de {{ periodo.Nivel_desc }}
  91. </option>
  92. </select>
  93. </label>
  94. </div>
  95. <div class="grid">
  96. <button type="submit">
  97. Registrar
  98. <i class="fas fa-database"></i>
  99. </button>
  100. </div>
  101. </form>
  102. </main>
  103. </div>
  104. <script>
  105. PetiteVue.createApp({
  106. store,
  107. // state
  108. page: 'host',
  109. // data
  110. hosts: [],
  111. periodos: [],
  112. selectedHost: null,
  113. current_host: {},
  114. reset() {
  115. this.current_host = {
  116. etiqueta: null,
  117. postgres_dbname: null,
  118. host: null,
  119. puerto: 5432,
  120. postgres_user: 'postgres',
  121. periodos_gema: []
  122. }
  123. },
  124. async selectHost() {
  125. if (this.selectedHost) {
  126. const data = await store.fetch(`http://www.localhost:3000/moodle_host?host=eq.${this.selectedHost}`, {
  127. headers: {
  128. 'Prefer': 'plurality=singular'
  129. }
  130. })
  131. this.current_host = data[0]
  132. this.current_host.postgres_password = '';
  133. }
  134. else {
  135. this.reset()
  136. }
  137. },
  138. async newHost() {
  139. try {
  140. const response = await fetch('http://www.localhost:3000/rpc/new_host', {
  141. method: 'POST',
  142. headers: {
  143. 'Content-Type': 'application/json',
  144. Authorization: `Bearer ${store.token}`
  145. },
  146. body: JSON.stringify({
  147. etiqueta_param: this.current_host.etiqueta,
  148. host_param: this.current_host.host,
  149. puerto_param: this.current_host.puerto,
  150. postgres_user_param: this.current_host.postgres_user,
  151. postgres_dbname_param: this.current_host.postgres_dbname,
  152. postgres_password_param: this.current_host.postgres_password,
  153. periodos_gema_param: `{${this.current_host.periodos_gema.join(',')}}`,
  154. moodle_host_id_param: this.current_host.moodle_host_id ?? null,
  155. }),
  156. })
  157. } catch (error) {
  158. store.error = {
  159. title: 'Error al registrar el host',
  160. message: 'Ocurrió un error al registrar el host',
  161. avoidable: true,
  162. }
  163. }
  164. store.error = {
  165. title: 'Host registrado',
  166. message: 'El host se ha registrado correctamente',
  167. avoidable: true,
  168. }
  169. this.reset()
  170. },
  171. async mounted() {
  172. this.reset()
  173. this.hosts = await store.fetch('http://www.localhost:3000/moodle_host')
  174. this.periodos = await store.fetch('/fetch/periodos.php')
  175. }
  176. }).mount()
  177. </script>