avisos.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import { createApp, reactive } from 'https://unpkg.com/petite-vue?module';
  2. const new_aviso = reactive({
  3. titulo: '',
  4. descripcion: '',
  5. fechaInicio: '',
  6. fechaFin: '',
  7. profesores: [],
  8. carreras: [],
  9. reset() {
  10. this.titulo = '';
  11. this.descripcion = '';
  12. this.fechaInicio = '';
  13. this.fechaFin = '';
  14. this.profesores = [];
  15. this.carreras = [];
  16. },
  17. get isValid() {
  18. return this.titulo !== '' && this.descripcion !== '' && this.fechaInicio !== '' && this.fechaFin !== '' && (this.profesores.length > 0 || this.carreras.length > 0) && this.facultad_id !== null;
  19. },
  20. });
  21. // define datepicker method
  22. const app = createApp({
  23. new_aviso,
  24. profesores: [],
  25. carreras: [],
  26. avisos: [],
  27. profesor: null,
  28. formatProfesor(profesor) {
  29. return `(${profesor.profesor_clave}) ${profesor.profesor_nombre}`;
  30. },
  31. addProfesor() {
  32. const profesorObj = this.profesores.find((profesor) => this.profesor === this.formatProfesor(profesor));
  33. if (profesorObj) {
  34. this.new_aviso.profesores.push(profesorObj);
  35. this.profesor = null;
  36. }
  37. },
  38. aviso_shown: null,
  39. // int?
  40. aviso_suspendido: null,
  41. suspenderAviso() {
  42. if (this.aviso_suspendido) {
  43. const aviso = this.avisos.find((aviso) => aviso.aviso_id === this.aviso_suspendido);
  44. if (aviso) {
  45. this.deleteAviso(aviso);
  46. }
  47. }
  48. },
  49. get relevant_profesores() {
  50. // not in array new_aviso.profesores
  51. const relevant = this.profesores.filter((profesor) => !this.new_aviso.profesores.map((profesor) => profesor.profesor_id).includes(profesor.profesor_id));
  52. // console.log('profesores:', this.profesores.map((profesor: Profesor) => profesor.profesor_nombre), 'relevant:', relevant.map((profesor: Profesor) => profesor.profesor_nombre), 'new_aviso:', this.new_aviso.profesores.map((profesor: Profesor) => profesor.profesor_nombre))
  53. return relevant;
  54. },
  55. get relevant_carreras() {
  56. // not in array new_aviso.carreras
  57. return this.carreras.filter((carrera) => !this.new_aviso.carreras.includes(carrera));
  58. },
  59. createAviso() {
  60. const data = {
  61. aviso_titulo: this.new_aviso.titulo,
  62. aviso_texto: this.new_aviso.descripcion,
  63. aviso_fecha_inicial: this.new_aviso.fechaInicio,
  64. aviso_fecha_final: this.new_aviso.fechaFin,
  65. profesores: this.new_aviso.profesores.map((profesor) => profesor.profesor_id),
  66. carreras: this.new_aviso.carreras.map((carrera) => carrera.carrera_id),
  67. };
  68. fetch('/action/avisos.php', {
  69. method: 'POST',
  70. body: JSON.stringify(data)
  71. }).then(res => res.json()).then(res => {
  72. if (res.success) {
  73. // hydrate with carreras and profesores
  74. this.avisos.push({
  75. ...data,
  76. carreras: this.carreras.filter((carrera) => data.carreras.includes(carrera.carrera_id)),
  77. profesores: this.profesores.filter((profesor) => data.profesores.includes(profesor.profesor_id)),
  78. aviso_estado: true,
  79. aviso_id: res.aviso_id,
  80. });
  81. this.new_aviso.reset();
  82. }
  83. else {
  84. alert(res.error);
  85. console.log(res.errors);
  86. }
  87. });
  88. },
  89. deleteAviso(aviso) {
  90. fetch(`/action/avisos.php`, {
  91. method: 'DELETE',
  92. body: JSON.stringify({ aviso_id: aviso.aviso_id })
  93. }).then(res => res.json()).then(res => {
  94. if (res.success) {
  95. this.avisos = this.avisos.filter((aviso) => aviso.aviso_id !== this.aviso_suspendido);
  96. this.aviso_suspendido = null;
  97. }
  98. else {
  99. alert(res.error);
  100. console.log(res.errors);
  101. }
  102. });
  103. },
  104. updateAviso() {
  105. fetch(`/action/avisos.php`, {
  106. method: 'PUT',
  107. body: JSON.stringify({
  108. aviso_id: this.aviso_shown.aviso_id,
  109. aviso_fecha_final: this.aviso_shown.aviso_fecha_final,
  110. })
  111. }).then(res => res.json()).then(res => {
  112. if (res.success) {
  113. }
  114. else {
  115. alert(res.error);
  116. console.log(res.errors);
  117. }
  118. });
  119. },
  120. async initializeDatepickers($el) {
  121. const periodo = await fetch('action/periodo_datos.php');
  122. const periodo_data = await periodo.json();
  123. $('.date-picker').datepicker({
  124. dateFormat: 'yy-mm-dd',
  125. maxDate: periodo_data.periodo_fecha_fin,
  126. minDate: 0,
  127. });
  128. $($el).on('change', () => {
  129. this.aviso_shown.aviso_fecha_final = $($el).val();
  130. });
  131. },
  132. async mounted() {
  133. this.avisos = await fetch("/action/avisos.php").then(res => res.json());
  134. this.profesores = await fetch('/action/action_profesor.php').then(res => res.json());
  135. this.carreras = await fetch('/action/action_carreras.php').then(res => res.json());
  136. await this.initializeDatepickers();
  137. const fechaInicio = $('#fechaInicio.date-picker');
  138. const fechaFin = $('#fechaFin.date-picker');
  139. fechaInicio.on("change", function () {
  140. new_aviso.fechaInicio = fechaInicio.val();
  141. fechaFin.datepicker("option", "minDate", fechaInicio.val());
  142. });
  143. fechaFin.on("change", function () {
  144. new_aviso.fechaFin = fechaFin.val();
  145. fechaInicio.datepicker("option", "maxDate", fechaFin.val());
  146. });
  147. }
  148. }).mount('#app');