All files / components/products RelatedProducts.vue

0% Statements 0/5
0% Branches 0/2
0% Functions 0/1
0% Lines 0/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125                                                                                                                                                                                                                                                         
<template>
  <div
    v-if="relatedProducts.length"
    class="d-flex flex-column d-none d-md-flex mx-0 px-0 mt-5"
  >
    <h6
      class="
        text-uppercase
        related-products
        align-self-start
        text-nowrap text-lg-left text-center
        gold
        m-0
        w-100
      "
    >
      related products
    </h6>
    <div class="products row justify-content-center m-0">
      <div
        class="product col-10 col-sm-6 col-md-4 col-lg-2 p-4 p-lg-3 m-2"
        v-for="product in relatedProducts"
        :key="product.id"
      >
        <ProductCard :product="product" />
      </div>
    </div>
  </div>
</template>

<script>
import { colorTitleNumbers, shuffleArray } from "~/helpers";
import PreloaderImage from "~/components/PreloaderImage";
import ProductCard from "~/components/products/ProductCard";
 
export default {
  name: "RelatedProducts",
  props: {
    product: Object,
  },
  components: { PreloaderImage, ProductCard },
  data: () => ({
    relatedProducts: [],
  }),
  methods: {
    shuffleArray,
    colorTitleNumbers,
  },
  async mounted() {
    const products = await this.$strapi.find("products");
 
    if (this.product.categories.length && products.length) {
      const relatedProducts = products.filter((item) => {
        const itemCategoryIds = item.categories.reduce((acc, next) => {
          acc.push(next.id);
          return acc;
        }, []);
 
        const productCategoryIds = this.product.categories.reduce(
          (acc, next) => {
            acc.push(next.id);
            return acc;
          },
          []
        );
 
        const result = productCategoryIds.filter((item) =>
          itemCategoryIds.includes(item)
        );
 
        return result.length && item.id !== this.product.id ? item : false;
      });
 
      this.relatedProducts = this.shuffleArray(relatedProducts);
 
      if (this.relatedProducts.length > 6) {
        this.relatedProducts.length = 6;
      }
    }
  },
};
</script>
 
<style scoped>
@media (min-width: 300px) {
  .product.col-sm-6 {
    flex: 0 0 44%;
    max-width: 44%;
  }
}
 
@media (min-width: 768px) {
  .product.col-md-4 {
    flex: 0 0 31%;
    max-width: 31%;
  }
}
 
@media (min-width: 992px) {
  .product.col-lg-2 {
    flex: 0 0 15%;
    max-width: 15%;
  }
}
 
.related-products {
  padding: 20px;
  background-color: #000;
}
 
.products {
  padding: 40px 0;
  background-color: #f5f5f5;
}
 
.product {
  box-shadow: 0px 0px 10px 1px rgba(0, 0, 0, 0.17);
  border-radius: 10px;
  background-color: #fff;
}
 
.product:hover {
  transform: scale(1.01);
}
</style>