Skip to content

Commit

Permalink
Fixed paging logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaddox5 committed Nov 6, 2024
1 parent 8f43924 commit b5214b8
Showing 1 changed file with 36 additions and 16 deletions.
52 changes: 36 additions & 16 deletions assets/src/components/v2/elevator/elevator_closures.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, {
ComponentType,
useEffect,
useLayoutEffect,
useMemo,
useState,
} from "react";
import NormalService from "Images/svgr_bundled/normal-service.svg";
Expand Down Expand Up @@ -78,7 +79,17 @@ const OutsideAlertList = ({
}: OutsideAlertListProps) => {
const [isFirstRender, setIsFirstRender] = useState(true);
const [pageIndex, setPageIndex] = useState(0);
const [numPages, setNumPages] = useState(0);

// Each value represents the pageIndex the row is visible on
const [rowPageIndexes, setRowPageIndexes] = useState<number[]>([]);

const [numPages, numOffsetRows] = useMemo(
() => [
rowPageIndexes.filter((val, i, self) => self.indexOf(val) === i).length,
rowPageIndexes.filter((offset) => offset !== pageIndex).length,
],
[rowPageIndexes],
);

useEffect(() => {
if (lastUpdate != null) {
Expand All @@ -97,14 +108,31 @@ const OutsideAlertList = ({
}, [pageIndex]);

useLayoutEffect(() => {
const closureRows = document.getElementsByClassName("alert-row");
const uniqueOffsets = Array.from(closureRows)
.map((closure) => (closure as HTMLDivElement).offsetLeft)
.filter((val, i, self) => self.indexOf(val) === i);
const closureRows = Array.from(
document.getElementsByClassName("alert-row"),
);

const rowPageIndexes = closureRows.map((closure) => {
const val = (closure as HTMLDivElement).offsetLeft - 48;
return val / 1080;
});

setNumPages(uniqueOffsets.length);
setRowPageIndexes(rowPageIndexes);
}, []);

const getPagingIndicators = (num: number) => {
const indicators: JSX.Element[] = [];
for (let i = 0; i < num; i++) {
const indicator =
pageIndex === i ? (
<PagingDotSelected key={i} />
) : (
<PagingDotUnselected key={i} />
);
indicators.push(indicator);
}
};

return (
<div className="outside-alert-list">
<div className="header-container">
Expand Down Expand Up @@ -133,16 +161,8 @@ const OutsideAlertList = ({
}
</div>
<div className="paging-info-container">
<div>+{alerts.length} more elevators</div>
<div className="paging-indicators">
{[...Array(numPages)].map((_, i) => {
return pageIndex === i ? (
<PagingDotSelected key={i} />
) : (
<PagingDotUnselected key={i} />
);
})}
</div>
<div>+{numOffsetRows} more elevators</div>
<div className="paging-indicators">{getPagingIndicators(numPages)}</div>
</div>
</div>
);
Expand Down

0 comments on commit b5214b8

Please sign in to comment.