vueSchedule.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. schedule: [],
  29. scheduleForDay: null
  30. },
  31. methods: {
  32. getScheduleForDay(day){
  33. for(let i = 0; i < this.schedule.length;i++) {
  34. scheduleDay = this.schedule[i]
  35. if(scheduleDay[0].Day === day) {
  36. this.scheduleForDay = scheduleDay
  37. console.log(scheduleDay)
  38. }
  39. }
  40. },
  41. async generateSchedule() {
  42. this.schedule = await requestFunc('/schedule/generate', 'GET')
  43. while(true) {
  44. if(this.schedule !== null) {
  45. this.getScheduleForDay(1)
  46. break
  47. }
  48. }
  49. }
  50. }
  51. });