From c8b3bd7e89b40c684eb2dbc3c235936575d1c2f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matilda=20Landstr=C3=B6m?= Date: Mon, 14 Oct 2024 08:44:56 +0200 Subject: [PATCH] refactor(SW-194): PR comment fixes --- components/Blocks/Accordion/index.tsx | 29 +++++++++---------- .../ContentType/HotelPage/Rooms/index.tsx | 17 +++++------ 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/components/Blocks/Accordion/index.tsx b/components/Blocks/Accordion/index.tsx index cdb47c01e..e8f3ae3d7 100644 --- a/components/Blocks/Accordion/index.tsx +++ b/components/Blocks/Accordion/index.tsx @@ -11,38 +11,37 @@ import ShowMoreButton from "@/components/TempDesignSystem/ShowMoreButton" import styles from "./accordion.module.css" import type { AccordionProps } from "@/types/components/blocks/Accordion" +import { HotelHashValues } from "@/types/components/hotelPage/tabNavigation" export default function AccordionSection({ accordion, title }: AccordionProps) { - const initialPageSize = 5 - const [pageSize, setPageSize] = useState(initialPageSize) - const showMoreVisible = accordion.length > initialPageSize - const showLessVisible = pageSize >= accordion.length + const showToggleButton = accordion.length > 5 + const [allAccordionsVisible, setAllAccordionsVisible] = + useState(!showToggleButton) - function handleShowMore() { - setPageSize(showLessVisible ? initialPageSize : accordion.length) + function toggleAccordions() { + setAllAccordionsVisible((state) => !state) } function getClassName(idx: number): string { - if (showMoreVisible && idx > pageSize - 1) { + if (!allAccordionsVisible && idx > 4) { return styles.hiddenItem - } else if (showMoreVisible && idx == pageSize - 1) { + } else if (!allAccordionsVisible && idx === 4) { return styles.lastItem } return "" } return ( - + {title && } - + {accordion.map((acc, idx: number) => ( @@ -50,10 +49,10 @@ export default function AccordionSection({ accordion, title }: AccordionProps) { ))} - {showMoreVisible ? ( + {showToggleButton ? ( diff --git a/components/ContentType/HotelPage/Rooms/index.tsx b/components/ContentType/HotelPage/Rooms/index.tsx index f783bf3dd..df1ce7b31 100644 --- a/components/ContentType/HotelPage/Rooms/index.tsx +++ b/components/ContentType/HotelPage/Rooms/index.tsx @@ -17,10 +17,9 @@ import { HotelHashValues } from "@/types/components/hotelPage/tabNavigation" export function Rooms({ rooms }: RoomsProps) { const intl = useIntl() - const initialPageSize = 5 - const [pageSize, setPageSize] = useState(initialPageSize) - const showMoreVisible = rooms.length > initialPageSize - const showLessVisible = pageSize >= rooms.length + const showToggleButton = rooms.length > 5 + const [allRoomsVisible, setAllRoomsVisible] = useState(!showToggleButton) + const scrollRef = useRef(null) const mappedRooms = rooms @@ -45,10 +44,10 @@ export function Rooms({ rooms }: RoomsProps) { .sort((a, b) => a.sortOrder - b.sortOrder) function handleShowMore() { - if (scrollRef.current && showMoreVisible) { + if (scrollRef.current && allRoomsVisible) { scrollRef.current.scrollIntoView({ behavior: "smooth" }) } - setPageSize(showLessVisible ? initialPageSize : rooms.length) + setAllRoomsVisible((state) => !state) } return ( @@ -68,7 +67,7 @@ export function Rooms({ rooms }: RoomsProps) {
2 ? styles.hiddenRoomCard : "" + !allRoomsVisible && index > 2 ? styles.hiddenRoomCard : "" } > - {showMoreVisible ? ( + {showToggleButton ? (