Skip to content

A local-first personal finance app

fix moving categories between groups on mobile (#9213)

Jayesh Kugsiya3234be04d2ec
diff --git a/packages/desktop-client/e2e/budget.mobile.test.ts b/packages/desktop-client/e2e/budget.mobile.test.ts
index e2430a2ed2..7770f2c617 100644
--- a/packages/desktop-client/e2e/budget.mobile.test.ts
+++ b/packages/desktop-client/e2e/budget.mobile.test.ts
@@ -297,6 +297,26 @@ budgetTypes.forEach(budgetType => {
await expect(page).toMatchThemeScreenshots();
});
+ test('moves a category to another group with drag and drop', async () => {
+ // Keep both rows on screen so the drag doesn't scroll the page midway.
+ await page.setViewportSize({ width: 350, height: 1000 });
+ const budgetPage = await navigation.goToBudgetPage();
+
+ const giftRow = budgetPage.categoryRows.filter({ hasText: 'Gift' });
+ const internetRow = budgetPage.categoryRows.filter({
+ hasText: 'Internet',
+ });
+
+ // Drop on the top edge of "Internet" to place "Gift" before it.
+ await giftRow.dragTo(internetRow, { targetPosition: { x: 20, y: 5 } });
+
+ await expect(
+ page
+ .getByRole('grid', { name: 'Bills expense group categories' })
+ .getByTestId('category-name'),
+ ).toHaveText(['Cell', 'Gift', 'Internet', 'Mortgage', 'Water', 'Power']);
+ });
+
test('opens the transfer confirmation when deleting a category group with transactions', async () => {
const budgetPage = await navigation.goToBudgetPage();
diff --git a/packages/desktop-client/src/components/mobile/budget/ExpenseCategoryList.tsx b/packages/desktop-client/src/components/mobile/budget/ExpenseCategoryList.tsx
index bf24824a1a..6813d5a771 100644
--- a/packages/desktop-client/src/components/mobile/budget/ExpenseCategoryList.tsx
+++ b/packages/desktop-client/src/components/mobile/budget/ExpenseCategoryList.tsx
@@ -1,5 +1,10 @@
-import type { DragItem } from 'react-aria';
-import { DropIndicator, GridList, useDragAndDrop } from 'react-aria-components';
+import {
+ DropIndicator,
+ GridList,
+ isTextDropItem,
+ useDragAndDrop,
+} from 'react-aria-components';
+import type { ItemDropTarget } from 'react-aria-components';
import { useTranslation } from 'react-i18next';
import { theme } from '@actual-app/components/theme';
@@ -9,10 +14,15 @@ import type {
} from '@actual-app/core/types/models';
import { css } from '@emotion/css';
-import { useMoveCategoryMutation } from '#budget';
+import { useReorderCategoryMutation } from '#budget';
+import { findSortDown } from '#components/budget/util';
import { ExpenseCategoryListItem } from './ExpenseCategoryListItem';
+// A dedicated drag type keeps income categories and category groups out of
+// expense groups.
+const EXPENSE_CATEGORY_DRAG_TYPE = 'application/x-actual-expense-category';
+
type ExpenseCategoryListProps = {
categoryGroup: CategoryGroupEntity;
categories: CategoryEntity[];
@@ -35,16 +45,29 @@ export function ExpenseCategoryList({
shouldHideCategory,
}: ExpenseCategoryListProps) {
const { t } = useTranslation();
- const moveCategory = useMoveCategoryMutation();
+ const reorderCategory = useReorderCategoryMutation();
+
+ // Moves a dragged category into this group, before or after the drop target.
+ // Handles drops from this group and from other expense groups.
+ const moveCategoryToTarget = (
+ categoryId: CategoryEntity['id'],
+ { key: targetKey, dropPosition }: ItemDropTarget,
+ ) => {
+ reorderCategory.mutate({
+ id: categoryId,
+ groupId: categoryGroup.id,
+ ...findSortDown(
+ categories,
+ dropPosition === 'before' ? 'top' : 'bottom',
+ String(targetKey),
+ ),
+ });
+ };
const { dragAndDropHooks } = useDragAndDrop({
+ acceptedDragTypes: [EXPENSE_CATEGORY_DRAG_TYPE],
getItems: keys =>
- [...keys].map(
- key =>
- ({
- 'text/plain': key as CategoryEntity['id'],
- }) as DragItem,
- ),
+ [...keys].map(key => ({ [EXPENSE_CATEGORY_DRAG_TYPE]: String(key) })),
renderDropIndicator: target => {
return (
<DropIndicator
@@ -62,53 +85,15 @@ export function ExpenseCategoryList({
},
onReorder: e => {
const [key] = e.keys;
- const categoryIdToMove = key as CategoryEntity['id'];
- const categoryToMove = categories.find(c => c.id === categoryIdToMove);
-
- if (!categoryToMove) {
- throw new Error(
- `Internal error: category with ID ${categoryIdToMove} not found.`,
- );
- }
-
- if (!categoryToMove.group) {
- throw new Error(
- `Internal error: category ${categoryIdToMove} is not in a group and cannot be moved.`,
- );
- }
-
- const targetCategoryId = e.target.key as CategoryEntity['id'];
-
- if (e.target.dropPosition === 'before') {
- moveCategory.mutate({
- id: categoryToMove.id,
- groupId: categoryToMove.group,
- targetId: targetCategoryId,
- });
- } else if (e.target.dropPosition === 'after') {
- const targetCategoryIndex = categories.findIndex(
- c => c.id === targetCategoryId,
- );
-
- if (targetCategoryIndex === -1) {
- throw new Error(
- `Internal error: category with ID ${targetCategoryId} not found.`,
- );
- }
-
- const nextToTargetCategory = categories[targetCategoryIndex + 1];
-
- moveCategory.mutate({
- id: categoryToMove.id,
- groupId: categoryToMove.group,
- // Due to the way `moveCategory` works, we use the category next to the
- // actual target category here because `moveCategory` always shoves the
- // category *before* the target category.
- // On the other hand, using `null` as `targetId` moves the category
- // to the end of the list.
- targetId: nextToTargetCategory?.id || null,
- });
+ moveCategoryToTarget(String(key), e.target);
+ },
+ onInsert: async e => {
+ const [item] = e.items;
+ if (!item || !isTextDropItem(item)) {
+ return;
}
+ const categoryId = await item.getText(EXPENSE_CATEGORY_DRAG_TYPE);
+ moveCategoryToTarget(categoryId, e.target);
},
});
diff --git a/upcoming-release-notes/fix-mobile-category-group-drag.md b/upcoming-release-notes/fix-mobile-category-group-drag.md
new file mode 100644
index 0000000000..de8eac3eff
--- /dev/null
+++ b/upcoming-release-notes/fix-mobile-category-group-drag.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [jkugsiya]
+---
+
+Fix moving a category to another group with drag and drop on the mobile budget page