diff --git a/src/components/searchApartment/SimilarSlider.tsx b/src/components/searchApartment/SimilarSlider.tsx
index a3bdbb5..d94e156 100644
--- a/src/components/searchApartment/SimilarSlider.tsx
+++ b/src/components/searchApartment/SimilarSlider.tsx
@@ -1,26 +1,26 @@
+/* eslint-disable react-hooks/exhaustive-deps */
/* eslint-disable @typescript-eslint/no-unused-vars */
import { useLayoutEffect, useRef, useState } from "react";
import { layoutsCards } from "../../consts/initialSearchPage";
import SimilarAppartmentCard from "./SimilarAppartmentCard";
import Button from "../Button";
import RightArrowIcon from "../icons/RightArrowIcon";
+import { useSwipeable } from "react-swipeable";
-const cols = 4;
+const desktopCols = 4;
+const laptopCols = 2;
const SimilarSlider = () => {
const [cards, setCards] = useState(layoutsCards);
const [offset, setOffset] = useState(0);
const cardRef = useRef
(null);
const [cardWidth, setCardWidth] = useState(0);
+ const [cols, setCols] = useState(laptopCols);
- useLayoutEffect(() => {
- const cardElement = cardRef.current;
- if (cardElement) {
- const gap = 16;
- const width = cardElement.clientWidth + gap;
- setCardWidth(width);
- }
- }, []);
+ const handlers = useSwipeable({
+ onSwipedLeft: () => handleOnRightBtnClick(),
+ onSwipedRight: () => handleOnLeftBtnClick(),
+ });
const handleOnLeftBtnClick = () => {
if (0 > offset) {
@@ -29,7 +29,7 @@ const SimilarSlider = () => {
};
const handleOnRightBtnClick = () => {
- if (offset > -cards.length + cols) {
+ if (offset > -cards.length + desktopCols) {
setOffset((prev) => prev - 1);
}
// if (offset === -cards.length + cols) {
@@ -39,13 +39,28 @@ const SimilarSlider = () => {
// }
};
+ useLayoutEffect(() => {
+ const cardElement = cardRef.current;
+ if (cardElement) {
+ const gap = 16;
+ const width = cardElement.clientWidth + gap;
+ setCardWidth(width);
+ }
+ }, [window.innerWidth]);
+
+ useLayoutEffect(() => {
+ const width = window.innerWidth;
+ const _cols = width < 1024 ? laptopCols : desktopCols;
+ setCols(_cols);
+ }, [window.innerWidth]);
+
return (
<>
Similar
-
-
-
+
+
-
+
{Array.from({ length: Math.floor(cards.length / cols) }).map(
(_, index) => {
return (
{cards
diff --git a/src/pages/SearchApartment.tsx b/src/pages/SearchApartment.tsx
index 2db4402..32f04e9 100644
--- a/src/pages/SearchApartment.tsx
+++ b/src/pages/SearchApartment.tsx
@@ -8,14 +8,14 @@ const SearchApartment = () => {
return (