main.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. const detailsTemplate = `<div>
  2. <h2>Details</h2>
  3. <ul>
  4. <li v-for="detail in details">{{ detail }}</li>
  5. </ul>
  6. </div>`;
  7. const productTemplate = `<div class="product">
  8. <div class="product-image">
  9. <a :href="link">
  10. <!-- bind the src attribute to the "image" property -->
  11. <img
  12. v-bind:src="image"
  13. :alt="description" :title="description"
  14. /><!-- :attr is shortcut for v-bind:attr -->
  15. </a>
  16. </div>
  17. <div class="product-info">
  18. <h1 v-cloak>{{ title }}</h1>
  19. <h2>Available?</h2>
  20. <p v-cloak v-show="inStock">In stock</p>
  21. <p v-cloak v-show="!inStock">Out of stock</p>
  22. <p>Shipping: {{ shipping }}</p>
  23. <product-details :details="details"></product-details>
  24. <h2>Colors</h2>
  25. <ul>
  26. <li v-for="(variant, index) in variants"
  27. v-bind:key="variant.variantId"
  28. @mouseover="updateImage(index)"
  29. class="color-box"
  30. :style="{ backgroundColor: variant.variantColor }"
  31. /><!-- @ est le raccourci de v-on: -->
  32. </ul>
  33. <h2>Sizes</h2>
  34. <ul>
  35. <li v-for="size in sizes" :key="size">{{ size }}</li>
  36. </ul>
  37. <button v-on:click="addToCart"
  38. :disabled="!inStock"
  39. :class="{ disabledButton: !inStock }"
  40. >Add to cart</button>
  41. <button v-on:click="removeFromCart">Remove</button>
  42. </div>
  43. <div>
  44. <h2>Reviews</h2>
  45. <p v-if="!reviews.length">There are no reviews yet.</p>
  46. <ul>
  47. <li v-for="review in reviews">
  48. <p>Name: {{ review.name }}</p>
  49. <p>Rating: {{ review.rating }}</p>
  50. <p>Recommends: {{ review.recommend }}</p>
  51. <p>Review: {{ review.review }}</p>
  52. </li>
  53. </ul>
  54. </div>
  55. <product-review @review-submitted="addReview"></product-review>
  56. </div>
  57. `;
  58. Vue.component("product-details", {
  59. props: {
  60. details: {
  61. type: Array,
  62. required: true,
  63. },
  64. },
  65. template: detailsTemplate,
  66. });
  67. Vue.component("product-review", {
  68. template: `
  69. <form class="review-form" @submit.prevent="onSubmit">
  70. <p v-if="errors.length">
  71. <b>Please correct the following errror(s):</b>
  72. <ul>
  73. <li v-for="error in errors">{{ error }}</li>
  74. </ul>
  75. </p>
  76. <p>
  77. <label for="name">Name:</label>
  78. <input id="name" v-model="name">
  79. </p>
  80. <p>
  81. <label for="review">Review:</label>
  82. <textarea id="review" v-model="review"></textarea>
  83. </p>
  84. <label for="rating">Rating:</label>
  85. <select id="rating" v-model.number="rating">
  86. <option>5</option>
  87. <option>4</option>
  88. <option>3</option>
  89. <option>2</option>
  90. <option>1</option>
  91. </select>
  92. <p>Would you recommend this product?</p>
  93. <div>
  94. <label for="reco-no">No</label> <input type="radio" v-model="recommend" id="reco-no" value="no">
  95. <label for="reco-maybe">Maybe</label><input type="radio" v-model="recommend" id="reco-maybe" value="maybe">
  96. <label for="reco-yes">Yes</label> <input type="radio" v-model="recommend" id="reco-yes" value="yes">
  97. </div>
  98. <p>
  99. <input type="submit" value="Submit">
  100. </p>
  101. </form>
  102. `,
  103. data() {
  104. return {
  105. name: null,
  106. recommend: null,
  107. review: null,
  108. rating: null,
  109. errors: [],
  110. }
  111. },
  112. methods: {
  113. onSubmit(evt) {
  114. this.errors = [];
  115. if (this.name && this.review && this.rating && this.recommend) {
  116. const productReview = {
  117. name: this.name,
  118. rating: this.rating,
  119. recommend: this.recommend,
  120. review: this.review,
  121. };
  122. this.$emit("review-submitted", productReview);
  123. this.name = null;
  124. this.rating = null;
  125. this.recommend = null;
  126. this.review = null;
  127. } else {
  128. for (const field of ["name", "rating", "recommend", "review"]) {
  129. if (!this[field]) {
  130. this.errors.push(`${field} required`);
  131. }
  132. }
  133. }
  134. }
  135. }
  136. });
  137. Vue.component("product", {
  138. props: {
  139. isPremium: {
  140. type: Boolean,
  141. required: true,
  142. }
  143. },
  144. template: productTemplate,
  145. // Computer properties are cached until dependencies change.
  146. // => cheaper than methods.
  147. computed: {
  148. image() {
  149. const image = this.variants[this.selectedVariant].variantImage;
  150. return image;
  151. },
  152. inStock() {
  153. return this.variants[this.selectedVariant].variantQuantity > 0;
  154. },
  155. shipping() {
  156. if (this.isPremium) {
  157. return "Free";
  158. }
  159. return "2.99 €";
  160. },
  161. title() {
  162. return [this.brand, this.product].join(' ');
  163. }
  164. },
  165. data() {
  166. return {
  167. brand: "Vue Mastery",
  168. description: "Chaussettes montantes",
  169. details: [
  170. "80% cotton",
  171. "20% polyester",
  172. "Gender-neutral",
  173. ],
  174. inventory: 15,
  175. link: "https://www.google.com/search?q=vuejs+socks",
  176. product: "Socks",
  177. reviews: [],
  178. selectedVariant: 0,
  179. sizes: [34, 36, 38, 40, 42, 44, 46],
  180. variants: [
  181. {
  182. variantId: 2234,
  183. variantColor: "green",
  184. variantImage: "./assets/img/vmSocks-green-onWhite.jpg",
  185. variantQuantity: 10,
  186. },
  187. {
  188. variantId: 2235,
  189. variantColor: "blue",
  190. variantImage: "./assets/img/vmSocks-blue-onWhite.png",
  191. variantQuantity: 0,
  192. },
  193. ]
  194. }
  195. },
  196. methods: {
  197. addToCart() {
  198. this.variants[this.selectedVariant].variantQuantity--;
  199. this.$emit("add-to-cart", this.variants[this.selectedVariant].variantId);
  200. },
  201. addReview(productReview) {
  202. this.reviews.push(productReview);
  203. },
  204. removeFromCart() {
  205. this.variants[this.selectedVariant].variantQuantity++;
  206. this.$emit("remove-from-cart", this.variants[this.selectedVariant].variantId);
  207. },
  208. updateImage(index) {
  209. this.selectedVariant = index;
  210. }
  211. },
  212. });
  213. const app = new Vue({
  214. el: "#app",
  215. data: {
  216. cart: [],
  217. isPremium: false,
  218. },
  219. methods: {
  220. removeItem(id) {
  221. console.log("removeItem", id);
  222. const index = this.cart.indexOf(id);
  223. if (index > -1) {
  224. this.cart.splice(index, 1);
  225. }
  226. },
  227. updateCart(id) {
  228. console.log("updateCart", id);
  229. this.cart.push(id);
  230. }
  231. }
  232. });