123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- import { createApp, reactive } from 'https://unpkg.com/petite-vue?module';
- const new_aviso = reactive({
- titulo: '',
- descripcion: '',
- fechaInicio: '',
- fechaFin: '',
- profesores: [],
- carreras: [],
- reset() {
- this.titulo = '';
- this.descripcion = '';
- this.fechaInicio = '';
- this.fechaFin = '';
- this.profesores = [];
- this.carreras = [];
- },
- get isValid() {
- return this.titulo !== '' && this.descripcion !== '' && this.fechaInicio !== '' && this.fechaFin !== '' && (this.profesores.length > 0 || this.carreras.length > 0) && this.facultad_id !== null;
- },
- });
- // define datepicker method
- const app = createApp({
- new_aviso,
- profesores: [],
- carreras: [],
- avisos: [],
- profesor: null,
- formatProfesor(profesor) {
- return `(${profesor.profesor_clave}) ${profesor.profesor_nombre}`;
- },
- addProfesor() {
- const profesorObj = this.profesores.find((profesor) => this.profesor === this.formatProfesor(profesor));
- if (profesorObj) {
- this.new_aviso.profesores.push(profesorObj);
- this.profesor = null;
- }
- },
- aviso_shown: null,
- // int?
- aviso_suspendido: null,
- suspenderAviso() {
- if (this.aviso_suspendido) {
- const aviso = this.avisos.find((aviso) => aviso.aviso_id === this.aviso_suspendido);
- if (aviso) {
- this.deleteAviso(aviso);
- }
- }
- },
- get relevant_profesores() {
- // not in array new_aviso.profesores
- const relevant = this.profesores.filter((profesor) => !this.new_aviso.profesores.map((profesor) => profesor.profesor_id).includes(profesor.profesor_id));
- // 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))
- return relevant;
- },
- get relevant_carreras() {
- // not in array new_aviso.carreras
- return this.carreras.filter((carrera) => !this.new_aviso.carreras.includes(carrera));
- },
- createAviso() {
- const data = {
- aviso_titulo: this.new_aviso.titulo,
- aviso_texto: this.new_aviso.descripcion,
- aviso_fecha_inicial: this.new_aviso.fechaInicio,
- aviso_fecha_final: this.new_aviso.fechaFin,
- profesores: this.new_aviso.profesores.map((profesor) => profesor.profesor_id),
- carreras: this.new_aviso.carreras.map((carrera) => carrera.carrera_id),
- };
- fetch('/action/avisos.php', {
- method: 'POST',
- body: JSON.stringify(data)
- }).then(res => res.json()).then(res => {
- if (res.success) {
- // hydrate with carreras and profesores
- this.avisos.push({
- ...data,
- carreras: this.carreras.filter((carrera) => data.carreras.includes(carrera.carrera_id)),
- profesores: this.profesores.filter((profesor) => data.profesores.includes(profesor.profesor_id)),
- aviso_estado: true,
- aviso_id: res.aviso_id,
- });
- this.new_aviso.reset();
- }
- else {
- alert(res.error);
- console.log(res.errors);
- }
- });
- },
- deleteAviso(aviso) {
- fetch(`/action/avisos.php`, {
- method: 'DELETE',
- body: JSON.stringify({ aviso_id: aviso.aviso_id })
- }).then(res => res.json()).then(res => {
- if (res.success) {
- this.avisos = this.avisos.filter((aviso) => aviso.aviso_id !== this.aviso_suspendido);
- this.aviso_suspendido = null;
- }
- else {
- alert(res.error);
- console.log(res.errors);
- }
- });
- },
- updateAviso() {
- fetch(`/action/avisos.php`, {
- method: 'PUT',
- body: JSON.stringify({
- aviso_id: this.aviso_shown.aviso_id,
- aviso_fecha_final: this.aviso_shown.aviso_fecha_final,
- })
- }).then(res => res.json()).then(res => {
- if (res.success) {
- }
- else {
- alert(res.error);
- console.log(res.errors);
- }
- });
- },
- async initializeDatepickers($el) {
- const periodo = await fetch('action/periodo_datos.php');
- const periodo_data = await periodo.json();
- $('.date-picker').datepicker({
- dateFormat: 'yy-mm-dd',
- maxDate: periodo_data.periodo_fecha_fin,
- minDate: 0,
- });
- $($el).on('change', () => {
- this.aviso_shown.aviso_fecha_final = $($el).val();
- });
- },
- async mounted() {
- this.avisos = await fetch("/action/avisos.php").then(res => res.json());
- this.profesores = await fetch('/action/action_profesor.php').then(res => res.json());
- this.carreras = await fetch('/action/action_carreras.php').then(res => res.json());
- await this.initializeDatepickers();
- const fechaInicio = $('#fechaInicio.date-picker');
- const fechaFin = $('#fechaFin.date-picker');
- fechaInicio.on("change", function () {
- new_aviso.fechaInicio = fechaInicio.val();
- fechaFin.datepicker("option", "minDate", fechaInicio.val());
- });
- fechaFin.on("change", function () {
- new_aviso.fechaFin = fechaFin.val();
- fechaInicio.datepicker("option", "maxDate", fechaFin.val());
- });
- }
- }).mount('#app');
|