Skip to content

Commit

Permalink
fix: disable go buttons when impossible to do so
Browse files Browse the repository at this point in the history
Signed-off-by: rare-magma <[email protected]>
  • Loading branch information
rare-magma committed Dec 24, 2023
1 parent 35b2bcd commit e7773f5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/components/NavBar/NavBar.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
color: var(--lightbgcolor);
}

.btn-go-button:disabled {
background-color: var(--lightbgcolor);
border: 1px solid var(--comment);
color: var(--comment);
}

.btn-outline-danger {
background-color: var(--lightbgcolor);
border: 1px solid var(--comment);
Expand Down
5 changes: 4 additions & 1 deletion src/components/NavBar/NavBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ export function NavBar() {
const [theme, setTheme] = useState("light");
const [invalidName, setInvalidName] = useState(false);

const { select, goBack, goForward, goHome } = useMove();
const { select, goBack, canGoBack, goForward, canGoForward, goHome } =
useMove();
const {
options,
createBudget,
Expand Down Expand Up @@ -172,6 +173,7 @@ export function NavBar() {
<NavBarItem
itemClassName={"me-1 my-2"}
onClick={goBack}
disabled={!canGoBack}
tooltipID={"tooltip-go-to-older-budget"}
tooltipText={"go to older budget"}
buttonAriaLabel={"go to older budget"}
Expand All @@ -181,6 +183,7 @@ export function NavBar() {
<NavBarItem
itemClassName={"m-2"}
onClick={goForward}
disabled={!canGoForward}
tooltipID={"tooltip-go-to-newer-budget"}
tooltipText={"go to newer budget"}
buttonAriaLabel={"go to newer budget"}
Expand Down
16 changes: 16 additions & 0 deletions src/hooks/useMove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,26 @@ export function useMove() {
budgetList && handleGo(1, budgetList.length - 1);
}

function checkCanGo(position: number): boolean {
const sortedList = budgetList?.sort((a, b) => a.name.localeCompare(b.name));
if (budget) {
const index = sortedList?.findIndex((b) => b.name.includes(budget.name));
if (index !== position) {
return true;
}
}
return false;
}

const canGoBack = checkCanGo(0);
const canGoForward = budgetList && checkCanGo(budgetList?.length - 1);

return {
select,
goBack,
goForward,
canGoBack,
canGoForward,
goHome,
};
}

0 comments on commit e7773f5

Please sign in to comment.