initial commit
This commit is contained in:
242
src/components/ProductCard/ProductCard.module.scss
Normal file
242
src/components/ProductCard/ProductCard.module.scss
Normal file
@@ -0,0 +1,242 @@
|
||||
// ProductListing.module.scss
|
||||
|
||||
.productCard {
|
||||
background: white;
|
||||
border-radius: 8px;
|
||||
padding: 1rem;
|
||||
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
||||
transition: transform 0.2s ease;
|
||||
text-decoration: none;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: space-between;
|
||||
|
||||
&:hover {
|
||||
transform: translateY(-4px);
|
||||
}
|
||||
@media screen and (max-width: 426px) {
|
||||
padding: 8px;
|
||||
}
|
||||
}
|
||||
|
||||
.discountBadge {
|
||||
position: absolute;
|
||||
bottom: 10px;
|
||||
left: 10px;
|
||||
background: #ff0000;
|
||||
color: white;
|
||||
padding: 0.25rem 0.5rem;
|
||||
border-radius: 8px;
|
||||
font-size: 0.875rem;
|
||||
font-weight: 600;
|
||||
@media screen and (max-width: 426px) {
|
||||
font-size: 12px;
|
||||
}
|
||||
}
|
||||
.rating {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 5px;
|
||||
svg {
|
||||
fill: #facc15;
|
||||
color: #facc15;
|
||||
}
|
||||
}
|
||||
|
||||
.imageContainer {
|
||||
// aspect-ratio: 1;
|
||||
overflow: hidden;
|
||||
border-radius: 4px;
|
||||
margin-bottom: 1rem;
|
||||
border: 1px solid #eee;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.productImage {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: contain;
|
||||
}
|
||||
|
||||
.productInfo {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.5rem;
|
||||
flex-grow: 1;
|
||||
}
|
||||
|
||||
.productName {
|
||||
font-size: 16px;
|
||||
font-weight: 600;
|
||||
color: #333;
|
||||
margin: 0;
|
||||
@media screen and (max-width: 426px) {
|
||||
font-size: 14px;
|
||||
}
|
||||
}
|
||||
|
||||
.productDescription {
|
||||
font-size: 14px;
|
||||
color: #666;
|
||||
line-height: 1.4;
|
||||
margin: 0;
|
||||
|
||||
@media screen and (max-width: 1023px) {
|
||||
font-size: 12px;
|
||||
}
|
||||
}
|
||||
|
||||
.priceContainer {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
margin-top: 0.5rem;
|
||||
margin: 0;
|
||||
justify-content: space-between;
|
||||
}
|
||||
|
||||
.currentPrice {
|
||||
font-size: 18px;
|
||||
font-weight: 600;
|
||||
color: #000000;
|
||||
@media screen and (max-width: 426px) {
|
||||
font-size: 14px;
|
||||
}
|
||||
}
|
||||
|
||||
.oldPrice {
|
||||
font-size: 12px;
|
||||
color: #d32824;
|
||||
text-decoration: line-through;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.actions {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
margin-top: 1rem;
|
||||
gap: 0.5rem;
|
||||
svg {
|
||||
height: 20px;
|
||||
width: 20px;
|
||||
}
|
||||
}
|
||||
|
||||
.favoriteButton {
|
||||
height: 36px;
|
||||
display: flex;
|
||||
// margin-right: 0.5rem;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
border-radius: 0.375rem;
|
||||
background-color: rgb(255 255 255);
|
||||
border: 1px solid rgb(237 228 255);
|
||||
svg {
|
||||
fill: #888888;
|
||||
}
|
||||
}
|
||||
|
||||
.addToCartButton {
|
||||
height: 36px;
|
||||
display: flex;
|
||||
padding-left: 0.5rem;
|
||||
padding-right: 0.5rem;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
border-radius: 0.25rem;
|
||||
border-width: 1px;
|
||||
width: 100%;
|
||||
transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
|
||||
transition-duration: 300ms;
|
||||
transition-duration: 150ms;
|
||||
background-color: #d32824;
|
||||
border: none;
|
||||
svg {
|
||||
fill: #fff;
|
||||
@media screen and (max-width: 426px) {
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
}
|
||||
}
|
||||
|
||||
&:hover {
|
||||
background-color: #e86064;
|
||||
cursor: pointer;
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.productGrid {
|
||||
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
|
||||
}
|
||||
}
|
||||
|
||||
.quantityControls {
|
||||
width: 100%;
|
||||
height: 36px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 2.5rem;
|
||||
background-color: #d32824;
|
||||
border-radius: 5px;
|
||||
min-width: 160px;
|
||||
|
||||
span {
|
||||
color: #fff;
|
||||
font-weight: 700;
|
||||
font-size: 16px;
|
||||
display: flex;
|
||||
width: 100%;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.quantityBtn {
|
||||
// width: 100%;
|
||||
height: 40px;
|
||||
border: none;
|
||||
background: transparent;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
@media screen and (max-width: 720px) {
|
||||
width: auto;
|
||||
}
|
||||
svg {
|
||||
fill: #fff;
|
||||
@media screen and (max-width: 720px) {
|
||||
width: 14px;
|
||||
}
|
||||
}
|
||||
|
||||
&:hover {
|
||||
background: #e86064;
|
||||
height: 100%;
|
||||
}
|
||||
}
|
||||
@media screen and (max-width: 720px) {
|
||||
width: 100%;
|
||||
min-width: 80px;
|
||||
gap: 0.5rem;
|
||||
min-height: 33px;
|
||||
height: 36px;
|
||||
}
|
||||
}
|
||||
.outOfStock {
|
||||
background-color: #ff4d4f;
|
||||
}
|
||||
|
||||
.disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.modalButton {
|
||||
// Style for modal buttons
|
||||
padding: 6px 15px;
|
||||
background-color: #1890ff;
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 8px;
|
||||
cursor: pointer;
|
||||
}
|
||||
@@ -0,0 +1,207 @@
|
||||
.carouselContainer {
|
||||
position: relative;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
overflow: hidden;
|
||||
touch-action: pan-y;
|
||||
}
|
||||
|
||||
.productImage {
|
||||
width: 99%;
|
||||
height: auto;
|
||||
object-fit: contain;
|
||||
// transition: transform 0.3s ease-in-out, opacity 0.3s ease-in-out;
|
||||
}
|
||||
|
||||
/* Fixed frame container to prevent layout shifts */
|
||||
.fixedFrameContainer {
|
||||
position: relative;
|
||||
width: 100%;
|
||||
aspect-ratio: 1; /* Square container */
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
background-color: white;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
/* Style for product card (non-detail view) images */
|
||||
.cardImage {
|
||||
width: auto;
|
||||
height: 300px; /* Increased height for card view */
|
||||
max-width: 100%;
|
||||
margin: auto;
|
||||
object-fit: contain;
|
||||
}
|
||||
|
||||
/* Style for images inside detail view */
|
||||
.detailImage {
|
||||
width: auto;
|
||||
height: auto;
|
||||
max-width: 90%;
|
||||
max-height: 90%;
|
||||
margin: auto;
|
||||
object-fit: contain;
|
||||
}
|
||||
|
||||
/* Specific styles for the detail carousel */
|
||||
.detailCarousel {
|
||||
.carouselContainer {
|
||||
height: auto;
|
||||
min-height: 400px;
|
||||
}
|
||||
|
||||
.fixedFrameContainer {
|
||||
min-height: 400px;
|
||||
}
|
||||
}
|
||||
|
||||
.imageWrapper {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
// .transitioning img {
|
||||
// opacity: 0.8;
|
||||
// transition: opacity 0.3s ease-in-out;
|
||||
// }
|
||||
|
||||
.arrowButton {
|
||||
position: absolute;
|
||||
top: 50%;
|
||||
transform: translateY(-50%);
|
||||
width: 30px;
|
||||
height: 30px;
|
||||
border-radius: 50%;
|
||||
background-color: rgba(255, 255, 255, 0.8);
|
||||
border: none;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
cursor: pointer;
|
||||
opacity: 0;
|
||||
transition: opacity 0.2s ease, background-color 0.2s ease;
|
||||
z-index: 10;
|
||||
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
||||
|
||||
&:hover {
|
||||
background-color: white;
|
||||
}
|
||||
|
||||
svg {
|
||||
color: #333;
|
||||
}
|
||||
}
|
||||
|
||||
/* Fixed positions for arrows - they won't move with image size changes */
|
||||
.leftArrow {
|
||||
left: 8px;
|
||||
}
|
||||
|
||||
.rightArrow {
|
||||
right: 8px;
|
||||
}
|
||||
|
||||
/* Make sure the carousel wrapper has a defined position */
|
||||
.carouselWrapper {
|
||||
position: relative;
|
||||
width: 100%;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.carouselContainer:hover .arrowButton {
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
.indicators {
|
||||
position: absolute;
|
||||
bottom: 8px;
|
||||
left: 0;
|
||||
right: 0;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
gap: 4px;
|
||||
}
|
||||
|
||||
.indicator {
|
||||
width: 6px;
|
||||
height: 6px;
|
||||
border-radius: 50%;
|
||||
background-color: rgba(200, 200, 200, 0.7);
|
||||
transition: background-color 0.2s ease;
|
||||
|
||||
&:hover {
|
||||
background-color: rgba(255, 255, 255, 0.8);
|
||||
}
|
||||
|
||||
&.active {
|
||||
background-color: #333;
|
||||
}
|
||||
}
|
||||
|
||||
.thumbnailContainer {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 8px;
|
||||
margin-top: 5px;
|
||||
justify-content: center;
|
||||
width: 100%;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.thumbnail {
|
||||
width: 60px;
|
||||
height: 60px;
|
||||
border-radius: 4px;
|
||||
overflow: hidden;
|
||||
cursor: pointer;
|
||||
border: 1px solid #e0e0e0;
|
||||
opacity: 0.7;
|
||||
transition: all 0.2s ease;
|
||||
|
||||
&:hover {
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
img {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: cover;
|
||||
}
|
||||
}
|
||||
|
||||
.activeThumbnail {
|
||||
opacity: 1;
|
||||
border: 2px solid #ff6b00;
|
||||
}
|
||||
|
||||
/* Responsive styling */
|
||||
@media (max-width: 768px) {
|
||||
.thumbnailContainer {
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.thumbnail {
|
||||
width: 50px;
|
||||
height: 50px;
|
||||
}
|
||||
|
||||
.detailCarousel {
|
||||
.carouselContainer {
|
||||
min-height: 300px;
|
||||
}
|
||||
|
||||
.fixedFrameContainer {
|
||||
min-height: 300px;
|
||||
}
|
||||
}
|
||||
|
||||
.cardImage {
|
||||
height: 180px; /* Adjusted height for mobile view */
|
||||
}
|
||||
}
|
||||
218
src/components/ProductCard/imageCarousel/index.jsx
Normal file
218
src/components/ProductCard/imageCarousel/index.jsx
Normal file
@@ -0,0 +1,218 @@
|
||||
import { useState, useRef, useEffect } from "react";
|
||||
import styles from "./ImageCarousel.module.scss";
|
||||
|
||||
const ImageCarousel = ({
|
||||
images,
|
||||
altText,
|
||||
showThumbnails = false,
|
||||
isDetailView = false, // Prop to differentiate between card and detail view
|
||||
}) => {
|
||||
const [currentIndex, setCurrentIndex] = useState(0);
|
||||
const touchStartX = useRef(0);
|
||||
const touchEndX = useRef(0);
|
||||
const carouselRef = useRef(null);
|
||||
|
||||
// Check if there are multiple images
|
||||
const hasMultipleImages = Array.isArray(images) && images.length > 1;
|
||||
|
||||
// Get current image URL
|
||||
const currentImage =
|
||||
hasMultipleImages && images[currentIndex]
|
||||
? images[currentIndex].images_400x400
|
||||
: images[0]?.images_400x400 || "";
|
||||
|
||||
// Auto-slide functionality - every 9 seconds
|
||||
useEffect(() => {
|
||||
if (!hasMultipleImages) return;
|
||||
|
||||
const interval = setInterval(() => {
|
||||
setCurrentIndex((prev) => (prev === images.length - 1 ? 0 : prev + 1));
|
||||
}, 9000);
|
||||
|
||||
return () => clearInterval(interval);
|
||||
}, [hasMultipleImages, images]);
|
||||
|
||||
// Navigate to previous image
|
||||
const handlePrev = (e) => {
|
||||
if (e) e.stopPropagation();
|
||||
if (!hasMultipleImages) return;
|
||||
setCurrentIndex((prev) => (prev === 0 ? images.length - 1 : prev - 1));
|
||||
};
|
||||
|
||||
// Navigate to next image
|
||||
const handleNext = (e) => {
|
||||
if (e) e.stopPropagation();
|
||||
if (!hasMultipleImages) return;
|
||||
setCurrentIndex((prev) => (prev === images.length - 1 ? 0 : prev + 1));
|
||||
};
|
||||
|
||||
// Handle thumbnail click
|
||||
const handleThumbnailClick = (index, e) => {
|
||||
if (e) e.stopPropagation();
|
||||
setCurrentIndex(index);
|
||||
};
|
||||
|
||||
// Touch event handlers
|
||||
const handleTouchStart = (e) => {
|
||||
touchStartX.current = e.touches[0].clientX;
|
||||
};
|
||||
|
||||
const handleTouchMove = (e) => {
|
||||
touchEndX.current = e.touches[0].clientX;
|
||||
};
|
||||
|
||||
const handleTouchEnd = () => {
|
||||
if (!hasMultipleImages) return;
|
||||
|
||||
const touchDiff = touchStartX.current - touchEndX.current;
|
||||
|
||||
// Swipe threshold - only respond to intentional swipes
|
||||
if (Math.abs(touchDiff) > 50) {
|
||||
if (touchDiff > 0) {
|
||||
// Swipe left -> Next image
|
||||
handleNext();
|
||||
} else {
|
||||
// Swipe right -> Previous image
|
||||
handlePrev();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Apply transition effect using CSS
|
||||
useEffect(() => {
|
||||
if (carouselRef.current) {
|
||||
carouselRef.current.classList.add(styles.transitioning);
|
||||
|
||||
const timer = setTimeout(() => {
|
||||
if (carouselRef.current) {
|
||||
carouselRef.current.classList.remove(styles.transitioning);
|
||||
}
|
||||
}, 900); // Match this timing with CSS transition duration
|
||||
|
||||
return () => clearTimeout(timer);
|
||||
}
|
||||
}, [currentIndex]);
|
||||
|
||||
// If there's only one image, just show it - applying different classes based on view
|
||||
if (!hasMultipleImages) {
|
||||
return (
|
||||
<div className={isDetailView ? styles.fixedFrameContainer : undefined}>
|
||||
<img
|
||||
src={currentImage || "/placeholder.svg"}
|
||||
alt={altText || "Ürün resmi"}
|
||||
className={`${styles.productImage} ${
|
||||
isDetailView ? styles.detailImage : styles.cardImage
|
||||
}`}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
className={`${styles.carouselWrapper} ${
|
||||
isDetailView ? styles.detailCarousel : ""
|
||||
}`}
|
||||
>
|
||||
<div
|
||||
className={styles.carouselContainer}
|
||||
onTouchStart={handleTouchStart}
|
||||
onTouchMove={handleTouchMove}
|
||||
onTouchEnd={handleTouchEnd}
|
||||
>
|
||||
<div
|
||||
ref={carouselRef}
|
||||
className={`${styles.imageWrapper} ${
|
||||
isDetailView ? styles.fixedFrameContainer : ""
|
||||
}`}
|
||||
>
|
||||
<img
|
||||
src={currentImage || "/placeholder.svg"}
|
||||
alt={altText || "Ürün resmi"}
|
||||
className={`${styles.productImage} ${
|
||||
isDetailView ? styles.detailImage : styles.cardImage
|
||||
}`}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Navigation arrows */}
|
||||
<button
|
||||
onClick={handlePrev}
|
||||
className={`${styles.arrowButton} ${styles.leftArrow}`}
|
||||
aria-label="Önceki resim"
|
||||
>
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width="16"
|
||||
height="16"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="2"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
>
|
||||
<polyline points="15 18 9 12 15 6"></polyline>
|
||||
</svg>
|
||||
</button>
|
||||
|
||||
<button
|
||||
onClick={handleNext}
|
||||
className={`${styles.arrowButton} ${styles.rightArrow}`}
|
||||
aria-label="Sonraki resim"
|
||||
>
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
width="16"
|
||||
height="16"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth="2"
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
>
|
||||
<polyline points="9 18 15 12 9 6"></polyline>
|
||||
</svg>
|
||||
</button>
|
||||
|
||||
{/* Indicators (dots) */}
|
||||
<div className={styles.indicators}>
|
||||
{images.map((_, idx) => (
|
||||
<span
|
||||
key={idx}
|
||||
className={`${styles.indicator} ${
|
||||
currentIndex === idx ? styles.active : ""
|
||||
}`}
|
||||
onClick={(e) => handleThumbnailClick(idx, e)}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Thumbnails - only show if showThumbnails is true */}
|
||||
{showThumbnails && (
|
||||
<div className={styles.thumbnailContainer}>
|
||||
{images.map((image, idx) => (
|
||||
<div
|
||||
key={idx}
|
||||
className={`${styles.thumbnail} ${
|
||||
currentIndex === idx ? styles.activeThumbnail : ""
|
||||
}`}
|
||||
onClick={(e) => handleThumbnailClick(idx, e)}
|
||||
>
|
||||
<img
|
||||
src={
|
||||
image.thumbnail || image.images_400x400 || "/placeholder.svg"
|
||||
}
|
||||
alt={`${altText || "Ürün"} thumbnail ${idx + 1}`}
|
||||
/>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default ImageCarousel;
|
||||
318
src/components/ProductCard/index.jsx
Normal file
318
src/components/ProductCard/index.jsx
Normal file
@@ -0,0 +1,318 @@
|
||||
import { useState, useEffect } from "react";
|
||||
import styles from "./ProductCard.module.scss";
|
||||
import { IoMdHeartEmpty, IoMdHeart } from "react-icons/io";
|
||||
import { FaShoppingCart } from "react-icons/fa";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { debounce } from "lodash";
|
||||
import {
|
||||
useAddFavoriteMutation,
|
||||
useRemoveFavoriteMutation,
|
||||
} from "../../app/api/favoritesApi";
|
||||
import { useGetFavoritesQuery } from "../../app/api/favoritesApi";
|
||||
import {
|
||||
useAddToCartMutation,
|
||||
useUpdateCartItemMutation,
|
||||
useRemoveFromCartMutation,
|
||||
useGetCartQuery,
|
||||
} from "../../app/api/cartApi";
|
||||
import { Modal } from "antd";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { DecreaseIcon, IncreaseIcon } from "../Icons";
|
||||
import ImageCarousel from "./imageCarousel/index";
|
||||
|
||||
// Helper function to strip HTML tags and truncate text
|
||||
const truncateDescription = (htmlString, maxLength = 80) => {
|
||||
// Create a temporary div to parse HTML
|
||||
const tempDiv = document.createElement("div");
|
||||
tempDiv.innerHTML = htmlString;
|
||||
|
||||
// Get text content without HTML tags
|
||||
const textContent = tempDiv.textContent || tempDiv.innerText || "";
|
||||
|
||||
// Truncate the text
|
||||
const truncatedText =
|
||||
textContent.length > maxLength
|
||||
? textContent.substring(0, maxLength).trim() + "..."
|
||||
: textContent;
|
||||
|
||||
return truncatedText;
|
||||
};
|
||||
|
||||
const ProductCard = ({
|
||||
product,
|
||||
showAddToCart = true,
|
||||
showFavoriteButton = true,
|
||||
onAddToCart,
|
||||
onToggleFavorite,
|
||||
isFavorite = false,
|
||||
descriptionMaxLength = 85, // New prop to control description length
|
||||
}) => {
|
||||
const { t } = useTranslation();
|
||||
const navigate = useNavigate();
|
||||
const [stockErrorModalVisible, setStockErrorModalVisible] = useState(false);
|
||||
const [addFavorite] = useAddFavoriteMutation();
|
||||
const [removeFavorite] = useRemoveFavoriteMutation();
|
||||
const { data: favoriteProducts = [], refetch } = useGetFavoritesQuery();
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [localIsFavorite, setLocalIsFavorite] = useState(
|
||||
favoriteProducts.some((fav) => fav.product?.id === product.id)
|
||||
);
|
||||
// Process description
|
||||
const truncatedDesc = truncateDescription(
|
||||
product.description,
|
||||
descriptionMaxLength
|
||||
);
|
||||
|
||||
const { data: cartData } = useGetCartQuery(undefined, {
|
||||
selectFromResult: (result) => ({
|
||||
data: result.data,
|
||||
}),
|
||||
});
|
||||
|
||||
const [addToCart] = useAddToCartMutation();
|
||||
const [updateCartItem] = useUpdateCartItemMutation();
|
||||
const [removeFromCart] = useRemoveFromCartMutation();
|
||||
|
||||
const cartItem = cartData?.data?.find(
|
||||
(item) => item.product?.id === product.id || item.product_id === product.id
|
||||
);
|
||||
const quantity = cartItem?.quantity || cartItem?.product_quantity || 0;
|
||||
const [localQuantity, setLocalQuantity] = useState(0);
|
||||
const [pendingQuantity, setPendingQuantity] = useState(localQuantity);
|
||||
|
||||
useEffect(() => {
|
||||
if (cartItem) {
|
||||
setLocalQuantity(cartItem.quantity || cartItem.product_quantity || 0);
|
||||
setPendingQuantity(cartItem.quantity || cartItem.product_quantity || 0);
|
||||
} else {
|
||||
setLocalQuantity(0);
|
||||
setPendingQuantity(0);
|
||||
}
|
||||
}, [cartData, cartItem]);
|
||||
|
||||
useEffect(() => {
|
||||
if (Array.isArray(favoriteProducts)) {
|
||||
const isFav = favoriteProducts.some(
|
||||
(fav) => fav.product?.id === product.id
|
||||
);
|
||||
setLocalIsFavorite(isFav);
|
||||
}
|
||||
}, [favoriteProducts, product.id]);
|
||||
|
||||
const handleAddToCart = async (event) => {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
if (product.stock <= 0) {
|
||||
setStockErrorModalVisible(true);
|
||||
return;
|
||||
}
|
||||
setLocalQuantity((prev) => prev + 1);
|
||||
setPendingQuantity((prev) => prev + 1);
|
||||
try {
|
||||
await addToCart({ productId: product.id, quantity: 1 }).unwrap();
|
||||
} catch (error) {
|
||||
console.error("Failed to add to cart:", error);
|
||||
setLocalQuantity((prev) => prev - 1);
|
||||
setPendingQuantity((prev) => prev - 1);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
const updateCart = async () => {
|
||||
if (pendingQuantity !== quantity && pendingQuantity > 0) {
|
||||
try {
|
||||
setIsLoading(true);
|
||||
await updateCartItem({
|
||||
productId: product.id,
|
||||
quantity: pendingQuantity,
|
||||
}).unwrap();
|
||||
} catch (error) {
|
||||
console.error("Failed to update cart item:", error);
|
||||
setLocalQuantity(quantity);
|
||||
setPendingQuantity(quantity);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const debouncedUpdate = debounce(updateCart, 300);
|
||||
|
||||
if (pendingQuantity !== quantity) {
|
||||
debouncedUpdate();
|
||||
}
|
||||
|
||||
return () => debouncedUpdate.cancel();
|
||||
}, [pendingQuantity, quantity, product.id, updateCartItem]);
|
||||
|
||||
const handleQuantityIncrease = (event) => {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
|
||||
if (isLoading) return;
|
||||
|
||||
if (localQuantity >= product.stock) {
|
||||
setStockErrorModalVisible(true);
|
||||
return;
|
||||
}
|
||||
|
||||
setLocalQuantity((prev) => prev + 1);
|
||||
setPendingQuantity((prev) => prev + 1);
|
||||
};
|
||||
|
||||
const handleQuantityDecrease = (event) => {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
|
||||
if (isLoading) return;
|
||||
|
||||
if (pendingQuantity <= 1) {
|
||||
setPendingQuantity(0);
|
||||
setLocalQuantity(0);
|
||||
setIsLoading(true);
|
||||
removeFromCart({ productId: product.id })
|
||||
.unwrap()
|
||||
.catch(() => {
|
||||
setLocalQuantity(1);
|
||||
setPendingQuantity(1);
|
||||
})
|
||||
.finally(() => {
|
||||
setIsLoading(false);
|
||||
});
|
||||
} else {
|
||||
setLocalQuantity((prev) => prev - 1);
|
||||
setPendingQuantity((prev) => prev - 1);
|
||||
}
|
||||
};
|
||||
|
||||
const handleToggleFavorite = async (event) => {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
if (isLoading) return;
|
||||
|
||||
setIsLoading(true);
|
||||
try {
|
||||
if (localIsFavorite) {
|
||||
const result = await removeFavorite(product.id).unwrap();
|
||||
if (result === "Removed" || result?.status === "success") {
|
||||
setLocalIsFavorite(false);
|
||||
}
|
||||
} else {
|
||||
const result = await addFavorite(product.id).unwrap();
|
||||
if (result === "Added" || result?.status === "success") {
|
||||
setLocalIsFavorite(true);
|
||||
}
|
||||
}
|
||||
await refetch();
|
||||
} catch (error) {
|
||||
console.error("Failed to toggle favorite:", error);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleCardClick = () => {
|
||||
navigate(`/product/${product.id}`);
|
||||
};
|
||||
|
||||
const { name, price_amount, old_price_amount, media = [], reviews } = product;
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className={styles.productCard} onClick={handleCardClick}>
|
||||
<div className={styles.imageContainer}>
|
||||
{product.discount && (
|
||||
<span className={styles.discountBadge}>-{product.discount}%</span>
|
||||
)}
|
||||
{product.stock === 0 && (
|
||||
<span className={`${styles.discountBadge} ${styles.outOfStock}`}>
|
||||
{t("common.out_of_stock")}
|
||||
</span>
|
||||
)}
|
||||
|
||||
<ImageCarousel images={media} altText={name} />
|
||||
</div>
|
||||
<div className={styles.productInfo}>
|
||||
<h3 className={styles.productName}>{name}</h3>
|
||||
|
||||
{/* Simple truncated description */}
|
||||
<p className={styles.productDescription}>{truncatedDesc}</p>
|
||||
|
||||
<div className={styles.priceContainer}>
|
||||
<div>
|
||||
<span className={styles.currentPrice}>{price_amount} m.</span>
|
||||
{old_price_amount && (
|
||||
<span className={styles.oldPrice}>{old_price_amount} m.</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className={styles.actions}>
|
||||
{showFavoriteButton && (
|
||||
<button
|
||||
className={styles.favoriteButton}
|
||||
onClick={handleToggleFavorite}
|
||||
>
|
||||
{localIsFavorite ? <IoMdHeart /> : <IoMdHeartEmpty />}
|
||||
</button>
|
||||
)}
|
||||
{showAddToCart && (
|
||||
<>
|
||||
{localQuantity > 0 ? (
|
||||
<div className={styles.quantityControls}>
|
||||
<button
|
||||
onClick={handleQuantityDecrease}
|
||||
className={styles.quantityBtn}
|
||||
>
|
||||
<DecreaseIcon />
|
||||
</button>
|
||||
<span>{localQuantity}</span>
|
||||
<button
|
||||
onClick={handleQuantityIncrease}
|
||||
className={styles.quantityBtn}
|
||||
>
|
||||
<IncreaseIcon />
|
||||
</button>
|
||||
</div>
|
||||
) : (
|
||||
<button
|
||||
className={styles.addToCartButton}
|
||||
onClick={handleAddToCart}
|
||||
>
|
||||
<FaShoppingCart />
|
||||
</button>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Stock Error Modal */}
|
||||
<Modal
|
||||
title={t("common.warning")}
|
||||
open={stockErrorModalVisible}
|
||||
onOk={() => setStockErrorModalVisible(false)}
|
||||
onCancel={() => setStockErrorModalVisible(false)}
|
||||
okText={t("common.ok")}
|
||||
footer={[
|
||||
<button
|
||||
key="ok"
|
||||
onClick={() => setStockErrorModalVisible(false)}
|
||||
className={styles.modalButton}
|
||||
>
|
||||
{t("common.ok")}
|
||||
</button>,
|
||||
]}
|
||||
>
|
||||
<p>
|
||||
{t("common.not_enough_stock", {
|
||||
available: product.stock,
|
||||
requested: localQuantity + 1,
|
||||
})}
|
||||
</p>
|
||||
</Modal>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default ProductCard;
|
||||
Reference in New Issue
Block a user