puestos.js 2.6 KB

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