main.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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>{{ review.name }}</p>
  49. <p>{{ review.rating }}</p>
  50. <p>{{ review.review }}</p>
  51. </li>
  52. </ul>
  53. </div>
  54. <product-review @review-submitted="addReview"></product-review>
  55. </div>
  56. `;
  57. Vue.component("product-details", {
  58. props: {
  59. details: {
  60. type: Array,
  61. required: true,
  62. },
  63. },
  64. template: detailsTemplate,
  65. });
  66. Vue.component("product-review", {
  67. template: `
  68. <form class="review-form" @submit.prevent="onSubmit">
  69. <p v-if="errors.length">
  70. <b>Please correct the following errror(s):</b>
  71. <ul>
  72. <li v-for="error in errors">{{ error }}</li>
  73. </ul>
  74. </p>
  75. <p>
  76. <label for="name">Name:</label>
  77. <input id="name" v-model="name">
  78. </p>
  79. <p>
  80. <label for="review">Review:</label>
  81. <textarea id="review" v-model="review"></textarea>
  82. </p>
  83. <label for="rating">Rating:</label>
  84. <select id="rating" v-model.number="rating">
  85. <option>5</option>
  86. <option>4</option>
  87. <option>3</option>
  88. <option>2</option>
  89. <option>1</option>
  90. </select>
  91. <p>
  92. <input type="submit" value="Submit">
  93. </p>
  94. </form>
  95. `,
  96. data() {
  97. return {
  98. name: null,
  99. review: null,
  100. rating: null,
  101. errors: [],
  102. }
  103. },
  104. methods: {
  105. onSubmit(evt) {
  106. this.errors = [];
  107. if (this.name && this.review && this.rating) {
  108. const productReview = {
  109. name: this.name,
  110. rating: this.rating,
  111. review: this.review,
  112. };
  113. this.$emit("review-submitted", productReview);
  114. this.name = null;
  115. this.rating = null;
  116. this.review = null;
  117. } else {
  118. for (const field of ["name", "rating", "review"]) {
  119. if (!this[field]) {
  120. this.errors.push(`${field} required`);
  121. }
  122. }
  123. }
  124. }
  125. }
  126. });
  127. Vue.component("product", {
  128. props: {
  129. isPremium: {
  130. type: Boolean,
  131. required: true,
  132. }
  133. },
  134. template: productTemplate,
  135. // Computer properties are cached until dependencies change.
  136. // => cheaper than methods.
  137. computed: {
  138. image() {
  139. const image = this.variants[this.selectedVariant].variantImage;
  140. return image;
  141. },
  142. inStock() {
  143. return this.variants[this.selectedVariant].variantQuantity > 0;
  144. },
  145. shipping() {
  146. if (this.isPremium) {
  147. return "Free";
  148. }
  149. return "2.99 €";
  150. },
  151. title() {
  152. return [this.brand, this.product].join(' ');
  153. }
  154. },
  155. data() {
  156. return {
  157. brand: "Vue Mastery",
  158. description: "Chaussettes montantes",
  159. details: [
  160. "80% cotton",
  161. "20% polyester",
  162. "Gender-neutral",
  163. ],
  164. inventory: 15,
  165. link: "https://www.google.com/search?q=vuejs+socks",
  166. product: "Socks",
  167. reviews: [],
  168. selectedVariant: 0,
  169. sizes: [34, 36, 38, 40, 42, 44, 46],
  170. variants: [
  171. {
  172. variantId: 2234,
  173. variantColor: "green",
  174. variantImage: "./assets/img/vmSocks-green-onWhite.jpg",
  175. variantQuantity: 10,
  176. },
  177. {
  178. variantId: 2235,
  179. variantColor: "blue",
  180. variantImage: "./assets/img/vmSocks-blue-onWhite.png",
  181. variantQuantity: 0,
  182. },
  183. ]
  184. }
  185. },
  186. methods: {
  187. addToCart() {
  188. this.variants[this.selectedVariant].variantQuantity--;
  189. this.$emit("add-to-cart", this.variants[this.selectedVariant].variantId);
  190. },
  191. addReview(productReview) {
  192. this.reviews.push(productReview);
  193. },
  194. removeFromCart() {
  195. this.variants[this.selectedVariant].variantQuantity++;
  196. this.$emit("remove-from-cart", this.variants[this.selectedVariant].variantId);
  197. },
  198. updateImage(index) {
  199. this.selectedVariant = index;
  200. }
  201. },
  202. });
  203. const app = new Vue({
  204. el: "#app",
  205. data: {
  206. cart: [],
  207. isPremium: false,
  208. },
  209. methods: {
  210. removeItem(id) {
  211. console.log("removeItem", id);
  212. const index = this.cart.indexOf(id);
  213. if (index > -1) {
  214. this.cart.splice(index, 1);
  215. }
  216. },
  217. updateCart(id) {
  218. console.log("updateCart", id);
  219. this.cart.push(id);
  220. }
  221. }
  222. });