Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mobile category and group functionalities #1737

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions packages/desktop-client/src/components/Modals.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import ManageRulesModal from './modals/ManageRulesModal';
import MergeUnusedPayees from './modals/MergeUnusedPayees';
import PlaidExternalMsg from './modals/PlaidExternalMsg';
import SelectLinkedAccounts from './modals/SelectLinkedAccounts';
import SingleInput from './modals/SingleInput';
import DiscoverSchedules from './schedules/DiscoverSchedules';
import ScheduleDetails from './schedules/EditSchedule';
import ScheduleLink from './schedules/LinkSchedule';
Expand Down Expand Up @@ -222,6 +223,30 @@ export default function Modals() {
/>
);

case 'new-category':
return (
<SingleInput
modalProps={modalProps}
title="New Category"
inputPlaceholder="Name"
buttonText="Add"
onValidate={options.onValidate}
onSubmit={options.onSubmit}
/>
);

case 'new-category-group':
return (
<SingleInput
modalProps={modalProps}
title="New Category Group"
inputPlaceholder="Name"
buttonText="Add"
onValidate={options.onValidate}
onSubmit={options.onSubmit}
/>
);

case 'budget-summary':
return (
<BudgetSummary
Expand Down
6 changes: 3 additions & 3 deletions packages/desktop-client/src/components/budget/BudgetTotals.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const BudgetTotals = memo(function BudgetTotals({
>
<Menu
onMenuSelect={type => {
if (type === 'toggleVisibility') {
if (type === 'toggle-visibility') {
toggleHiddenCategories();
} else if (type === 'expandAllCategories') {
expandAllCategories();
Expand All @@ -80,8 +80,8 @@ const BudgetTotals = memo(function BudgetTotals({
}}
items={[
{
name: 'toggleVisibility',
text: 'Toggle hidden',
name: 'toggle-visibility',
text: 'Toggle hidden categories',
},
{
name: 'expandAllCategories',
Expand Down
127 changes: 112 additions & 15 deletions packages/desktop-client/src/components/budget/MobileBudget.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ import { useSpreadsheet } from 'loot-core/src/client/SpreadsheetProvider';
import { send, listen } from 'loot-core/src/platform/client/fetch';
import {
addCategory,
addGroup,
deleteCategory,
deleteGroup,
moveCategory,
moveCategoryGroup,
updateCategory,
updateGroup,
} from 'loot-core/src/shared/categories';
import * as monthUtils from 'loot-core/src/shared/months';

Expand All @@ -24,15 +29,13 @@ class Budget extends Component {
constructor(props) {
super(props);

this.summary = 0;

const currentMonth = monthUtils.currentMonth();
this.state = {
bounds: { start: currentMonth, end: currentMonth },
currentMonth: currentMonth,
initialized: false,
editMode: false,
categoryGroups: null,
categoryGroups: [],
};
}

Expand Down Expand Up @@ -72,7 +75,7 @@ class Budget extends Component {
}

componentWillUnmount() {
// this.cleanup();
this.cleanup?.();
}

prewarmMonth = async (month, type = null) => {
Expand Down Expand Up @@ -101,25 +104,112 @@ class Budget extends Component {
this.props.applyBudgetAction(currentMonth, type, this.state.bounds);
};

onAddCategory = groupId => {
this.props.navigation.navigate('AddCategoryModal', {
groupId,
onAdd: async name => {
let id = await this.props.createCategory(name, groupId);
let { categoryGroups } = this.state;

this.setState({
categoryGroups: addCategory(categoryGroups, {
onAddGroup = () => {
this.props.pushModal('new-category-group', {
onValidate: name => (!name ? 'Name is required.' : null),
onSubmit: async name => {
const id = await this.props.createGroup(name);
this.setState(state => ({
categoryGroups: addGroup(state.categoryGroups, {
id,
name,
cat_group: groupId,
categories: [],
is_income: 0,
}),
}));
},
});
};

onAddCategory = (groupId, isIncome) => {
this.props.pushModal('new-category', {
onValidate: name => (!name ? 'Name is required.' : null),
onSubmit: async name => {
const id = await this.props.createCategory(name, groupId, isIncome);
this.setState(state => ({
categoryGroups: addCategory(state.categoryGroups, {
id,
name,
cat_group: groupId,
is_income: isIncome ? 1 : 0,
}),
});
}));
},
});
};

onSaveGroup = group => {
this.props.updateGroup(group);
this.setState(state => ({
categoryGroups: updateGroup(state.categoryGroups, group),
}));
};

onDeleteGroup = async groupId => {
let group = this.state.categoryGroups?.find(g => g.id === groupId);

if (!group) {
return;
}

let mustTransfer = false;
for (let category of group.categories) {
if (await send('must-category-transfer', { id: category.id })) {
mustTransfer = true;
break;
}
}

if (mustTransfer) {
this.props.pushModal('confirm-category-delete', {
group: groupId,
onDelete: transferCategory => {
this.props.deleteGroup(groupId, transferCategory);
this.setState(state => ({
categoryGroups: deleteGroup(state.categoryGroups, groupId),
}));
},
});
} else {
this.props.deleteGroup(groupId);
this.setState(state => ({
categoryGroups: deleteGroup(state.categoryGroups, groupId),
}));
}
};

onSaveCategory = category => {
this.props.updateCategory(category);
this.setState(state => ({
categoryGroups: updateCategory(state.categoryGroups, category),
}));
};

onDeleteCategory = async categoryId => {
const mustTransfer = await send('must-category-transfer', {
id: categoryId,
});

if (mustTransfer) {
this.props.pushModal('confirm-category-delete', {
category: categoryId,
onDelete: transferCategory => {
if (categoryId !== transferCategory) {
this.props.deleteCategory(categoryId, transferCategory);
this.setState(state => ({
categoryGroups: deleteCategory(state.categoryGroups, categoryId),
}));
}
},
});
} else {
this.props.deleteCategory(categoryId);
this.setState(state => ({
categoryGroups: deleteCategory(state.categoryGroups, categoryId),
}));
}
};

onReorderCategory = (id, { inGroup, aroundCategory }) => {
let { categoryGroups } = this.state;
let groupId, targetId;
Expand Down Expand Up @@ -237,6 +327,7 @@ class Budget extends Component {
categories,
categoryGroups,
prefs,
savePrefs,
budgetType,
navigation,
applyBudgetAction,
Expand Down Expand Up @@ -281,11 +372,17 @@ class Budget extends Component {
onShowBudgetDetails={this.onShowBudgetDetails}
onPrevMonth={this.onPrevMonth}
onNextMonth={this.onNextMonth}
onSaveGroup={this.onSaveGroup}
onDeleteGroup={this.onDeleteGroup}
onAddGroup={this.onAddGroup}
onAddCategory={this.onAddCategory}
onSaveCategory={this.onSaveCategory}
onDeleteCategory={this.onDeleteCategory}
onReorderCategory={this.onReorderCategory}
onReorderGroup={this.onReorderGroup}
onOpenActionSheet={() => {}} //this.onOpenActionSheet}
onBudgetAction={applyBudgetAction}
savePrefs={savePrefs}
/>
)}
</SyncRefresh>
Expand Down
Loading