vueSpecialty.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. const requestFunc = async(url, method = "GET", data = null, token = null) => {
  2. apihost = 'http://schedule.tomtit.tomsk.ru/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. specialties:[],
  29. selectedRow: {
  30. "Code": "",
  31. "Name": "",
  32. "IDDuration": 0,
  33. "ID": ""
  34. },
  35. requestType: null
  36. },
  37. methods: {
  38. async getSpecialties(){
  39. this.specialties = await requestFunc("/specialty", "GET")
  40. },
  41. selectRow(row, index){
  42. let rows = document.querySelectorAll('.clickHover')
  43. this.selectedRow = row;
  44. // empty string
  45. if(rows[index].style.backgroundColor != 'red'){
  46. rows[index].style.backgroundColor = 'red'
  47. }
  48. else{
  49. rows[index].style.backgroundColor = 'white'
  50. this.clearSelectedRow()
  51. }
  52. },
  53. clearSelectedRow(){
  54. this.selectedRow = {
  55. "Code": "",
  56. "Name": "",
  57. "IDDuration": 0
  58. }
  59. },
  60. changeRequestType(requestType){
  61. this.requestType = requestType
  62. },
  63. modalClick(add, requestType) {
  64. if(add==='yes') {
  65. this.clearSelectedRow();
  66. }
  67. this.changeRequestType(requestType);
  68. }
  69. },
  70. mounted() {
  71. this.getSpecialties()
  72. }
  73. });
  74. addSpecialty = (requestType)=>{
  75. let code = document.querySelector('.code').value
  76. let name = document.querySelector('.name').value
  77. let duration = document.querySelectorAll('.duration-radio');
  78. let radioValue
  79. for(let i=0; i<duration.length; i++ ){
  80. if (duration[i].checked) {
  81. radioValue = duration[i].value
  82. }
  83. }
  84. var myHeaders = new Headers();
  85. myHeaders.append("Content-Type", "application/json");
  86. var raw = {"Code":code,"Name":name,"IDDuration":Number(radioValue)};
  87. if (vueApp.requestType=== 'PATCH') {
  88. let ID = document.querySelector('.ID').value
  89. console.log(ID)
  90. raw.ID= Number(ID)
  91. }
  92. requestFunc(url="/specialty/", method=vueApp.requestType, data=raw)
  93. alert("Добавлено")
  94. }