summaryrefslogtreecommitdiff
path: root/src/lib/product/components/Product/ProductMobile.jsx
diff options
context:
space:
mode:
authorRafi Zadanly <zadanlyr@gmail.com>2023-03-31 16:39:25 +0700
committerRafi Zadanly <zadanlyr@gmail.com>2023-03-31 16:39:25 +0700
commit0de043b180c1529b2d57d900523eece703e543a2 (patch)
tree47990d05913b5517c7fd9e59e22bfe92b8714018 /src/lib/product/components/Product/ProductMobile.jsx
parent8c97d9f88c52e625f32d64c94718b5808e9c4dca (diff)
cart, product
Diffstat (limited to 'src/lib/product/components/Product/ProductMobile.jsx')
-rw-r--r--src/lib/product/components/Product/ProductMobile.jsx101
1 files changed, 35 insertions, 66 deletions
diff --git a/src/lib/product/components/Product/ProductMobile.jsx b/src/lib/product/components/Product/ProductMobile.jsx
index e560639c..13b02354 100644
--- a/src/lib/product/components/Product/ProductMobile.jsx
+++ b/src/lib/product/components/Product/ProductMobile.jsx
@@ -11,24 +11,28 @@ import { HeartIcon } from '@heroicons/react/24/outline'
import { useRouter } from 'next/router'
import MobileView from '@/core/components/views/MobileView'
import { toast } from 'react-hot-toast'
-import useVariantPrice from '@/lib/variant/hooks/useVariantPrice'
-import PriceSkeleton from '@/core/components/elements/Skeleton/PriceSkeleton'
-import useProductPrice from '../../hooks/useProductPrice'
import { createSlug } from '@/core/utils/slug'
const ProductMobile = ({ product, wishlist, toggleWishlist }) => {
const router = useRouter()
- const { productPrice } = useProductPrice({ id: product.id })
const [quantity, setQuantity] = useState('1')
const [selectedVariant, setSelectedVariant] = useState(null)
const [informationTab, setInformationTab] = useState(informationTabOptions[0].value)
+ const getLowestPrice = () => {
+ const prices = product.variants.map((variant) => variant.price)
+ const lowest = prices.reduce((lowest, price) => {
+ return price.priceDiscount < lowest.priceDiscount ? price : lowest
+ }, prices[0])
+ return lowest
+ }
+
const [activeVariant, setActiveVariant] = useState({
id: null,
code: product.code,
name: product.name,
- price: product.lowestPrice,
+ price: getLowestPrice(),
stock: product.stockTotal,
weight: product.weight
})
@@ -93,8 +97,8 @@ const ProductMobile = ({ product, wishlist, toggleWishlist }) => {
const productSimilarQuery = [
product?.name.replace(/[()/"&]/g, ''),
- `fq=-product_id:${product.id}`,
- `fq=-manufacture_id:${product.manufacture?.id || 0}`
+ `fq=-product_id_i:${product.id}`,
+ `fq=-manufacture_id_i:${product.manufacture?.id || 0}`
].join('&')
return (
@@ -124,36 +128,32 @@ const ProductMobile = ({ product, wishlist, toggleWishlist }) => {
)}
</button>
</div>
- <h1 className='leading-6 font-medium'>{activeVariant?.name}</h1>
- {activeVariant.id && <VariantPrice id={activeVariant.id} />}
- {product.variants?.length > 1 && !activeVariant.id && productPrice.isLoading && (
- <PriceSkeleton />
+ <h1 className='leading-6 font-medium mb-3'>{activeVariant?.name}</h1>
+
+ {product.variants.length > 1 && activeVariant.price.priceDiscount > 0 && !selectedVariant && (
+ <div className='text-gray_r-12/80 text-caption-2 mt-2 mb-1'>Harga mulai dari: </div>
)}
- {product.variants?.length > 1 && !activeVariant.id && productPrice.isFetched && (
- <>
- <div className='text-gray_r-12/80 text-caption-2 mt-2 mb-1'>Harga mulai dari: </div>
- {productPrice?.data?.discount > 0 && (
- <div className='flex gap-x-1 items-center'>
- <div className='text-gray_r-11 line-through text-caption-1'>
- {currencyFormat(productPrice?.data?.priceExclude)}
- </div>
- <div className='badge-solid-red'>{productPrice?.data?.discount}%</div>
- </div>
- )}
- <h3 className='text-red_r-11 font-semibold mt-1'>
- {productPrice?.data?.priceExcludeAfterDiscount > 0 ? (
- currencyFormat(productPrice?.data?.priceExcludeAfterDiscount)
- ) : (
- <span className='text-gray_r-11 leading-6 font-normal'>
- Hubungi kami untuk dapatkan harga terbaik,&nbsp;
- <a href='https://wa.me/' className='text-red_r-11 underline'>
- klik disini
- </a>
- </span>
- )}
- </h3>
- </>
+
+ {activeVariant?.price?.discountPercentage > 0 && (
+ <div className='flex gap-x-1 items-center'>
+ <div className='text-gray_r-11 line-through text-caption-1'>
+ {currencyFormat(activeVariant?.price?.price)}
+ </div>
+ <div className='badge-solid-red'>{activeVariant?.price?.discountPercentage}%</div>
+ </div>
)}
+ <h3 className='text-red_r-11 font-semibold mt-1'>
+ {activeVariant?.price?.priceDiscount > 0 ? (
+ currencyFormat(activeVariant?.price?.priceDiscount)
+ ) : (
+ <span className='text-gray_r-11 leading-6 font-normal'>
+ Hubungi kami untuk dapatkan harga terbaik,&nbsp;
+ <a href='https://wa.me/' className='text-red_r-11 underline'>
+ klik disini
+ </a>
+ </span>
+ )}
+ </h3>
</div>
<Divider />
@@ -268,37 +268,6 @@ const ProductMobile = ({ product, wishlist, toggleWishlist }) => {
)
}
-const VariantPrice = ({ id }) => {
- const { variantPrice } = useVariantPrice({ id })
-
- if (variantPrice.isLoading) return <PriceSkeleton />
-
- return (
- <>
- {variantPrice?.data?.discount > 0 && (
- <div className='flex gap-x-1 items-center mt-2'>
- <div className='text-gray_r-11 line-through text-caption-1'>
- {currencyFormat(variantPrice?.data?.priceExclude)}
- </div>
- <div className='badge-solid-red'>{variantPrice?.data?.discount}%</div>
- </div>
- )}
- <h3 className='text-red_r-11 font-semibold mt-1'>
- {variantPrice?.data?.priceExcludeAfterDiscount > 0 ? (
- currencyFormat(variantPrice?.data?.priceExcludeAfterDiscount)
- ) : (
- <span className='text-gray_r-11 leading-6 font-normal'>
- Hubungi kami untuk dapatkan harga terbaik,&nbsp;
- <a href='https://wa.me/' className='text-red_r-11 underline'>
- klik disini
- </a>
- </span>
- )}
- </h3>
- </>
- )
-}
-
const informationTabOptions = [
{ value: 'specification', label: 'Spesifikasi' },
{ value: 'description', label: 'Deskripsi' },