vueSpecialty.js 3.3 KB

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