Skip to content

Commit

Permalink
Merge branch 'master' into jed/type-modals
Browse files Browse the repository at this point in the history
  • Loading branch information
j-f1 authored Jul 18, 2023
2 parents 7065212 + 77ef864 commit 4cdc375
Show file tree
Hide file tree
Showing 6 changed files with 378 additions and 22 deletions.
156 changes: 152 additions & 4 deletions packages/desktop-client/src/components/accounts/Account.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,23 @@ function AllTransactions({ account = {}, transactions, filtered, children }) {
return children(allTransactions);
}

function getField(field) {
switch (field) {
case 'account':
return 'account.name';
case 'payee':
return 'payee.name';
case 'category':
return 'category.name';
case 'payment':
return 'amount';
case 'deposit':
return 'amount';
default:
return field;
}
}

class AccountInternal extends PureComponent {
constructor(props) {
super(props);
Expand All @@ -142,6 +159,7 @@ class AccountInternal extends PureComponent {
latestDate: null,
filterId: [],
conditionsOp: 'and',
sort: [],
};
}

Expand Down Expand Up @@ -232,6 +250,11 @@ class AccountInternal extends PureComponent {
this.refetchTransactions();
}, 100);
}

//Resest sort/filter/search on account change
if (this.props.accountId !== prevProps.accountId) {
this.setState({ sort: [], search: '', filters: [] });
}
}

componentWillUnmount() {
Expand Down Expand Up @@ -450,7 +473,12 @@ class AccountInternal extends PureComponent {
let accountId = this.props.accountId;
let account = this.props.accounts.find(account => account.id === accountId);
return (
account && this.state.search === '' && this.state.filters.length === 0
account &&
this.state.search === '' &&
this.state.filters.length === 0 &&
(this.state.sort.length === 0 ||
(this.state.sort.field === 'date' &&
this.state.sort.ascDesc === 'desc'))
);
};

Expand Down Expand Up @@ -523,11 +551,32 @@ class AccountInternal extends PureComponent {
this.props.savePrefs({ ['show-balances-' + accountId]: false });
this.setState({ showBalances: false, balances: [] });
} else {
this.setState({
transactions: [],
transactionCount: 0,
filters: [],
search: '',
sort: [],
showBalances: true,
});
this.fetchTransactions();
this.props.savePrefs({ ['show-balances-' + accountId]: true });
this.setState({ showBalances: true });
this.calculateBalances();
}
break;
case 'remove-sorting': {
let filters = this.state.filters;
this.setState({ sort: [] });
if (filters.length > 0) {
this.applyFilters([...filters]);
} else {
this.fetchTransactions();
}
if (this.state.search !== '') {
this.onSearch(this.state.search);
}
break;
}
case 'toggle-cleared':
if (this.state.showCleared) {
this.props.savePrefs({ ['hide-cleared-' + accountId]: true });
Expand Down Expand Up @@ -815,6 +864,9 @@ class AccountInternal extends PureComponent {
this.setState({ conditionsOp: value });
this.setState({ filterId: { ...this.state.filterId, status: 'changed' } });
this.applyFilters([...filters]);
if (this.state.search !== '') {
this.onSearch(this.state.search);
}
};

onReloadSavedFilter = (savedFilter, item) => {
Expand All @@ -837,6 +889,9 @@ class AccountInternal extends PureComponent {
this.setState({ conditionsOp: 'and' });
this.setState({ filterId: [] });
this.applyFilters([]);
if (this.state.search !== '') {
this.onSearch(this.state.search);
}
};

onUpdateFilter = (oldFilter, updatedFilter) => {
Expand All @@ -849,6 +904,9 @@ class AccountInternal extends PureComponent {
status: this.state.filterId && 'changed',
},
});
if (this.state.search !== '') {
this.onSearch(this.state.search);
}
};

onDeleteFilter = filter => {
Expand All @@ -864,6 +922,9 @@ class AccountInternal extends PureComponent {
},
});
}
if (this.state.search !== '') {
this.onSearch(this.state.search);
}
};

onApplyFilter = async cond => {
Expand All @@ -884,6 +945,9 @@ class AccountInternal extends PureComponent {
});
this.applyFilters([...filters, cond]);
}
if (this.state.search !== '') {
this.onSearch(this.state.search);
}
};

onScheduleAction = async (name, ids) => {
Expand Down Expand Up @@ -918,11 +982,91 @@ class AccountInternal extends PureComponent {
[conditionsOpKey]: [...filters, ...customFilters],
});
this.updateQuery(this.currentQuery, true);
this.setState({ filters: conditions, search: '' });
this.setState({ filters: conditions });
} else {
this.setState({ transactions: [], transactionCount: 0 });
this.fetchTransactions();
this.setState({ filters: conditions, search: '' });
this.setState({ filters: conditions });
}

if (this.state.sort.length !== 0) {
this.applySort();
}
};

applySort = (field, ascDesc, prevField, prevAscDesc) => {
let filters = this.state.filters;
let sortField = getField(!field ? this.state.sort.field : field);
let sortAscDesc = !ascDesc ? this.state.sort.ascDesc : ascDesc;
let sortPrevField = getField(
!prevField ? this.state.sort.prevField : prevField,
);
let sortPrevAscDesc = !prevField
? this.state.sort.prevAscDesc
: prevAscDesc;

if (!field) {
//no sort was made (called by applyFilters)
this.currentQuery = this.currentQuery.orderBy({
[sortField]: sortAscDesc,
});
} else {
//sort called directly
if (filters.length > 0) {
//if filters already exist then apply them
this.applyFilters([...filters]);
this.currentQuery = this.currentQuery.orderBy({
[sortField]: sortAscDesc,
});
} else {
//no filters exist make new rootquery
this.currentQuery = this.rootQuery.orderBy({
[sortField]: sortAscDesc,
});
}
}
if (sortPrevField) {
//apply previos sort if it exists
this.currentQuery = this.currentQuery.orderBy({
[sortPrevField]: sortPrevAscDesc,
});
}

this.updateQuery(this.currentQuery, this.state.filters.length > 0);
};

onSort = (headerClicked, ascDesc) => {
let prevField;
let prevAscDesc;
//if staying on same column but switching asc/desc
//then keep prev the same
if (headerClicked === this.state.sort.field) {
prevField = this.state.sort.prevField;
prevAscDesc = this.state.sort.prevAscDesc;
this.setState({
sort: {
...this.state.sort,
ascDesc: ascDesc,
},
});
} else {
//if switching to new column then capture state
//of current sort column as prev
prevField = this.state.sort.field;
prevAscDesc = this.state.sort.ascDesc;
this.setState({
sort: {
field: headerClicked,
ascDesc: ascDesc,
prevField: this.state.sort.field,
prevAscDesc: this.state.sort.ascDesc,
},
});
}

this.applySort(headerClicked, ascDesc, prevField, prevAscDesc);
if (this.state.search !== '') {
this.onSearch(this.state.search);
}
};

Expand Down Expand Up @@ -1010,6 +1154,7 @@ class AccountInternal extends PureComponent {
showEmptyMessage={showEmptyMessage}
balanceQuery={balanceQuery}
canCalculateBalance={this.canCalculateBalance}
isSorted={this.state.sort.length !== 0}
reconcileAmount={reconcileAmount}
search={this.state.search}
filters={this.state.filters}
Expand Down Expand Up @@ -1095,6 +1240,9 @@ class AccountInternal extends PureComponent {
</View>
) : null
}
onSort={this.onSort}
sortField={this.state.sort.field}
ascDesc={this.state.sort.ascDesc}
onChange={this.onTransactionsChange}
onRefetch={this.refetchTransactions}
onRefetchUpToRow={row =>
Expand Down
18 changes: 16 additions & 2 deletions packages/desktop-client/src/components/accounts/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export function AccountHeader({
balanceQuery,
reconcileAmount,
canCalculateBalance,
isSorted,
search,
filters,
conditionsOp,
Expand Down Expand Up @@ -350,6 +351,7 @@ export function AccountHeader({
account={account}
canSync={canSync}
canShowBalances={canCalculateBalance()}
isSorted={isSorted}
showBalances={showBalances}
showCleared={showCleared}
onMenuSelect={item => {
Expand All @@ -372,6 +374,7 @@ export function AccountHeader({
onMenuSelect(item);
}}
onClose={() => setMenuOpen(false)}
isSorted={isSorted}
/>
)}
</View>
Expand Down Expand Up @@ -411,6 +414,7 @@ function AccountMenu({
canShowBalances,
showCleared,
onClose,
isSorted,
onReconcile,
onMenuSelect,
}) {
Expand All @@ -434,6 +438,10 @@ function AccountMenu({
}
}}
items={[
isSorted && {
name: 'remove-sorting',
text: 'Remove all Sorting',
},
canShowBalances && {
name: 'toggle-balance',
text: (showBalances ? 'Hide' : 'Show') + ' Running Balance',
Expand Down Expand Up @@ -464,14 +472,20 @@ function AccountMenu({
);
}

function CategoryMenu({ onClose, onMenuSelect }) {
function CategoryMenu({ onClose, onMenuSelect, isSorted }) {
return (
<MenuTooltip width={200} onClose={onClose}>
<Menu
onMenuSelect={item => {
onMenuSelect(item);
}}
items={[{ name: 'export', text: 'Export' }]}
items={[
isSorted && {
name: 'remove-sorting',
text: 'Remove all Sorting',
},
{ name: 'export', text: 'Export' },
]}
/>
</MenuTooltip>
);
Expand Down
15 changes: 15 additions & 0 deletions packages/desktop-client/src/components/table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ type CellProps = Omit<ComponentProps<typeof View>, 'children' | 'value'> & {
formatter?: (value: string, type?: unknown) => string;
focused?: boolean;
textAlign?: string;
alignItems?: string;
borderColor?: string;
plain?: boolean;
exposed?: boolean;
Expand All @@ -201,6 +202,7 @@ export function Cell({
value,
formatter,
textAlign,
alignItems,
onExpose,
borderColor: oldBorderColor,
children,
Expand Down Expand Up @@ -232,6 +234,7 @@ export function Cell({
borderBottomWidth: borderColor ? 1 : 0,
borderColor,
backgroundColor,
alignItems: alignItems,
};

return (
Expand Down Expand Up @@ -905,7 +908,9 @@ type TableProps = {
animated?: boolean;
allowPopupsEscape?: boolean;
isSelected?: (id: TableItem['id']) => boolean;
saveScrollWidth: (parent, child) => void;
};

export const Table = forwardRef<TableHandleRef, TableProps>(
(
{
Expand All @@ -928,6 +933,7 @@ export const Table = forwardRef<TableHandleRef, TableProps>(
animated,
allowPopupsEscape,
isSelected,
saveScrollWidth,
...props
},
ref,
Expand Down Expand Up @@ -1016,6 +1022,15 @@ export const Table = forwardRef<TableHandleRef, TableProps>(
let editing = editingId === item.id;
let selected = isSelected && isSelected(item.id);

if (scrollContainer.current && saveScrollWidth) {
saveScrollWidth(
scrollContainer.current.offsetParent
? scrollContainer.current.offsetParent.clientWidth
: 0,
scrollContainer.current ? scrollContainer.current.clientWidth : 0,
);
}

let row = renderItem({
item,
editing,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ export default function TransactionList({
hideFraction,
addNotification,
renderEmpty,
onSort,
sortField,
ascDesc,
onChange,
onRefetch,
onCloseAddTransaction,
Expand Down Expand Up @@ -196,6 +199,9 @@ export default function TransactionList({
style={{ backgroundColor: 'white' }}
onNavigateToTransferAccount={onNavigateToTransferAccount}
onNavigateToSchedule={onNavigateToSchedule}
onSort={onSort}
sortField={sortField}
ascDesc={ascDesc}
/>
);
}
Loading

0 comments on commit 4cdc375

Please sign in to comment.