Newer
Older
import LeftArrowIcon from 'assets/icons/ico/left-arrow.svg'
import RightArrowIcon from 'assets/icons/ico/right-arrow.svg'
import StyledIconButton from 'components/CommonKit/IconButton/StyledIconButton'
import Content from 'components/Content/Content'
import CozyBar from 'components/Header/CozyBar'
import Header from 'components/Header/Header'
import { UserChallengeState } from 'enums'
import React, { useCallback, useEffect, useState } from 'react'
import { useAppSelector } from 'store/hooks'
import ChallengeCard from './ChallengeCard/ChallengeCard'
import './challengeView.scss'
const { userChallengeList } = useAppSelector(state => state.ecolyo.challenge)
window.outerWidth < 500 ? window.outerWidth - marginPx * 6 : 285
const cardHeight = window.outerHeight * 0.65
const [headerHeight, setHeaderHeight] = useState<number>(0)
const [touchStart, setTouchStart] = useState<number>()
const [touchEnd, setTouchEnd] = useState<number>()

Marlène SIMONDANT
committed
const [isLastDuelDone, setIsLastDuelDone] = useState<boolean>(false)
const [containerTranslation, setContainerTranslation] =
const resetValues = () => {
// Method used to cancel a swipe on a simple click
setTouchEnd(0)
setTouchStart(0)
}
const moveSliderRight = useCallback(() => {

Marlène SIMONDANT
committed
if (
index < userChallengeList.length - 1 ||
(isLastDuelDone && index < userChallengeList.length)
) {
setContainerTranslation(prev => prev - cardWidth - marginPx)
}, [cardWidth, index, isLastDuelDone, userChallengeList.length])
const moveSliderLeft = useCallback(() => {
setContainerTranslation(prev => prev + cardWidth + marginPx)
const moveToSlide = useCallback(
(slideIndex: number) => {
setContainerTranslation(-slideIndex * (cardWidth + marginPx) + marginPx)
setIndex(slideIndex)
const handleClickOrTouchEnd = () => {
// if the swipe is too small and can be taken for a touch
if (touchStart && touchEnd) {
if (touchStart - touchEnd < 5 && -5 < touchStart - touchEnd) return
// Change the following value in order to change the swipe sensibility
if (touchStart - touchEnd > 75) {
// If swipe left move slider right and add positive translation
moveSliderRight()
}
if (touchStart - touchEnd < -75) {
// If swipe right move slider left and add negative translation
let currentChallengeIndex = userChallengeList.findIndex(
challenge =>
challenge.state === UserChallengeState.UNLOCKED ||
challenge.state === UserChallengeState.ONGOING ||
challenge.state === UserChallengeState.DUEL
)
if (currentChallengeIndex === -1) {
currentChallengeIndex = isLastDuelDone ? userChallengeList.length : 0
}
setContainerTranslation(
-currentChallengeIndex * (cardWidth + marginPx) + marginPx
)
setIndex(currentChallengeIndex)
}, [userChallengeList, cardWidth, isLastDuelDone])

Marlène SIMONDANT
committed
useEffect(() => {
if (
userChallengeList[userChallengeList.length - 1].state ==
UserChallengeState.DONE
) {
setIsLastDuelDone(true)
}
}, [userChallengeList])
<CozyBar titleKey="common.title_challenge" />
desktopTitleKey="common.title_challenge"
<Content heightOffset={headerHeight}>
className="challengeSlider"
onTouchStart={event => setTouchStart(event.targetTouches[0].clientX)}
onTouchMove={event => setTouchEnd(event.targetTouches[0].clientX)}
onTouchEnd={handleClickOrTouchEnd}
onMouseDown={event => setTouchStart(event.clientX)}
onMouseMove={event => setTouchEnd(event.clientX)}
onMouseUp={handleClickOrTouchEnd}
style={{
transform: `translateX(${containerTranslation}px)`,
}}
{userChallengeList.map((userChallenge, i) => (
userChallenge={userChallenge}

Marlène SIMONDANT
committed
{isLastDuelDone && (
<ChallengeCard
indexSlider={index}

Marlène SIMONDANT
committed
cardHeight={cardHeight}
isChallengeCardLast={true}
moveToSlide={moveToSlide}
/>
)}
<div className="sliderButtons">
onClick={moveSliderLeft}
icon={LeftArrowIcon}
aria-label={t('challenge.accessibility.button_slider_previous')}
onClick={moveSliderRight}
icon={RightArrowIcon}
export default ChallengeView