All files / src/Onboarding Onboarding.js

77.77% Statements 28/36
87.36% Branches 83/95
57.14% Functions 4/7
80% Lines 28/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232                                                                  1x                                               40x 40x 40x 40x 40x                         40x                   40x 24x               40x 40x                 40x   40x 40x 40x                           40x           40x                                                         40x                             25x 25x 25x 25x   25x 25x   25x   25x                                                                                             1x 1x    
import React from 'react'
 
/**propTypes**/
import { propTypes } from './props/propTypes'
import { defaultProps } from './props/defaultProps';
 
/**components**/
import Icon from '@zohodesk/icons/lib/Icon';
import Flex from '@zohodesk/layout/es/Flex/Flex'
import Button from '@zohodesk/components/lib/Button/Button';
import Modal from '@zohodesk/components/lib/Modal/Modal';
import Ribbon from '@zohodesk/components/es/Ribbon/Ribbon';
import Typography from '@zohodesk/components/es/Typography/Typography';
import btnStyle from '@zohodesk/components/es/semantic/Button/semanticButton.module.css';
import { useZIndex } from '@zohodesk/components/es/Provider/ZindexProvider';
 
/** local **/
import VideoLookup from '../VideoLookup/VideoLookup';
import Link from '../Link/Link';
import CarouselDots from './CarouselDots/CarouselDots';
import Image from '../Image/Image';
 
/** hooks **/
import useOnboardingSlider from './hooks/useOnboardingSlider';
import useMergeStyle from '@zohodesk/hooks/es/utils/useMergeStyle';
import useOnboarding from './hooks/useOnboarding';
 
/** Constant **/ 
import { DUMMY_OBJECT } from '../utils/General';
 
/** style **/
import defaultStyle from './css/Onboarding.module.css'
 
const Onboarding = ({
    hasCloseIcon,
    onClose,
    ribbonText,
    hasRibbon,
    videoLookupCloseText,
    customStyle,
    sliderData,
    isAutoPlayAnimation,
    hasBouncAnimationOnMount,
    customId,
    testId,
    hasExpandedButton,
    onPrimaryButtonClick,
    onSecondaryButtonClick,
    customProps,
    tagAttributes,
    a11yAttributes,
    closeIconTooltip,
    dragBoundaryLimit,
    isDraggable,
    position
}) => {
 
    const getNextIndex = useZIndex();
    const style = useMergeStyle(defaultStyle, customStyle);
    const { state, dragRef, openVideoLookup, closeVideoLookup } = useOnboarding({ dragBoundaryLimit, isDraggable: isDraggable && sliderData.length > 0 })
    const { videoLookupLink, isVideoLookupOpen, isEmbeddedLink, videoLookupFormat } = state
    const { primaryButtonProps, secondaryButtonProps, secondaryButtonLinkProps, ribbonProps, footerProps ,headingProps, descriptionProps,videoLookupProps} = customProps;
 
    
    const {
        sliderIndex,
        mainContainerEleRef,
        isAnimationPaused,
        translateMovementVal,
        handleMouseEnter,
        handleMouseLeave,
        handleSliderIndexChange,
        handleSliderNext,
        handleAnimationEnd
    } = useOnboardingSlider({
        isAutoPlayAnimation: isAutoPlayAnimation,
        sliderData: sliderData,
        animationClass: style.mainContainerAnimation
    });
 
 
 
 
 
    const renderMediaElement = (imageSource, videoSourceLink, videoSourceFormat) =>
        imageSource ? (
            <Image src={imageSource} className={`${style.mediaElement} ${style.image}`} />
        ) : videoSourceLink ? (
            <video className={style.mediaElement} autoPlay loop muted>
                <source src={videoSourceLink} type={`video/${videoSourceFormat}`}/>
            </video>
        ) : null;
 
    const renderFooter = (sliderData, currentSliderIndex) => {
        const currentSliderData = sliderData && sliderData[currentSliderIndex];
        const {
            hasPrimaryButton=true,
            hasSecondaryButton=false,
            hasSecondaryButtonLink=false,
            primaryButtonText='Got it',
            secondaryButtonText,
            secondaryButtonLink,
            tabName
        } = currentSliderData || DUMMY_OBJECT;
 
        const expandedClass = hasExpandedButton ? style.expandedButton : "";
        const isFinalSlide = sliderData && sliderData.length - 1 === currentSliderIndex;
        const handlePrimaryButtonClick = () => {
            if(typeof onPrimaryButtonClick == 'function') {
                onPrimaryButtonClick(tabName);
            }
            else{
                if(!isFinalSlide){
                    handleSliderNext();
                }
                else{
                    onClose && onClose();
                }
            }
        };
 
        const handleSecondaryButtonClick = () => {
            if(typeof onSecondaryButtonClick == 'function') {
                onSecondaryButtonClick(tabName);
            }
        }
 
        return (
            <Flex $ui_displayMode="flex" $ui_direction={hasExpandedButton?"column":"row"} $ui_wrapMode={hasExpandedButton?"wrap":undefined} $ui_alignItems="center" $ui_className={style.footer} {...footerProps}>
                {sliderData.length > 1 && (
                    <CarouselDots
                        sliderDetails={sliderData}
                        currentSliderIndex={currentSliderIndex}
                        isAnimationPaused={isAnimationPaused}
                        onClick={handleSliderIndexChange}
                        customId={`${customId}_carousel_dots`}
                        testId={`${testId}_carousel_dots`}
                    />
                )}
                <Flex $ui_displayMode="flex" $ui_justifyContent="end" $flag_flexible $ui_wrapMode={hasExpandedButton?"wrap":undefined} $ui_className={style.btnWrapper}>
                    {hasSecondaryButton && (
                        hasSecondaryButtonLink ?
                        <Link href={secondaryButtonLink} title={secondaryButtonText} dataId={`${customId}_secondary_button_link`} target="_blank" className={`${style.secondaryButtton} ${style.secondaryButttonLink} ${style.footerButton} ${expandedClass}`} {...secondaryButtonLinkProps}>
                            {secondaryButtonText}
                        </Link>
                        :
                        <Button dataId={`${customId}_secondary_button`} title={secondaryButtonText} text={secondaryButtonText} onClick={handleSecondaryButtonClick}  customClass={{ customButton: ` ${style.secondaryButtton} ${style.footerButton} ${expandedClass}` }} {...secondaryButtonProps}/>
                    )}
                    {hasPrimaryButton && (
                        <Button dataId={`${customId}_primary_button`} title={primaryButtonText} text={primaryButtonText} palette="primaryFilled" customClass={{ customButton: `${style.primaryButton} ${style.footerButton} ${expandedClass}` }} onClick={handlePrimaryButtonClick} {...primaryButtonProps} />
                    )}
                </Flex>
            </Flex>
        );
    };
 
    return (
        <div data-drag-container={isDraggable?'true':'false'} style={(position !== 'static' || isDraggable ) ? {zIndex:getNextIndex()} : null } ref={mainContainerEleRef} data-id={`${customId}_container`} data-test-id={`${testId}_container`} className={`${style.mainContainer} ${position !== 'static' ? style[`positon_${position}`] : ''} ${hasBouncAnimationOnMount?style.mainContainerAnimation:""}`} onAnimationEnd={hasBouncAnimationOnMount ? handleAnimationEnd : null} {...tagAttributes} {...a11yAttributes}>
            <div className={style.mainWrapper} onMouseEnter={handleMouseEnter} onMouseLeave={handleMouseLeave}>
                <div className={style.gradientLayer}>
                    <div className={`${style.circleBlur} ${style.circleBlurOne}`}></div>
                    <div className={`${style.circleBlur} ${style.circleBlurTwo}`}></div>
                </div>
                {Array.isArray(sliderData) && sliderData.length > 0 ? (
                    <div className={style.carousel} ref={isDraggable?dragRef:null} data-drag-hook={isDraggable?"true":"false"}>
                        <Flex
                            $ui_displayMode='flex'
                            $tagAttributes_container={{ style: { '--local_translate_movement': `${translateMovementVal}%` } }}
                            $ui_className={style.carouselInner}    
                        >
                            {sliderData.map((item, index) => {
                                const { imageSource, videoSource, mediaElement, heading, description, hasVideoPlayButton = false, videoLookupSource, tabName } = item;
                                const videoLookupSourceLink = videoLookupSource && videoLookupSource.link ? videoLookupSource.link : null;
                                const videoLookupSourceFormat = videoLookupSource && videoLookupSource.format ? videoLookupSource.format : null;
                                const isEmbeddedvideoLookupSource = videoLookupSource && videoLookupSource.isEmbeddedLink ? videoLookupSource.isEmbeddedLink : null;
 
                                const videoSourceLink = videoSource && videoSource.link ? videoSource.link : null;
                                const videoSourceFormat = videoSource && videoSource.format ? videoSource.format : null;
 
                                const handleOpenVideoLookup = () => openVideoLookup(videoLookupSourceLink, isEmbeddedvideoLookupSource,videoLookupSourceFormat)
 
                                return (
                                    <Flex $ui_displayMode='flex' $ui_direction='column' $flag_shrink={false} $ui_className={style.carouselItem} key={tabName}>
                                        <Flex $ui_className={`${style.header}`} $ui_displayMode='flex' $ui_justifyContent='center' $ui_alignItems='center' $flag_fullsize >
                                            <div className={`${style.innerHead} ${hasVideoPlayButton ? style.dvLayer : ''}`}>
                                                {mediaElement ? mediaElement : renderMediaElement(imageSource, videoSourceLink, videoSourceFormat)}
                                                {hasVideoPlayButton && (
                                                    <Flex $ui_displayMode='flex' $ui_justifyContent='center' $ui_alignItems='center' $ui_className={style.playIconWrapper}>
                                                        <Flex 
                                                            $ui_displayMode='flex' 
                                                            $ui_justifyContent='center' 
                                                            $ui_alignItems='center'
                                                            $ui_tagName='button'
                                                            customId={`${customId}_play_icon`} testId={`${testId}_play_icon`}
                                                            $ui_className={`${style.playIcon} ${style.animate} ${btnStyle.buttonReset}`}
                                                            $event_onClick={videoLookupSourceLink ? handleOpenVideoLookup :null}
                                                        >
                                                            <Icon name='ZD-GN-arrowFill' iconClass={style.playIconInner} />
                                                        </Flex>
                                                    </Flex>
                                                )}
                                            </div>
 
                                        </Flex>
                                        <Flex $ui_scroll="vertical" $ui_className={style.content}>
                                            <Typography $ui_size='20' $ui_lineClamp='3' $ui_weight='bold' $ui_className={style.heading} $i18n_dataTitle={heading} $ui_lineHeight='1.4'  {...headingProps} >{heading}</Typography>
                                            <Typography $ui_size='14' $ui_lineHeight='1.6'  $ui_lineClamp='4' $i18n_dataTitle={description} $ui_className={style.description}  {...descriptionProps}>{description}</Typography>
                                        </Flex>
                                    </Flex>
                                );
                            })}
                        </Flex>
                    </div>) : null}
                {renderFooter(sliderData, sliderIndex)}
                {/* Close icon */}
                {hasCloseIcon ? <Flex $ui_tagName='button' $tagAttributes_container={{'data-title':closeIconTooltip}} $ui_displayMode='flex' customId={`${customId}_close_icon`} testId={`${testId}_close_icon`} $ui_justifyContent='center' $ui_alignItems='center' $ui_className={`${style.closeIconContainer} ${btnStyle.buttonReset}`} $event_onClick={onClose}>
                    <Icon name='ZD-inappClose' iconClass={style.closeIcon} />
                </Flex> : null}
                {/* Ribbon */}
                {hasRibbon ? <Ribbon dataId={`${customId}_ribbon`} palette='primary' size='medium' customClass={style.newRibbon} type='flag' text={ribbonText} {...ribbonProps}/> : null}
                {/* video lookup */}
                {isVideoLookupOpen ? <Modal ><VideoLookup videoFormat={videoLookupFormat} onClose={closeVideoLookup} isEmbeddedLink={isEmbeddedLink} link={videoLookupLink} isOpened={isVideoLookupOpen} closeText={videoLookupCloseText} {...videoLookupProps} /></Modal> : null}
            </div>
        </div>
    )
}
 
export default Onboarding
Onboarding.propTypes = propTypes;
Onboarding.defaultProps = defaultProps;