|
| 1 | +import { RESOURCE_CATEGORIES } from '@/lib/constants/enums'; |
| 2 | +import { useTypedSelector } from '@/lib/hooks/store'; |
| 3 | +import { getStoryblokStories } from '@/lib/storyblok'; |
| 4 | +import filterResourcesForLocaleAndPartnerAccess from '@/lib/utils/filterStoryByLanguageAndPartnerAccess'; |
| 5 | +import { getDefaultFullSlug } from '@/lib/utils/getDefaultFullSlug'; |
| 6 | +import userHasAccessToPartnerContent from '@/lib/utils/userHasAccessToPartnerContent'; |
| 7 | +import { Box } from '@mui/material'; |
| 8 | +import { ISbStoriesParams, ISbStoryData } from '@storyblok/react/rsc'; |
| 9 | +import Cookies from 'js-cookie'; |
| 10 | +import { useLocale } from 'next-intl'; |
| 11 | +import { useEffect, useState } from 'react'; |
| 12 | +import { RelatedContentCard } from '../cards/RelatedContentCard'; |
| 13 | +import { ShortsCard } from '../cards/ShortsCard'; |
| 14 | +import Carousel, { getSlideWidth } from '../common/Carousel'; |
| 15 | + |
| 16 | +export interface ResourceCarouselProps { |
| 17 | + resourceTypes?: string[]; |
| 18 | + title?: string; |
| 19 | + // Either you can pass the data down if you already have it or you can pull from the storyblok API |
| 20 | + resources?: ISbStoryData[]; |
| 21 | +} |
| 22 | + |
| 23 | +const ResourceCarousel = ({ |
| 24 | + resourceTypes, |
| 25 | + title = 'resource-category-carousel', |
| 26 | + resources = [], |
| 27 | +}: ResourceCarouselProps) => { |
| 28 | + const userId = useTypedSelector((state) => state.user.id); |
| 29 | + const entryPartnerReferral = useTypedSelector((state) => state.user.entryPartnerReferral); |
| 30 | + const partnerAccesses = useTypedSelector((state) => state.partnerAccesses); |
| 31 | + const partnerAdmin = useTypedSelector((state) => state.partnerAdmin); |
| 32 | + // TODO filter by partner based on user access! |
| 33 | + const locale = useLocale(); // Get the current locale |
| 34 | + const baseProps: Partial<ISbStoriesParams> = { |
| 35 | + language: locale, |
| 36 | + version: 'published', |
| 37 | + sort_by: 'position:description', |
| 38 | + }; |
| 39 | + |
| 40 | + const [carouselStories, setCarouselStories] = useState<ISbStoryData[]>([]); |
| 41 | + useEffect(() => { |
| 42 | + if (resources.length < 1 && resourceTypes) { |
| 43 | + const referralPartner = Cookies.get('referralPartner') || entryPartnerReferral; |
| 44 | + const userPartners = userHasAccessToPartnerContent( |
| 45 | + partnerAdmin?.partner, |
| 46 | + partnerAccesses, |
| 47 | + referralPartner, |
| 48 | + userId, |
| 49 | + ); |
| 50 | + |
| 51 | + if (resourceTypes.includes('shorts')) { |
| 52 | + getStoryblokStories(locale, { |
| 53 | + ...baseProps, |
| 54 | + starts_with: 'shorts/', |
| 55 | + }) |
| 56 | + .then((shorts) => { |
| 57 | + setCarouselStories([ |
| 58 | + ...carouselStories, |
| 59 | + ...((shorts && |
| 60 | + filterResourcesForLocaleAndPartnerAccess(shorts, locale, userPartners)) || |
| 61 | + []), |
| 62 | + ]); |
| 63 | + }) |
| 64 | + .catch((error) => { |
| 65 | + console.error('Failed to fetch carousel shorts' + error); |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + // This is a placeholder for the courses carousel implementation |
| 70 | + if (resourceTypes.includes('conversations')) { |
| 71 | + // add try catches |
| 72 | + getStoryblokStories(locale, { |
| 73 | + ...baseProps, |
| 74 | + starts_with: 'conversations/', |
| 75 | + }) |
| 76 | + .then((conversations) => { |
| 77 | + setCarouselStories([ |
| 78 | + ...carouselStories, |
| 79 | + ...((conversations && |
| 80 | + filterResourcesForLocaleAndPartnerAccess(conversations, locale, userPartners)) || |
| 81 | + []), |
| 82 | + ]); |
| 83 | + }) |
| 84 | + .catch((error) => { |
| 85 | + console.error('Failed to fetch carousel conversation' + error); |
| 86 | + }); |
| 87 | + } |
| 88 | + // This is a placeholder for the courses carousel implementation |
| 89 | + if (resourceTypes.includes('courses')) { |
| 90 | + getStoryblokStories(locale, { |
| 91 | + ...baseProps, |
| 92 | + starts_with: 'courses/', |
| 93 | + }) |
| 94 | + .then((courses) => { |
| 95 | + setCarouselStories([...carouselStories, ...(courses || [])]); |
| 96 | + }) |
| 97 | + .catch((error) => { |
| 98 | + console.error('Failed to fetch carousel courses' + error); |
| 99 | + }); |
| 100 | + } |
| 101 | + } else { |
| 102 | + setCarouselStories(resources); |
| 103 | + } |
| 104 | + }, []); |
| 105 | + |
| 106 | + if (!resourceTypes && resources.length < 1) { |
| 107 | + console.error('ResourceCarousel: resourceTypes or resources must be provided'); |
| 108 | + return null; |
| 109 | + } |
| 110 | + |
| 111 | + const slidesPerView = { |
| 112 | + xs: 1, |
| 113 | + sm: 2, |
| 114 | + md: 3, |
| 115 | + lg: 3, |
| 116 | + xl: 3, |
| 117 | + }; |
| 118 | + return ( |
| 119 | + <Carousel |
| 120 | + title={title} |
| 121 | + theme="primary" |
| 122 | + showArrows={true} |
| 123 | + slidesPerView={slidesPerView} |
| 124 | + items={carouselStories.map((story) => { |
| 125 | + return ( |
| 126 | + (story.content.component === 'resource_short_video' && ( |
| 127 | + <Box p={0.25} minWidth="260px" width="260px" key={story.name}> |
| 128 | + <ShortsCard |
| 129 | + title={story.content.name} |
| 130 | + category={RESOURCE_CATEGORIES.SHORT_VIDEO} |
| 131 | + href={getDefaultFullSlug(story.full_slug, locale)} |
| 132 | + duration={story.content.duration} |
| 133 | + image={story.content.preview_image} |
| 134 | + /> |
| 135 | + </Box> |
| 136 | + )) || |
| 137 | + (story.content.component === 'resource_conversation' && ( |
| 138 | + <Box |
| 139 | + sx={{ |
| 140 | + ...getSlideWidth(1, 2, 3), |
| 141 | + }} |
| 142 | + padding={1} |
| 143 | + key={story.name} |
| 144 | + > |
| 145 | + <RelatedContentCard |
| 146 | + title={story.name} |
| 147 | + href={getDefaultFullSlug(story.full_slug, locale)} |
| 148 | + category={RESOURCE_CATEGORIES.CONVERSATION} |
| 149 | + duration={story.content.duration} |
| 150 | + /> |
| 151 | + </Box> |
| 152 | + )) |
| 153 | + ); |
| 154 | + })} |
| 155 | + /> |
| 156 | + ); |
| 157 | +}; |
| 158 | + |
| 159 | +export default ResourceCarousel; |
0 commit comments