Skip to content

Commit 9ca859a

Browse files
committed
feat: some improvements
Signed-off-by: TheFaheem <faheemmushtaq89@gmail.com>
1 parent 2b997e4 commit 9ca859a

2 files changed

Lines changed: 80 additions & 52 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { ChevronLeft, ChevronRight } from '@mui/icons-material';
2+
import React, { ReactNode, useRef } from 'react';
3+
4+
interface CarouselProps {
5+
items: ReactNode[];
6+
title?: string;
7+
scrollAmount?: number;
8+
showNavButtons?: boolean;
9+
itemClassName?: string;
10+
}
11+
12+
import { CarouselButton, CarouselContainer, CarouselWrapper } from './style';
13+
14+
const Carousel: React.FC<CarouselProps> = ({
15+
items,
16+
scrollAmount = 300,
17+
showNavButtons = true,
18+
itemClassName = 'carousel-item'
19+
}) => {
20+
const carouselRef = useRef<HTMLDivElement>(null);
21+
22+
// Skip rendering if no items
23+
if (!items.length) return null;
24+
25+
const scroll = (direction: 'left' | 'right') => {
26+
if (carouselRef.current) {
27+
carouselRef.current.scrollBy({
28+
left: direction === 'left' ? -scrollAmount : scrollAmount,
29+
behavior: 'smooth'
30+
});
31+
}
32+
};
33+
34+
return (
35+
<CarouselWrapper>
36+
{showNavButtons && (
37+
<CarouselButton onClick={() => scroll('left')}>
38+
<ChevronLeft />
39+
</CarouselButton>
40+
)}
41+
<CarouselContainer ref={carouselRef}>
42+
{items.map((item, index) => (
43+
<div key={`carousel-item-${index}`} className={itemClassName}>
44+
{item}
45+
</div>
46+
))}
47+
</CarouselContainer>
48+
{showNavButtons && (
49+
<CarouselButton onClick={() => scroll('right')}>
50+
<ChevronRight />
51+
</CarouselButton>
52+
)}
53+
</CarouselWrapper>
54+
);
55+
};
56+
57+
export default Carousel;

src/custom/CatalogDetail/RelatedDesigns.tsx

Lines changed: 23 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
1-
import { ChevronLeft, ChevronRight } from '@mui/icons-material';
2-
import { useRef } from 'react';
31
import { CatalogCardDesignLogo } from '../CustomCatalog';
42
import CustomCatalogCard, { Pattern } from '../CustomCatalog/CustomCard';
3+
import Carousel from './Carousel';
54
import { getHeadingText } from './helper';
6-
import {
7-
AdditionalContainer,
8-
CarouselButton,
9-
CarouselContainer,
10-
CarouselWrapper,
11-
ContentHeading
12-
} from './style';
5+
import { AdditionalContainer, ContentHeading } from './style';
136
import { UserProfile } from './types';
14-
157
export interface PatternsPerUser {
168
patterns: Pattern[];
179
}
@@ -45,7 +37,6 @@ const RelatedDesigns: React.FC<RelatedDesignsProps> = ({
4537
fetchingOrgError,
4638
filterByType
4739
}) => {
48-
const carouselRef = useRef<HTMLDivElement>(null);
4940
const filteredPatterns = filterByType
5041
? detailsByType?.patterns?.filter((pattern) => pattern.id !== details.id) || []
5142
: patternsPerUser?.patterns?.filter((pattern) => pattern.id !== details.id) || [];
@@ -57,15 +48,26 @@ const RelatedDesigns: React.FC<RelatedDesignsProps> = ({
5748
? 'Unknown Organization'
5849
: orgName;
5950

60-
const scroll = (direction: 'left' | 'right') => {
61-
if (carouselRef.current) {
62-
const scrollAmount = 300; // Adjust scroll distance per click
63-
carouselRef.current.scrollBy({
64-
left: direction === 'left' ? -scrollAmount : scrollAmount,
65-
behavior: 'smooth'
66-
});
67-
}
68-
};
51+
const carouselItems = filteredPatterns.map((pattern) => (
52+
<CustomCatalogCard
53+
pattern={pattern}
54+
patternType={type}
55+
onCardClick={() => onSuggestedPatternClick(pattern)}
56+
UserName={`${userProfile?.first_name ?? ''} ${userProfile?.last_name ?? ''}`}
57+
avatarUrl={userProfile?.avatar_url}
58+
basePath={technologySVGPath}
59+
subBasePath={technologySVGSubpath}
60+
cardTechnologies={true}
61+
>
62+
<CatalogCardDesignLogo
63+
imgURL={pattern?.catalog_data?.imageURL}
64+
height={'5.5rem'}
65+
width={'100%'}
66+
zoomEffect={false}
67+
type={{ type: type }}
68+
/>
69+
</CustomCatalogCard>
70+
));
6971
return (
7072
<AdditionalContainer>
7173
<ContentHeading>
@@ -75,38 +77,7 @@ const RelatedDesigns: React.FC<RelatedDesignsProps> = ({
7577
: getHeadingText({ type, userProfile, organizationName, fetchingOrgError })}
7678
</h2>
7779
</ContentHeading>
78-
<CarouselWrapper>
79-
<CarouselButton onClick={() => scroll('left')}>
80-
<ChevronLeft />
81-
</CarouselButton>
82-
<CarouselContainer ref={carouselRef}>
83-
{filteredPatterns.map((pattern, index) => (
84-
<div key={`design-${index}`} className="carousel-item">
85-
<CustomCatalogCard
86-
pattern={pattern}
87-
patternType={type}
88-
onCardClick={() => onSuggestedPatternClick(pattern)}
89-
UserName={`${userProfile?.first_name ?? ''} ${userProfile?.last_name ?? ''}`}
90-
avatarUrl={userProfile?.avatar_url}
91-
basePath={technologySVGPath}
92-
subBasePath={technologySVGSubpath}
93-
cardTechnologies={true}
94-
>
95-
<CatalogCardDesignLogo
96-
imgURL={pattern?.catalog_data?.imageURL}
97-
height={'5.5rem'}
98-
width={'100%'}
99-
zoomEffect={false}
100-
type={{ type: type }}
101-
/>
102-
</CustomCatalogCard>
103-
</div>
104-
))}
105-
</CarouselContainer>
106-
<CarouselButton onClick={() => scroll('right')}>
107-
<ChevronRight />
108-
</CarouselButton>
109-
</CarouselWrapper>
80+
<Carousel items={carouselItems} scrollAmount={300} />
11081
</AdditionalContainer>
11182
);
11283
};

0 commit comments

Comments
 (0)