1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-25 04:46:40 +07:00

fixed hero slider

This commit is contained in:
Fedor Katurov 2020-11-06 12:02:46 +07:00
parent 558fbc703a
commit 916883ad16
2 changed files with 23 additions and 20 deletions

View file

@ -4,7 +4,7 @@ import classNames from 'classnames';
import * as styles from './styles.scss'; import * as styles from './styles.scss';
import { getURL } from '~/utils/dom'; 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 { URLS, PRESETS } from '~/constants/urls';
import { Icon } from '~/components/input/Icon'; import { Icon } from '~/components/input/Icon';
import { INode } from "~/redux/types"; import { INode } from "~/redux/types";
@ -13,11 +13,13 @@ type IProps = RouteComponentProps & {
heroes: IFlowState['heroes']; heroes: IFlowState['heroes'];
}; };
const FlowHeroUnconnected: FC<IProps> = ({ heroes, history }) => { const FlowHeroUnconnected: FC<IProps> = ({ heroes }) => {
const preset = useMemo(() => (window.innerWidth <= 768 ? PRESETS.cover : PRESETS.small_hero), []); const preset = useMemo(() => (window.innerWidth <= 768 ? PRESETS.cover : PRESETS.small_hero), []);
const [limit, setLimit] = useState(6); const [limit, setLimit] = useState(6);
const [current, setCurrent] = useState(0); const [current, setCurrent] = useState(0);
const [loaded, setLoaded] = useState<Partial<INode>[]>([]); const [loaded, setLoaded] = useState<Partial<INode>[]>([]);
const timer = useRef(null)
const history = useHistory();
const onLoad = useCallback((i: number) => { const onLoad = useCallback((i: number) => {
setLoaded([...loaded, heroes[i]]) setLoaded([...loaded, heroes[i]])
@ -29,9 +31,25 @@ const FlowHeroUnconnected: FC<IProps> = ({ heroes, history }) => {
return loaded[current]?.title || ''; return loaded[current]?.title || '';
}, [loaded, current, heroes]); }, [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 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 ( return (
<div className={styles.wrap}> <div className={styles.wrap}>
<div className={styles.loaders}> <div className={styles.loaders}>
@ -57,7 +75,7 @@ const FlowHeroUnconnected: FC<IProps> = ({ heroes, history }) => {
</div> </div>
)} )}
{loaded.length > 0 && loaded.slice(0, limit).map((hero, index) => ( {loaded.slice(0, limit).map((hero, index) => (
<div <div
className={classNames(styles.hero, { className={classNames(styles.hero, {
[styles.is_visible]: true, [styles.is_visible]: true,
@ -67,6 +85,7 @@ const FlowHeroUnconnected: FC<IProps> = ({ heroes, history }) => {
backgroundImage: `url("${getURL({ url: hero.thumbnail }, preset)}")`, backgroundImage: `url("${getURL({ url: hero.thumbnail }, preset)}")`,
}} }}
key={hero.id} key={hero.id}
onClick={goToNode}
> >
<img <img
src={getURL({ url: hero.thumbnail }, preset)} src={getURL({ url: hero.thumbnail }, preset)}

View file

@ -109,25 +109,11 @@
} }
} }
// .title {
// flex: 0;
// height: 48px;
// display: flex;
// align-items: center;
// justify-content: center;
// padding: 0 $gap 0 0;
// background: red;
// border-radius: $radius;
// font: $font_hero_title;
// text-transform: uppercase;
// }
.buttons { .buttons {
display: flex; display: flex;
align-items: center; align-items: center;
justify-content: center; justify-content: center;
height: 48px; height: 48px;
// background: rgba(0, 0, 0, 0.7);
flex-direction: row; flex-direction: row;
width: 96px; width: 96px;
border-radius: $radius; border-radius: $radius;
@ -146,7 +132,6 @@
} }
} }
// new
.loaders { .loaders {
position: absolute; position: absolute;
top: 0; top: 0;
@ -159,6 +144,5 @@
position: absolute; position: absolute;
left: 0; left: 0;
top: 0; top: 0;
} }
} }