vueTeacher.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. const requestFunc = async(url, method = "GET", data = null, token = null) => {
  2. apihost = 'http://127.0.0.1/api'
  3. method = method.toLocaleUpperCase()
  4. let fullurl = `${apihost}${url}`;
  5. let options = {
  6. method: method,
  7. headers: {
  8. "Content-Type": "application/json",
  9. "Authorization": `Bearer ${token}`,
  10. },
  11. };
  12. switch(method) {
  13. case "PUT":
  14. delete options.headers["Content-Type"];
  15. options.body = data;
  16. break;
  17. case "POST": case "PATCH": case "DELETE":
  18. options.body = JSON.stringify(data);
  19. break;
  20. }
  21. const res = await fetch(fullurl, options);
  22. return await res.json();
  23. };
  24. var vueApp = new Vue({
  25. el: '#app',
  26. delimiters: ['${', '}'],
  27. data: {
  28. selected:null,
  29. teachers:[],
  30. classrooms:[]
  31. },
  32. methods: {
  33. showTeachers() {
  34. console.log(this.teachers)
  35. },
  36. async getTeachers(){
  37. this.teachers = await requestFunc("/teacher", "GET")
  38. },
  39. async getClassrooms(){
  40. classrooms = await requestFunc("/classroom", "GET")
  41. for(let i =0; i < classrooms.length; i++) {
  42. Vue.set(this.classrooms, i, classrooms[i]);
  43. }
  44. },
  45. async mountFunc(){
  46. await this.getTeachers()
  47. await this.getClassrooms()
  48. },
  49. },
  50. async mounted() {
  51. await this.mountFunc()
  52. console.log(this.classrooms)
  53. }
  54. });
  55. addTeacher = () =>{
  56. let surname = document.querySelector('.surname').value
  57. let name = document.querySelector('.name').value
  58. let patronymic = document.querySelector('.patronymic').value
  59. var select = document.getElementById("selectClassroom");
  60. var value = select.value;
  61. var myHeaders = new Headers();
  62. myHeaders.append("Content-Type", "application/json");
  63. var raw = {"Surname":surname,"Name":name,"IDCLassroom":Number(value),"Patronymic": patronymic};
  64. requestFunc(url="/teacher/", method="POST", data=raw)
  65. alert("Добавлено")
  66. }