Skip to content

Commit 8305761

Browse files
committed
exercises
1 parent 7eddb32 commit 8305761

File tree

9 files changed

+120
-50
lines changed

9 files changed

+120
-50
lines changed

Exercises.md

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,41 @@
11
Exercises
22
=========
3+
Claim an exercise at https://github.com/itenium-be/Vue.js-Tutorial/issues
34

4-
Remove from cart
5-
----------------
5+
6+
1) Remove from cart
7+
-------------------
68
Add a button to remove socks from the Cart.
7-
- Disable the button when the color is not present in the cart.
89

910

10-
Inventory per color
11-
-------------------
12-
Keep track of inventory per color.
13-
- Add to cart should only be enabled when there are socks in that color left in the inventory.
11+
2) Inventory per color
12+
----------------------
13+
Keep track of inventory per color/variant.
1414

1515

16-
Reviews
17-
-------
18-
- Add validation: name is required
19-
- Add validation: Must accept the terms
20-
- Display all reviews already submitted
16+
3) Reviews
17+
----------
18+
A. Add validation: Name is required. Must accept the terms.
19+
B. Display all reviews already submitted.
20+
C. Turn the rating `select` into ⭐⭐⭐⭐⭐.
21+
- Allow for partial selection. (See Github issue for code snippets)
2122

2223

23-
New page: Cart contents
24-
-----------------------
25-
Add a new router link `/cart` and... display the cart contents.
26-
- Create a new `CartProduct` component to display the information. (with an image?)
27-
- Add a product price.
28-
- Allow to order more/less of a product in the Cart
29-
- Calculate the total Cart cost. (with shipping costs?)
24+
4) New page: Cart contents
25+
--------------------------
26+
A. Add a product price.
27+
B. Create a new `ProductLine` component to display the information. (with an image?)
28+
C. Add a new router link `/cart` (`router.ts`)
29+
D. Display the cart contents on the component of 4C.
30+
E. Allow to order more/less of a product in the Cart.
31+
F. Calculate the total Cart cost. (with shipping costs?)
3032

3133

32-
Vuex
33-
----
34-
- Put the `Product.vue` product properties in the store.
35-
- On the main page, loop over the products and display them.
34+
5) Vuex
35+
-------
36+
A. Put the `Product.vue` product properties in `store.ts` in a products array.
37+
B. Turn product into a `@Prop() product!: any` of `Product.vue`.
38+
C. On the main page, loop over the products and display them.
39+
D. Display a list of the `ProductLine` instead and a button to go to the detail page.
40+
E. Create a new route to go to a `ProductDetail` page.
41+
F. The `cart` is now defined in `Home.vue`. Should probably move this to the store aswell?
98.5 KB
Loading
6.26 KB
Loading
218 KB
Loading

cli-socks/src/components/Product.vue

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
<template>
22
<div class="product">
33
<div class="product-image">
4-
<img :src="require(`@/assets/socks-${imageColor}.jpg`)">
4+
<img :src="require(`@/assets/socks-${product.brand}-${selectedVariant.color}.jpg`)">
55
</div>
66

77
<div class="product-info">
88
<h1>
9-
{{ product }}
9+
{{ product.name }}
1010
<!-- eslint-disable-next-line vue/no-unused-vars -->
1111
<i v-for="i in averageReviewScore" class="fa fa-star" :key="i"></i>
1212
</h1>
1313

1414
<div>
15-
<p v-if="inventory > 10">In Stock</p>
16-
<p v-else-if="inventory">Almost sold out</p>
15+
<p v-if="product.inventory > 10">In Stock</p>
16+
<p v-else-if="product.inventory">Almost sold out</p>
1717
<p v-else>Out of Stock</p>
1818
</div>
1919

@@ -22,13 +22,13 @@
2222

2323
<button
2424
@click="addToCart"
25-
:disabled="!inventory"
26-
:class="['add-to-cart', inventory ? '' : 'disabledButton']"
25+
:disabled="!product.inventory"
26+
:class="['add-to-cart', product.inventory ? '' : 'disabledButton']"
2727
>
2828
Add to Cart
2929
</button>
3030

31-
<div v-for="(variant, index) in variants"
31+
<div v-for="(variant, index) in product.variants"
3232
:key="variant.id"
3333
class="color-box"
3434
:class="{active: selectedVariantIndex === index}"
@@ -53,35 +53,39 @@ import ProductReview from './ProductReview.vue';
5353
export default class Product extends Vue {
5454
@Prop({default: false}) private premium!: boolean;
5555
56-
product = "Socks";
56+
product = {
57+
name: "Vue Socks",
58+
brand: "Vue",
59+
variants: [
60+
{id: 1, color: "green"},
61+
{id: 2, color: "blue"}
62+
],
63+
inventory: 3,
64+
reviews: []
65+
}
66+
5767
selectedVariantIndex = 0;
58-
variants = [
59-
{id: 1, color: "green"},
60-
{id: 2, color: "blue"}
61-
];
62-
inventory = 3;
63-
reviews: number[] = [];
64-
65-
get imageColor(): string {
66-
return this.variants[this.selectedVariantIndex].color;
68+
69+
get selectedVariant(): any {
70+
return this.product.variants[this.selectedVariantIndex];
6771
}
6872
6973
get averageReviewScore(): number | null {
70-
if (!this.reviews.length) {
74+
if (!this.product.reviews.length) {
7175
return null;
7276
}
73-
return Math.round(this.reviews.reduce((a, c) => a + c, 0) / this.reviews.length);
77+
return Math.round(this.product.reviews.reduce((a, c) => a + c, 0) / this.product.reviews.length);
7478
}
7579
7680
addToCart() {
77-
console.log('uhoh', this.inventory);
78-
this.inventory--;
79-
const selectedVariant = this.variants[this.selectedVariantIndex];
81+
this.product.inventory--;
82+
const selectedVariant = this.product.variants[this.selectedVariantIndex];
8083
this.$emit('add-to-cart', {product: this.product, variant: selectedVariant});
8184
}
8285
8386
addReview(review: any) {
84-
this.reviews.push(review.rating);
87+
const reviews = this.product.reviews as any;
88+
reviews.push(review.rating);
8589
}
8690
}
8791
</script>

cli-socks/src/router.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,16 @@ export default new Router({
1919
// route level code-splitting
2020
// this generates a separate chunk (counter.[hash].js) for this route
2121
// which is lazy-loaded when the route is visited.
22-
component: () =>
23-
import(/* webpackChunkName: "counter" */ "./views/Counter.vue")
24-
}
22+
component: () => import(/* webpackChunkName: "counter" */ "./views/Counter.vue")
23+
},
24+
// {
25+
// path: '/product/:id',
26+
// component: () => import(/* webpackChunkName: "productDetail" */ "./views/ProductPage.vue")
27+
// }
28+
// Get id with: {{ $route.params.id }}
29+
// Example:
30+
// path: "/user/:username/post/:post_id"
31+
// Matches: /user/eve/post/42
32+
// params are: { username: 'eve', post_id: '42' }
2533
]
2634
});

cli-socks/src/store.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import Vuex from "vuex";
33

44
Vue.use(Vuex);
55

6+
// Exercise 5? Kill the counter store and use the products store below!
7+
68
export default new Vuex.Store({
79
// ~ component.data
810
state: {
@@ -19,7 +21,7 @@ export default new Vuex.Store({
1921
}
2022
},
2123

22-
// ~ redux.actions
24+
// ~ redux.reducers
2325
mutations: {
2426
increment(state) {
2527
state.count++;
@@ -29,3 +31,53 @@ export default new Vuex.Store({
2931
}
3032
},
3133
});
34+
35+
36+
// const productList = [{
37+
// name: "Vue Socks",
38+
// brand: "Vue",
39+
// variants: [
40+
// {id: 1, color: "green"},
41+
// {id: 2, color: "blue"}
42+
// ],
43+
// inventory: 3,
44+
// reviews: []
45+
// },
46+
// {
47+
// name: "Angular Socks",
48+
// brand: "Angular",
49+
// variants: [
50+
// {id: 1, color: "red"},
51+
// {id: 2, color: "blue"}
52+
// ],
53+
// inventory: 3,
54+
// reviews: []
55+
// },
56+
// {
57+
// name: "npm Socks",
58+
// brand: "npm",
59+
// variants: [
60+
// {id: 1, color: "red"},
61+
// ],
62+
// inventory: 3,
63+
// reviews: []
64+
// }];
65+
66+
67+
// export default new Vuex.Store({
68+
// state: {
69+
// premium: true,
70+
// cart: [],
71+
// products: productList
72+
// },
73+
74+
// actions: {},
75+
// getters: {},
76+
77+
// mutations: {
78+
// addToCart(state, product) {
79+
// const cart = state.cart as any;
80+
// cart.push(product);
81+
// }
82+
// },
83+
// });

0 commit comments

Comments
 (0)