Attempt to resurrect qomet code - need some rewrite
[qomet.git] / public / javascripts / courseList.js
... / ...
CommitLineData
1new Vue({
2 el: '#courseList',
3 data: {
4 courseArray: courseArray,
5 newCourse: {
6 code: "",
7 description: "",
8 },
9 },
10 mounted: function() {
11 $('.modal').modal();
12 },
13 methods: {
14 redirect: function(code) {
15 document.location.href = "/" + initials + "/" + code;
16 },
17 addCourse: function() {
18 if (!admin)
19 return;
20 // modal, fill code and description
21 let error = Validator.checkObject({code:this.newCourse.code}, "Course");
22 if (!!error)
23 return alert(error);
24 else
25 $('#newCourse').modal('close');
26 $.ajax("/courses",
27 {
28 method: "POST",
29 data: this.newCourse,
30 dataType: "json",
31 success: res => {
32 if (!res.errmsg)
33 {
34 this.newCourse["code"] = "";
35 this.newCourse["description"] = "";
36 this.courseArray.push(res);
37 }
38 else
39 alert(res.errmsg);
40 },
41 }
42 );
43 },
44 deleteCourse: function(course) {
45 if (!admin)
46 return;
47 if (confirm("Delete course '" + course.code + "' ?"))
48 $.ajax("/courses",
49 {
50 method: "DELETE",
51 data: { cid: course._id },
52 dataType: "json",
53 success: res => {
54 if (!res.errmsg)
55 this.courseArray.splice( this.courseArray.findIndex( item => {
56 return item._id == course._id;
57 }), 1 );
58 else
59 alert(res.errmsg);
60 },
61 }
62 );
63 },
64 }
65});