From 119d0b339dfa93866ecb2c53b4203d60d7bf856b Mon Sep 17 00:00:00 2001
From: Jordan Lees <jordanlees@mailbox.org>
Date: Mon, 12 Aug 2024 01:49:17 -0400
Subject: [PATCH] Fix off-by-one error when placing category into 2nd-to-last
 place (#3241)

* Fix off-by-one error when placing category into 2nd-to-last place

Signed-off-by: JL102 <jordanlees@mailbox.org>

* Add changelog file

Signed-off-by: JL102 <jordanlees@mailbox.org>

---------

Signed-off-by: JL102 <jordanlees@mailbox.org>
---
 .../src/components/budget/BudgetTable.jsx              | 10 ++++++++--
 packages/desktop-client/src/components/budget/util.ts  |  2 +-
 upcoming-release-notes/3241.md                         |  6 ++++++
 3 files changed, 15 insertions(+), 3 deletions(-)
 create mode 100644 upcoming-release-notes/3241.md

diff --git a/packages/desktop-client/src/components/budget/BudgetTable.jsx b/packages/desktop-client/src/components/budget/BudgetTable.jsx
index 76ff7a605..24d72bf87 100644
--- a/packages/desktop-client/src/components/budget/BudgetTable.jsx
+++ b/packages/desktop-client/src/components/budget/BudgetTable.jsx
@@ -9,7 +9,12 @@ import { BudgetCategories } from './BudgetCategories';
 import { BudgetSummaries } from './BudgetSummaries';
 import { BudgetTotals } from './BudgetTotals';
 import { MonthsProvider } from './MonthsContext';
-import { findSortDown, findSortUp, getScrollbarWidth } from './util';
+import {
+  findSortDown,
+  findSortUp,
+  getScrollbarWidth,
+  separateGroups,
+} from './util';
 
 export function BudgetTable(props) {
   const {
@@ -86,9 +91,10 @@ export function BudgetTable(props) {
   };
 
   const _onReorderGroup = (id, dropPos, targetId) => {
+    const [expenseGroups] = separateGroups(categoryGroups); // exclude Income group from sortable groups to fix off-by-one error
     onReorderGroup({
       id,
-      ...findSortDown(categoryGroups, dropPos, targetId),
+      ...findSortDown(expenseGroups, dropPos, targetId),
     });
   };
 
diff --git a/packages/desktop-client/src/components/budget/util.ts b/packages/desktop-client/src/components/budget/util.ts
index e0153e152..a3658651e 100644
--- a/packages/desktop-client/src/components/budget/util.ts
+++ b/packages/desktop-client/src/components/budget/util.ts
@@ -103,7 +103,7 @@ export function findSortDown(
     }
 
     const newIdx = idx + 1;
-    if (newIdx < arr.length - 1) {
+    if (newIdx < arr.length) {
       return { targetId: arr[newIdx].id };
     } else {
       // Move to the end
diff --git a/upcoming-release-notes/3241.md b/upcoming-release-notes/3241.md
new file mode 100644
index 000000000..8da4780f0
--- /dev/null
+++ b/upcoming-release-notes/3241.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [JL102]
+---
+
+Fixed category appearing in last slot when you drag it to the second-to-last slot
-- 
GitLab