-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
115 lines (114 loc) · 3.17 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
new Vue({
el: '#app',
data: {
products: [],
tempProduct: {
imageUrl: [],
},
pagination: {},
isNew: false,
user: {
token: '',
uuid: 'fb51b371-f18c-4c36-bc07-26304c376f66'
}
},
methods: {
getProducts(page=1) {
const api = `https://course-ec-api.hexschool.io/api/${this.user.uuid}/admin/ec/products?page=${page}`;
axios.get(api)
.then((res)=>{
this.products= res.data.data;
this.pagination = res.data.meta.pagination;
})
.catch((error)=>{
console.log(error);
});
},
openModel(method,item) {
if(method == 'createProduct') {
this.tempProduct = {
imageUrl: [],
};
this.isNew = true;
$('#exampleModal').modal('show');
}else if(method == 'updateProduct') {
this.tempProduct = JSON.parse(JSON.stringify(item));
this.isNew = false;
this.getProduct();
}else if(method == 'deleteProduct') {
this.tempProduct = JSON.parse(JSON.stringify(item));
$('#delProductModal').modal('show');
}
},
updateProduct() {
let api = `https://course-ec-api.hexschool.io/api/${this.user.uuid}/admin/ec/product`;
if(this.isNew) {
axios.post(api,this.tempProduct)
.then((res)=>{
this.getProducts();
this.tempProduct = {
imageUrl: [],
};
$('#exampleModal').modal('hide');
})
.catch((error)=>{
console.log(error);
});
}else {
api = `${api}/${this.tempProduct.id}`
axios.patch(api,this.tempProduct)
.then((res)=>{
console.log(res);
this.getProducts();
this.tempProduct = {
imageUrl: [],
};
$('#exampleModal').modal('hide');
})
.catch((error)=>{
console.log(error);
});
}
},
getProduct() {
let api = `https://course-ec-api.hexschool.io/api/${this.user.uuid}/admin/ec/product/${this.tempProduct.id}`;
axios.get(api)
.then((res)=>{
// this.tempProduct.description = res.data.data.description;
this.tempProduct = res.data.data;
// console.log(res);
$('#exampleModal').modal('show');
})
.catch((error)=>{
console.log(error);
});
},
deleteProduct() {
let api = `https://course-ec-api.hexschool.io/api/${this.user.uuid}/admin/ec/product/${this.tempProduct.id}`;
axios.delete(api)
.then((res)=>{
$('#delProductModal').modal('hide');
this.tempProduct = {
imageUrl: [],
};
this.getProducts();
})
.catch((error)=>{
console.log(error);
});
},
clearTemp() {
this.tempProduct = {
imageUrl: [],
};
}
},
created() {
this.user.token = document.cookie.replace(/(?:(?:^|.*;\s*)hexToken\s*\=\s*([^;]*).*$)|^.*$/, "$1");
axios.defaults.headers.common['Authorization'] = `Bearer ${this.user.token}`;
if(this.user.token == '') {
window.location = 'login.html';
}
this.getProducts();
}
});