Skip to content

Commit

Permalink
Fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
dro-1 committed Oct 31, 2021
1 parent 5d2be70 commit 59ee9d4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
22 changes: 10 additions & 12 deletions src/components/Collection/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React, { useEffect, useCallback, useRef } from "react";
import React, { useEffect, useRef } from "react";
import { useSelector, useDispatch } from "react-redux";
import { useCarousel } from "../common/Carousel";
import { ApplicationStore } from "../../redux/types";
import { setSelectedIndex } from "../../redux/App/actionCreators";
import { playNavigationAudio, pageLoading } from "../../helpers";
import { pageLoading } from "../../helpers";
import Collection from "./Collection";

const CollectionContainer: React.FC = () => {
Expand All @@ -15,23 +15,21 @@ const CollectionContainer: React.FC = () => {

const dispatch = useDispatch();

const onNavMove = useCallback(() => {
playNavigationAudio();
}, []);


useEffect(() => {
document.onkeydown = (e) => {
if (!pageLoading()) {
if (e.key === "ArrowRight") {
if (selectedIndex + 1 !== games.length)
if (selectedIndex + 1 !== games.length) {
dispatch(setSelectedIndex(selectedIndex + 1));
carouselInstance.carouselData?.scrollRight();
onNavMove();
carouselInstance.carouselData?.scrollRight();
}
} else if (e.key === "ArrowLeft") {
if (selectedIndex !== 0)
if (selectedIndex !== 0) {
dispatch(setSelectedIndex(selectedIndex - 1));
carouselInstance.carouselData?.scrollLeft();
onNavMove();
carouselInstance.carouselData?.scrollLeft();
}
}
}
};
Expand All @@ -40,7 +38,7 @@ const CollectionContainer: React.FC = () => {
carouselInstance.carouselData,
games.length,
dispatch,
onNavMove,
games
]);

useEffect(() => {
Expand Down
11 changes: 9 additions & 2 deletions src/components/common/Carousel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import React, {
} from "react";
import { CarouselProps, RefType, CarouselData } from "./types";
import Carousel from "./Carousel";
import { playNavigationAudio } from './../../../helpers'

export const useCarousel = () => {
const [carouselData, setCarouselData] = useState<CarouselData | null>(null);
Expand All @@ -31,15 +32,20 @@ const CarouselContainer: React.FC<CarouselProps> = ({
React.Children.map(children, () => createRef())
);

const onNavMove = useCallback(() => {
playNavigationAudio();
}, []);

const scrollRight = useCallback(
(step = 1) => {
if (transformCount.current < refs.length - numberToShow) {
transform.current = transform.current + step * itemWidth;
containerRef.current!.style.transform = `translateX(-${transform.current}px)`;
transformCount.current += 1;
onNavMove();
}
},
[itemWidth, transform, refs.length, numberToShow]
[itemWidth, transform, numberToShow, onNavMove, refs]
);

const scrollLeft = useCallback(
Expand All @@ -48,9 +54,10 @@ const CarouselContainer: React.FC<CarouselProps> = ({
transform.current = transform.current - step * itemWidth;
containerRef.current!.style.transform = `translateX(-${transform.current}px)`;
transformCount.current -= 1;
onNavMove();
}
},
[itemWidth, transform]
[itemWidth, transform, onNavMove]
);

useEffect(() => {
Expand Down

0 comments on commit 59ee9d4

Please sign in to comment.