From 916883ad16fa7183d9e0db400eed3c943f480468 Mon Sep 17 00:00:00 2001 From: Fedor Katurov Date: Fri, 6 Nov 2020 12:02:46 +0700 Subject: [PATCH] fixed hero slider --- src/components/flow/FlowHero/index.tsx | 27 ++++++++++++++++++++---- src/components/flow/FlowHero/styles.scss | 16 -------------- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/components/flow/FlowHero/index.tsx b/src/components/flow/FlowHero/index.tsx index fae9074d..9b795640 100644 --- a/src/components/flow/FlowHero/index.tsx +++ b/src/components/flow/FlowHero/index.tsx @@ -4,7 +4,7 @@ import classNames from 'classnames'; import * as styles from './styles.scss'; import { getURL } from '~/utils/dom'; -import { withRouter, RouteComponentProps } from 'react-router'; +import { withRouter, RouteComponentProps, useHistory } from 'react-router'; import { URLS, PRESETS } from '~/constants/urls'; import { Icon } from '~/components/input/Icon'; import { INode } from "~/redux/types"; @@ -13,11 +13,13 @@ type IProps = RouteComponentProps & { heroes: IFlowState['heroes']; }; -const FlowHeroUnconnected: FC = ({ heroes, history }) => { +const FlowHeroUnconnected: FC = ({ heroes }) => { const preset = useMemo(() => (window.innerWidth <= 768 ? PRESETS.cover : PRESETS.small_hero), []); const [limit, setLimit] = useState(6); const [current, setCurrent] = useState(0); const [loaded, setLoaded] = useState[]>([]); + const timer = useRef(null) + const history = useHistory(); const onLoad = useCallback((i: number) => { setLoaded([...loaded, heroes[i]]) @@ -29,9 +31,25 @@ const FlowHeroUnconnected: FC = ({ heroes, history }) => { return loaded[current]?.title || ''; }, [loaded, current, heroes]); - const onNext = useCallback(() => setCurrent(current < items - 1 ? current + 1 : 0), [current, items]) + const onNext = useCallback(() => { + if (heroes.length > limit) setLimit(limit + 1) + setCurrent(current < items - 1 ? current + 1 : 0) + }, [current, items, limit, heroes.length]) const onPrev = useCallback(() => setCurrent(current > 0 ? current - 1 : items - 1), [current, items]) + const goToNode = useCallback(() => { + history.push(URLS.NODE_URL(loaded[current].id)) + }, [current, loaded]); + + useEffect(() => { + timer.current = setTimeout(onNext, 5000) + return () => clearTimeout(timer.current) + }, [current, timer.current]) + + useEffect(() => { + if (loaded.length === 1) onNext() + }, [loaded]) + return (
@@ -57,7 +75,7 @@ const FlowHeroUnconnected: FC = ({ heroes, history }) => {
)} - {loaded.length > 0 && loaded.slice(0, limit).map((hero, index) => ( + {loaded.slice(0, limit).map((hero, index) => (
= ({ heroes, history }) => { backgroundImage: `url("${getURL({ url: hero.thumbnail }, preset)}")`, }} key={hero.id} + onClick={goToNode} >