import { useEffect, useState, } from 'react' import size from 'lodash/size' import includes from 'lodash/includes' import type { TournamentLanding } from 'requests/getTournamentLanding' import { getTournamentLanding } from 'requests/getTournamentLanding' import { PAGES } from 'config/pages' import { redirectToUrl } from 'helpers/redirectToUrl' import { useLexicsStore } from 'features/LexicsStore' import { useAuthStore } from 'features/AuthStore' import { getLandingName, isPastLandingDate } from './helpers' export const useTournamentLanding = () => { const [tournamentInfo, setTournamentInfo] = useState(null) const { addLexicsConfig } = useLexicsStore() const { page, setIsFromLanding } = useAuthStore() const buttonLexic = tournamentInfo?.lexic_button || '' const period = tournamentInfo?.lexic_period || '' const title = tournamentInfo?.lexic_title || '' const description = tournamentInfo?.lexic_description || '' const gallery = tournamentInfo?.media.gallery useEffect(() => { const lexics = [buttonLexic, period, title, description] addLexicsConfig(lexics) }, [ addLexicsConfig, buttonLexic, description, period, title, ]) const redirectToHomePage = () => redirectToUrl(PAGES.home) const onButtonClick = () => { if (includes(page, 'matches')) { setIsFromLanding(true) redirectToUrl(page) } else { redirectToUrl(tournamentInfo?.url_button || '') } } useEffect(() => { getTournamentLanding(getLandingName()) .then((data) => ( isPastLandingDate(data.date_to) ? redirectToHomePage() : setTournamentInfo(data) )) .catch(redirectToHomePage) }, []) const [sliderItemId, setSliderItemId] = useState(0) const onSliderSwitchClick = (itemId: number) => setSliderItemId(itemId) const imgCounter = size(gallery) useEffect(() => { if (sliderItemId === imgCounter) { setSliderItemId(0) } const getSliderInterval = setInterval(() => { setSliderItemId(sliderItemId + 1) }, 5000) return () => clearInterval(getSliderInterval) }, [imgCounter, sliderItemId]) return { buttonColor: tournamentInfo?.button_color, buttonLexic, description, gallery, logo: tournamentInfo?.media.logo, logoInsports: tournamentInfo?.logo_insports, onButtonClick, onSliderSwitchClick, period, redirectToHomePage, sliderItemId, teams: tournamentInfo?.teams, title, tournamentInfo, } }