From fb5eb06bdd7f7a36a73a6f844006bc66c1b545e1 Mon Sep 17 00:00:00 2001 From: Mateja Date: Sat, 24 Jul 2021 20:19:10 +0200 Subject: Fetch products inside Vuex and display them using `ProductCard` components. --- client/src/components/ProductCard.vue | 22 ++++++++++++++++------ client/src/store/index.js | 17 ++++++++++++++++- client/src/views/Home.vue | 12 ++++++++++-- 3 files changed, 42 insertions(+), 9 deletions(-) (limited to 'client/src') diff --git a/client/src/components/ProductCard.vue b/client/src/components/ProductCard.vue index 57698e5..849c360 100644 --- a/client/src/components/ProductCard.vue +++ b/client/src/components/ProductCard.vue @@ -1,9 +1,10 @@ @@ -12,9 +13,18 @@ export default { name: 'ProductCard', props: { - productName: String, - productImg: String, - productLink: String + Id: String, + Name: String, + Img: String, + Price: Number + }, + computed: { + ImgLink() { + return `${process.env.VUE_APP_ROOT_API}/${this.Img}`; + }, + ProductLink() { + return `/product/${this.Id}` + } } } diff --git a/client/src/store/index.js b/client/src/store/index.js index 5f05f19..d2fe9b7 100644 --- a/client/src/store/index.js +++ b/client/src/store/index.js @@ -1,11 +1,26 @@ -import { createStore } from 'vuex' +import {createStore} from 'vuex' +import axios from 'axios'; export default createStore({ state: { + products: [] + }, + getters: { + getProducts(state) { + return state.products; + } }, mutations: { + setProducts(state, products) { + state.products = products; + } }, actions: { + async pullProducts(context) { + await axios.get(`${process.env.VUE_APP_ROOT_API}/products`) + .then(response => context.commit('setProducts', response.data)) + .catch(error => console.error(error)); + } }, modules: { } diff --git a/client/src/views/Home.vue b/client/src/views/Home.vue index 3e89885..a71d2cb 100644 --- a/client/src/views/Home.vue +++ b/client/src/views/Home.vue @@ -1,7 +1,7 @@ @@ -13,6 +13,14 @@ export default { name: 'Home', components: { ProductCard + }, + mounted() { + this.$store.dispatch('pullProducts'); + }, + computed: { + products() { + return this.$store.getters.getProducts; + } } } -- cgit v1.2.3