carreras.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { createApp } from 'https://unpkg.com/petite-vue?module';
  2. const app = createApp({
  3. carreras: [],
  4. niveles: [],
  5. message: {},
  6. async setNivel(carrera, nivel) {
  7. if (carrera.nivel_id === nivel.nivel_id) {
  8. return;
  9. }
  10. carrera.nivel_id = nivel.nivel_id;
  11. carrera.nivel_nombre = nivel.nivel_nombre;
  12. await fetch('action/carrera.php', {
  13. method: 'PUT',
  14. body: JSON.stringify({
  15. carrera_id: carrera.carrera_id,
  16. nivel_id: nivel.nivel_id
  17. })
  18. })
  19. .then(res => res.json())
  20. .then(res => {
  21. this.message.title = "Actualización";
  22. this.message.text = res.error ?? res.success;
  23. this.message.type = res.error ? 'danger' : 'success';
  24. this.message.timestamp = new Date().toLocaleTimeString();
  25. });
  26. },
  27. async mounted() {
  28. this.carreras = await fetch('action/carrera.php').then(res => res.json());
  29. this.niveles = await fetch('action/nivel.php').then(res => res.json());
  30. // group by facultad_id
  31. const carreras = this.carreras.reduce((acc, cur) => {
  32. const { facultad_nombre } = cur;
  33. if (!acc[facultad_nombre]) {
  34. acc[facultad_nombre] = [];
  35. }
  36. acc[facultad_nombre].push(cur);
  37. return acc;
  38. }, {});
  39. this.carreras = Object.entries(carreras).map(([facultad_nombre, carreras]) => ({
  40. facultad_nombre: facultad_nombre,
  41. carreras
  42. }));
  43. }
  44. }).mount('#app');