import Divider from '@/core/components/elements/Divider/Divider';
import Image from '@/core/components/elements/Image/Image';
import Link from '@/core/components/elements/Link/Link';
import currencyFormat from '@/core/utils/currencyFormat';
import { useEffect, useState } from 'react';
import Select from 'react-select';
import ProductSimilar from '../ProductSimilar';
import LazyLoad from 'react-lazy-load';
import { updateItemCart } from '@/core/utils/cart';
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 { createSlug } from '@/core/utils/slug';
import BottomPopup from '@/core/components/elements/Popup/BottomPopup';
import whatsappUrl from '@/core/utils/whatsappUrl';
import PromotionType from '@/lib/promotinProgram/components/PromotionType';
import { gtagAddToCart } from '@/core/utils/googleTag';
import odooApi from '@/core/api/odooApi';
import ImageNext from 'next/image';
import CountDown2 from '@/core/components/elements/CountDown/CountDown2';
import Breadcrumb from './Breadcrumb';
import useAuth from '@/core/hooks/useAuth';
import { sellingProductFormat } from '@/core/utils/formatValue';
import ProductPromoSection from '~/modules/product-promo/components/Section';
const ProductMobile = ({ product, wishlist, toggleWishlist }) => {
const router = useRouter();
const auth = useAuth();
const { slug } = router.query;
const [quantity, setQuantity] = useState('1');
const [selectedVariant, setSelectedVariant] = useState(null);
const [informationTab, setInformationTab] = useState(
informationTabOptions[0].value
);
const [addCartAlert, setAddCartAlert] = useState(false);
const [isLoadingSLA, setIsLoadingSLA] = useState(true);
const [promotionType, setPromotionType] = useState(false);
const [promotionActiveId, setPromotionActiveId] = useState(null);
const [backgorundFlashSale, setBackgorundFlashSale] = useState(null);
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;
};
useEffect(() => {
const getBackgound = async () => {
const get = await odooApi(
'GET',
'/api/v1/banner?type=flash-sale-background-banner'
);
if (get.length > 0) {
setBackgorundFlashSale(get[0].image);
}
};
getBackgound();
}, []);
const [activeVariant, setActiveVariant] = useState({
id: null,
code: product.code,
name: product.name,
price: getLowestPrice(),
stock: product.stockTotal,
weight: product.weight,
hasProgram: false,
qtySold: product.qtySold,
});
const variantOptions = product.variants?.map((variant) => {
let label = [];
if (variant.isFlashsale) {
label.push("🗲");
}
if (variant.code) {
label.push(`[${variant.code}]`);
}
if (variant.attributes.length > 0) {
label.push(variant.attributes.join(', '));
} else {
label.push(product.name);
}
return {
value: variant.id,
label: label.join(' '),
};
});
useEffect(() => {
if (!selectedVariant && variantOptions.length == 1) {
const selectedVariant =
variantOptions?.find((variant) => variant.is_in_bu) ||
variantOptions?.[0];
setSelectedVariant(selectedVariant);
// setSelectedVariant(variantOptions[0]);
}
}, [selectedVariant, variantOptions]);
useEffect(() => {
if (selectedVariant) {
const variant = product.variants.find(
(variant) => variant.id == selectedVariant.value
);
const variantAttributes =
variant.attributes.length > 0
? ' - ' + variant.attributes.join(', ')
: '';
const newActiveVariant = {
id: variant.id,
code: variant.code,
name: variant.parent.name + variantAttributes,
price: variant.price,
stock: variant.stock,
weight: variant.weight,
hasProgram: variant.hasProgram,
isFlashsale: variant.isFlashsale,
qtySold: variant.qtySold,
};
setActiveVariant(newActiveVariant);
const fetchSLA = async () => {
const dataSLA = await odooApi(
'GET',
`/api/v1/product_variant/${variant.id}/stock`
);
setActiveVariant({ ...newActiveVariant, sla: dataSLA });
};
fetchSLA();
}
}, [selectedVariant, product]);
const validAction = () => {
let isValid = true;
if (!selectedVariant) {
toast.error('Pilih varian terlebih dahulu');
isValid = false;
}
if (!quantity || quantity < 1 || isNaN(parseInt(quantity))) {
toast.error('Jumlah barang minimal 1');
isValid = false;
}
return isValid;
};
const redirectToLogin = (action) => {
const nextURL = `/shop/product/${slug}?action=${action}&variantId=${activeVariant.id}&qty=${quantity}`;
router.push(`/login?next=${encodeURIComponent(nextURL)}`);
return true;
};
const handleClickCart = () => {
if (!validAction()) return;
gtagAddToCart(activeVariant, quantity);
if (!auth) {
return redirectToLogin('add_to_cart');
}
updateItemCart({
productId: activeVariant.id,
quantity,
programLineId: promotionActiveId,
selected: true,
});
setAddCartAlert(true);
};
const handleClickBuy = () => {
if (!validAction()) return;
if (!auth) {
return redirectToLogin('buy');
}
updateItemCart({
productId: activeVariant.id,
quantity,
programLineId: promotionActiveId,
selected: true,
source: 'buy',
});
router.push(`/shop/checkout?source=buy`);
};
const productSimilarQuery = [
product?.name,
`fq=-product_id_i:${product.id}`,
`fq=-manufacture_id_i:${product.manufacture?.id || 0}`,
].join('&');
return (
{activeVariant?.name}
{product?.qtySold > 0 && (
{activeVariant?.price?.price > 0 ? (
<>
{currencyFormat(activeVariant?.price?.price)}
)}
Informasi Produk
Kamu Mungkin Juga Suka