|
2 | 2 |
|
3 | 3 | <!-- Template Part --> |
4 | 4 | <template> |
5 | | - <div class="container"> |
| 5 | + <div class="container" :class="{'loading': loading}"> |
| 6 | + |
| 7 | + <!-- Spinner will only appear if the private property `loading` is true. --> |
| 8 | + <div class="loading-wrapper" v-if="loading"> |
| 9 | + <div class="spinner-border text-secondary" role="status"> |
| 10 | + <span class="sr-only">Loading...</span> |
| 11 | + </div> |
| 12 | + </div> |
6 | 13 |
|
7 | 14 | <div class="row"> |
8 | 15 |
|
9 | 16 | <div class="col-lg-3"> |
10 | | - |
11 | 17 | <h1 class="my-4">Shop Catalog</h1> |
12 | | - |
| 18 | + <div class="list-group" v-if="Array.isArray(categories) && categories.length > 0"> |
| 19 | + <a :href="`/?category=${category.id}`" class="list-group-item" v-for="category in categories"> |
| 20 | + {{ category.name }} |
| 21 | + </a> |
| 22 | + </div> |
13 | 23 | </div> |
14 | 24 | <!-- /.col-lg-3 --> |
15 | 25 |
|
16 | 26 | <div class="col-lg-9"> |
17 | 27 |
|
18 | | - <div class="row my-5"> |
19 | | - |
| 28 | + <div class="row my-5" v-if="Array.isArray(products) && products.length > 0"> |
| 29 | + <div class="col-lg-4 col-md-6 mb-4" v-for="product in products"> |
| 30 | + <div class="card h-100"> |
| 31 | + <a :href="`/product/${product.name}`"> |
| 32 | + <img class="card-img-top" src="http://placehold.it/700x400" alt=""> |
| 33 | + </a> |
| 34 | + <div class="card-body"> |
| 35 | + <h4 class="card-title"> |
| 36 | + <a :href="`/product/${product.name}`"> |
| 37 | + {{ _.capitalize (product.name) }} |
| 38 | + </a> |
| 39 | + </h4> |
| 40 | + <h5>${{ product.price }}</h5> |
| 41 | + <p class="card-text"> |
| 42 | + {{ product.description }} |
| 43 | + </p> |
| 44 | + </div> |
| 45 | + </div> |
| 46 | + </div> |
20 | 47 | </div> |
21 | 48 | <!-- /.row --> |
22 | 49 |
|
|
31 | 58 |
|
32 | 59 |
|
33 | 60 | <!-- JavaScript Part --> |
34 | | -<script></script> |
| 61 | +<script> |
| 62 | +export default { |
| 63 | + // Public properties |
| 64 | + data: function(){ |
| 65 | + return { |
| 66 | + categories: [], |
| 67 | + products: [], |
| 68 | + loading: true |
| 69 | + } |
| 70 | + }, |
| 71 | + // Private method runs before the component is mount on the screen |
| 72 | + mounted() { |
| 73 | + //console.log('Component is mounted!'); |
| 74 | + this.loadCategories(); |
| 75 | + this.loadProducts(); |
| 76 | + }, |
| 77 | + // Methods |
| 78 | + methods: { |
| 79 | + loadCategories: function (){ |
| 80 | + // load API |
| 81 | + // assign this.categories |
| 82 | + // catch errors |
| 83 | + axios.get('/api/categories').then(response => { |
| 84 | + this.categories = response.data.data; |
| 85 | + }).catch(err => { |
| 86 | + console.log('API Promise rejected error :', err); |
| 87 | + } |
| 88 | + ); |
| 89 | + }, |
| 90 | + loadProducts: function (){ |
| 91 | + axios.get('/api/products').then(response => { |
| 92 | + this.products = response.data.data; |
| 93 | + this.loading = false; |
| 94 | + }).catch(err => { |
| 95 | + console.log(err); |
| 96 | + }); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | +</script> |
35 | 101 |
|
36 | 102 |
|
37 | 103 |
|
0 commit comments