vueSpecialty.js 2.9 KB

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