puestos.ts 3.0 KB

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