import React, { useState } from 'react'; import { Placeholder } from '@times-components/image'; import { DeckData } from '../../helpers/fetch/types'; import { CarouselDataObj } from './types'; import { useFetch } from '../../helpers/fetch/FetchProvider'; import { sanitiseCopy } from '../../helpers/text-formatting/SanitiseCopy'; import { Card } from './Card'; import { Arrow } from './Arrow'; import { AspectRatio } from '../aspect-ratio/AspectRatio'; import { TrackingContextProvider } from '../../helpers/tracking/TrackingContextProvider'; import { PlaceholderContainer } from '../common-styles'; import { CarouselButtonContainer, CarouselButton, CarouselIndicatorContainer, CarouselIndicator, CarouselContainer, StyledCarousel, Copy, Credit, ImageTitle, MobileOrLarge } from './styles'; const CustomPagination: React.FC<{ activePage: number; current: number; onClick: (current: number, label?: string) => number; data: CarouselDataObj[]; }> = ({ activePage, onClick, current, data }) => { return ( onClick(current - 1, 'left')} > {data.map(({}, index) => { const isActivePage = activePage === index; return ( onClick(index)} active={isActivePage} /> ); })} onClick(current + 1, 'right')} > ); }; export type GalleryCarouselProps = { sectionColour: string; initialIndex?: number; }; export enum Layout { Small = '4033', Wide = '4035' } type GalleryCarouselFields = { headline: string; label: string; size: Layout }; type GalleryCarouselDeckData = DeckData; export const GalleryCarousel: React.FC = ({ sectionColour, initialIndex = 0 }) => { const { loading, error, data } = useFetch(); if (loading) { return ( ); } if (error || data === undefined || data.body.data.length === 0) { return null; } const { headline, label, size } = data.fields; const carouselData = data.body.data; const isSmall = (infoCardSize: Layout) => { return infoCardSize === Layout.Small; }; const isWide = (infoCardSize: Layout) => { return infoCardSize === Layout.Wide; }; const [current, setCurrent] = useState(initialIndex); const handleChange = (event: any) => { setCurrent(event.index); }; return ( {({ intersectObserverRef, fireAnalyticsEvent }) => ( { const handlePaginationClick = ( index: string, buttonLabel?: string ) => { if (buttonLabel) { fireAnalyticsEvent({ attrs: { event_navigation_name: `button : ${buttonLabel}`, component_name: headline, event_navigation_browsing_method: 'click' } }); } onClick && onClick(index); }; return ( {carouselData.length > 1 && ( )} ); }} > {/* @ts-ignore */} {carouselData.map(row => ( ))} {carouselData[current].data.credit} {carouselData[current].data.imageTitle && ( {carouselData[current].data.imageTitle} )} {carouselData[current].data.copy && ( )} )} ); };