puestos.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { createApp } from 'https://unpkg.com/petite-vue?module';
  2. const app = createApp({
  3. message: null,
  4. puestos: [],
  5. carreras: [],
  6. materias: [],
  7. usuarios: [],
  8. async nuevoPuesto(nuevoPuesto) {
  9. try {
  10. const res = await fetch('action/puesto.php', {
  11. method: 'POST',
  12. body: JSON.stringify({
  13. puesto_nombre: nuevoPuesto
  14. })
  15. });
  16. const data = await res.json();
  17. this.puestos.push(data);
  18. // order by puesto.nombre
  19. this.puestos.sort((a, b) => a.nombre.localeCompare(b.nombre));
  20. }
  21. catch (error) {
  22. alert(`Error: ${error}`);
  23. }
  24. },
  25. to_delete: null,
  26. async eliminarPuesto(puesto_id) {
  27. try {
  28. const res = await fetch('action/puesto.php', {
  29. method: 'DELETE',
  30. body: JSON.stringify({
  31. puesto_id
  32. })
  33. });
  34. const data = await res.json();
  35. this.message = data.msg;
  36. // after 3 seconds, remove the message
  37. setTimeout(() => {
  38. this.message = null;
  39. }, 3000);
  40. this.puestos = this.puestos.filter((p) => p.puesto_id !== puesto_id);
  41. // order by puesto.nombre
  42. this.puestos.sort((a, b) => a.nombre.localeCompare(b.nombre));
  43. }
  44. catch (error) {
  45. alert(`Error: ${error}`);
  46. }
  47. },
  48. async actualizarPuesto(puesto_id, materias, usuario_id) {
  49. try {
  50. const res = await fetch('action/puesto.php', {
  51. method: 'PUT',
  52. body: JSON.stringify({
  53. puesto_id,
  54. materias: materias.map(m => m.materia_id),
  55. usuario_id
  56. })
  57. });
  58. const data = await res.json();
  59. this.message = data.msg;
  60. // after 3 seconds, remove the message
  61. setTimeout(() => {
  62. this.message = null;
  63. }, 3000);
  64. }
  65. catch (error) {
  66. alert(`Error: ${error}`);
  67. }
  68. },
  69. async mounted() {
  70. this.puestos = await fetch('action/puesto.php').then(res => res.json());
  71. this.carreras = await fetch('action/action_carreras.php').then(res => res.json());
  72. this.materias = await fetch('action/action_materias.php').then(res => res.json());
  73. this.usuarios = await fetch('action/usuarios.php').then(res => res.json());
  74. }
  75. }).mount('#app');