Skip to content

Commit

Permalink
Merge pull request #1073 from w3bdesign/1001-refactor-code
Browse files Browse the repository at this point in the history
1001 refactor code
  • Loading branch information
w3bdesign committed Jun 9, 2023
2 parents 308a91c + 5c4490a commit e667f5a
Show file tree
Hide file tree
Showing 11 changed files with 616 additions and 497 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
PUBLIC_GRAPHQL_URL = "change me"
PUBLIC_PLACEHOLDER_SMALL_IMAGE_URL = "https://via.placeholder.com/500"
PUBLIC_CURRENCY_SYMBOL = "kr"
PUBLIC_CURRENCY = "NOK"
PUBLIC_CURRENCY_LOCALE = "nb-NO"
ALGOLIA_APPLICATION_ID = "change me"
ALGOLIA_API_KEY = "change me"
ALGOLIA_INDEX_NAME = "change me"
2 changes: 1 addition & 1 deletion components/Layout/LayoutCart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
{{ cartLength }}
</span>
<span class="text-white lg:text-black"
>Total: {{ config.currencySymbol }} {{ subTotal }}</span
>Total: {{ config.public.currencySymbol }} {{ subTotal }}</span
>
</div>
</transition>
Expand Down
27 changes: 27 additions & 0 deletions components/Products/ProductImage.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<template>
<img
id="product-image"
class="h-auto p-8 transition duration-500 ease-in-out transform md:p-0 hover:shadow-lg hover:scale-95"
:alt="alt"
:src="displayedImage"
:width="width"
:height="height"
/>
</template>

<script setup>
import { computed } from "vue";
const props = defineProps({
alt: { type: String, required: true },
src: { type: String, required: true },
width: { type: String, required: false },
height: { type: String, required: false },
});
const config = useRuntimeConfig();
const displayedImage = computed(() => {
return props.src || config.public.placeholderImage;
});
</script>
89 changes: 89 additions & 0 deletions components/Products/ProductPrice.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<template>
<div>
<div
v-if="onSale"
class="flex"
:class="shouldCenterPrice ? 'justify-center' : ''"
>
<p class="pt-1 mt-4 text-gray-900" :class="getFontSizeClass">
{{ formatPrice(displayPrice) }}
</p>
<p
class="pt-1 pl-4 mt-4 text-gray-900 line-through"
:class="getSaleFontSizeClass"
>
{{ formatPrice(basePrice) }}
</p>
</div>
<p
v-else
class="flex pt-1 mt-4 text-2xl text-gray-900"
:class="shouldCenterPrice ? 'justify-center' : ''"
>
{{ formatPrice(displayPrice) }}
</p>
</div>
</template>

<script setup>
import { computed } from "vue";
import {
formatPrice,
filteredVariantPrice,
hasVariations,
} from "@/utils/functions";
const props = defineProps({
product: Object,
priceFontSize: String,
shouldCenterPrice: Boolean,
});
const onSale = computed(() => props.product.onSale);
const productHasVariations = computed(() => hasVariations(props.product));
const basePrice = computed(() => {
if (productHasVariations.value) {
return filteredVariantPrice(props.product.price);
} else {
return props.product.regularPrice;
}
});
const displayPrice = computed(() => {
if (onSale.value) {
return productHasVariations.value
? filteredVariantPrice(props.product.price)
: props.product.salePrice;
} else {
return props.product.price;
}
});
const getFontSizeClass = computed(() => {
switch (props.priceFontSize) {
case "small":
return "text-lg";
case "normal":
return "text-2xl";
case "big":
return "text-2xl";
default:
return "text-xl";
}
});
const getSaleFontSizeClass = computed(() => {
switch (props.priceFontSize) {
case "small":
return "text-lg";
case "normal":
return "text-xl";
case "big":
return "text-xl";
default:
return "text-xl";
}
});
</script>
72 changes: 18 additions & 54 deletions components/Products/ProductsShowAll.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<div v-if="allCategoryProducts?.productCategory?.products?.nodes">
<template v-if="allCategoryProducts?.productCategory?.products?.nodes">
<section>
<div id="product-container" class="flex flex-wrap items-center">
<template
Expand All @@ -17,42 +17,24 @@
query: { id: product.databaseId },
}"
>
<img
id="product-image"
class="p-8 border mx-auto w-4/5 border-gray-200 rounded drop-shadow-lg transition duration-500 ease-in-out transform cursor-pointer lg:ml-0 lg:w-full lg:p-2 hover:scale-95"
:alt="product.name"
:src="productImage(product)"
/>
<ProductImage :alt="product.name" :src="productImage(product)" />

<div class="flex justify-center pt-3">
<p class="text-2xl font-bold text-center cursor-pointer">
{{ product.name }}
</p>
</div>
</NuxtLink>
<div v-if="product.onSale" class="flex justify-center mt-2">
<div class="text-lg text-gray-900 line-through">
<span v-if="product.variations">
{{ filteredVariantPrice(product.price, "right") }}</span
>
<span v-else>{{ product.regularPrice }}</span>
</div>
<div class="ml-4 text-xl text-gray-900">
<span v-if="product.variations">
{{ filteredVariantPrice(product.price) }}</span
>
<span v-else>{{ product.salePrice }}</span>
</div>
</div>
<div v-else>
<p class="mt-2 text-xl text-center text-gray-900">
{{ product.price }}
</p>
</div>
<ProductPrice
:product="product"
priceFontSize="normal"
:shouldCenterPrice="true"
/>
</div>
</template>
</div>
</section>
</div>
</template>
<div v-else>
<section>
<div id="product-container" class="flex flex-wrap items-center">
Expand All @@ -69,37 +51,18 @@
query: { id: product.databaseId },
}"
>
<img
id="product-image"
class="p-8 border mx-auto w-4/5 border-gray-200 rounded drop-shadow-lg transition duration-500 ease-in-out transform cursor-pointer lg:ml-0 lg:w-full lg:p-2 hover:scale-95"
:alt="product.name"
:src="productImage(product)"
/>
<ProductImage :alt="product.name" :src="productImage(product)" />
<div class="flex justify-center pt-3">
<p class="text-2xl font-bold text-center cursor-pointer">
{{ product.name }}
</p>
</div>
</NuxtLink>
<div v-if="product.onSale" class="flex justify-center mt-2">
<div class="text-lg text-gray-900 line-through">
<span v-if="product.variations">
{{ filteredVariantPrice(product.price, "right") }}</span
>
<span v-else>{{ product.regularPrice }}</span>
</div>
<div class="ml-4 text-xl text-gray-900">
<span v-if="product.variations">
{{ filteredVariantPrice(product.price) }}</span
>
<span v-else>{{ product.salePrice }}</span>
</div>
</div>
<div v-else>
<p class="mt-2 text-xl text-center text-gray-900">
{{ product.price }}
</p>
</div>
<ProductPrice
:product="product"
priceFontSize="normal"
:shouldCenterPrice="true"
/>
</div>
</template>
</div>
Expand All @@ -111,7 +74,8 @@
import FETCH_ALL_PRODUCTS_QUERY from "@/apollo/queries/FETCH_ALL_PRODUCTS_QUERY.gql";
import GET_PRODUCTS_FROM_CATEGORY_QUERY from "@/apollo/queries/GET_PRODUCTS_FROM_CATEGORY_QUERY.gql";
import { filteredVariantPrice } from "@/utils/functions";
import ProductImage from "@/components/Products/ProductImage.vue";
import ProductPrice from "@/components/Products/ProductPrice.vue";
const props = defineProps({
categoryId: { type: String, required: false },
Expand All @@ -121,7 +85,7 @@ const props = defineProps({
const config = useRuntimeConfig();
const productImage = (product) =>
product.image ? product.image.sourceUrl : config.placeholderImage;
product.image ? product.image.sourceUrl : config.public.placeholderImage;
const productVariables = { limit: 99 };
const { data: allProducts } = await useAsyncQuery(
Expand Down
Loading

1 comment on commit e667f5a

@vercel
Copy link

@vercel vercel bot commented on e667f5a Jun 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.