|
1 | 1 | document.addEventListener('DOMContentLoaded', function() { |
2 | 2 |
|
3 | 3 | let productListTemplate = document.querySelector('.productListTemplate'); |
| 4 | + let categoryListTemplate = document.querySelector('.categoryListTemplate'); |
| 5 | + |
| 6 | + let categoryListTemplateInstance = Handlebars.compile(categoryListTemplate.innerHTML); |
4 | 7 | let productListTemplateInstance = Handlebars.compile(productListTemplate.innerHTML); |
| 8 | + |
5 | 9 | let productsElem = document.querySelector('.products'); |
| 10 | + let categoriesElem = document.querySelector('.categories'); |
| 11 | + |
| 12 | + let productNameElem = document.querySelector('.productName'); |
| 13 | + let priceElem = document.querySelector('.price'); |
| 14 | + let categoryIdElem = document.querySelector('.categoryId'); |
| 15 | + let productBtn = document.querySelector('.addProductBtn'); |
6 | 16 |
|
7 | 17 | // Client side Factory function |
8 | 18 | let productService = ProductService(); |
9 | 19 |
|
10 | | - productService |
| 20 | + function showCategoryDropdown() { |
| 21 | + axios |
| 22 | + .get('/api/categories') |
| 23 | + .then(function(results){ |
| 24 | + let response = results.data; |
| 25 | + let data = response.data; |
| 26 | + let html = categoryListTemplateInstance({ |
| 27 | + categoryList: data |
| 28 | + }); |
| 29 | + categoriesElem.innerHTML = html; |
| 30 | + }); |
| 31 | + } |
| 32 | + |
| 33 | + function showProducts() { |
| 34 | + productService |
11 | 35 | .getProducts() |
12 | 36 | .then(function(results){ |
13 | 37 | let response = results.data; |
14 | 38 | let data = response.data; |
15 | | - // console.log(data); |
16 | | - let productTableHTML = productListTemplateInstance({ |
| 39 | + let html = productListTemplateInstance({ |
17 | 40 | productList : data |
18 | 41 | }); |
| 42 | + let productTableHTML = html; |
19 | 43 | productsElem.innerHTML = productTableHTML; |
20 | 44 | }); |
21 | | -}); |
| 45 | + } |
| 46 | + |
| 47 | + function clearFields() { |
| 48 | + productNameElem.value = ''; |
| 49 | + categoryIdElem.value = ''; |
| 50 | + priceElem.value= ''; |
| 51 | + } |
| 52 | + |
| 53 | + productBtn.addEventListener('click', function() { |
| 54 | + |
| 55 | + let description = productNameElem.value; |
| 56 | + let category_id = categoryIdElem.value; |
| 57 | + let price = priceElem.value; |
| 58 | + |
| 59 | + productService.addProduct({ |
| 60 | + category_id, |
| 61 | + description, |
| 62 | + price |
| 63 | + }) |
| 64 | + .then(function() { |
| 65 | + showProducts(); |
| 66 | + clearFields(); |
| 67 | + }) |
| 68 | + .catch(function(err){ |
| 69 | + alert(err); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + showCategoryDropdown(); |
| 74 | + showProducts(); |
22 | 75 |
|
| 76 | +}); |
23 | 77 |
|
24 | 78 | function ProductService() { |
25 | 79 | function getProducts(){ |
26 | 80 | return axios.get('/api/products') |
27 | 81 | } |
28 | 82 |
|
| 83 | + function addProduct(data) { |
| 84 | + return axios.post('/api/products', data) |
| 85 | + } |
| 86 | + |
29 | 87 | return { |
30 | 88 | getProducts |
31 | 89 | } |
|
0 commit comments