Skip to content

Commit

Permalink
Support account sort order
Browse files Browse the repository at this point in the history
  • Loading branch information
ystxn committed Nov 30, 2024
1 parent bcfb89e commit 9cf5490
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/core/api.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ const api = () => {
listAccounts: (callback) => apiCall(GET, 'account', callback),
addAccount: (payload, callback) => apiCall(POST, 'account', callback, payload),
editAccount: (id, payload, callback) => apiCall(PUT, `account/${id}`, callback, payload),
editAccountVisibility: (accountId, visible, callback) => apiCall(PUT, `account/${accountId}/${visible}`, callback),
editAccountVisibility: (accountId, visible, callback) => apiCall(PUT, `account/${accountId}?visible=${visible}`, callback),
editAccountSort: (accountId, direction, callback) => apiCall(PUT, `account/${accountId}/sort/${direction}`, callback),
deleteAccount: (id, callback) => apiCall(DELETE, `account/${id}`, callback),
listTransactions: (id, callback) => apiCall(GET, `transaction/${id}`, callback),
addTransaction: (payload, callback) => apiCall(POST, 'transaction', callback, payload),
Expand Down
1 change: 1 addition & 0 deletions src/settings/accounts-form.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const AccountsDialog = ({ issuers, accounts, setAccounts, setShowAddDialog, acco
if (!accountToEdit) {
addAccount(newAccount, postProcess);
} else {
newAccount.sortOrder = accountToEdit.sortOrder;
editAccount(accountToEdit.id, newAccount, postProcess);
}
};
Expand Down
44 changes: 43 additions & 1 deletion src/settings/accounts-grid.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import styled from 'styled-components';
import Switch from '@mui/material/Switch';
import VisibilityIcon from '@mui/icons-material/Visibility';
import { HorizontalLoader } from '../core/utils';
import Button from '@mui/material/Button/Button';

const GridBox = styled.div`
display: flex;
Expand Down Expand Up @@ -46,7 +47,7 @@ const AccountsGrid = ({
issuers, accounts, setAccounts, isMobile, selectedAccount, setSelectedAccount, setAccountToEdit, setShowAddDialog,
}) => {
const [ visibleColumns, setVisibleColumns ] = useState({});
const { editAccountVisibility, showStatus } = api();
const { editAccountVisibility, showStatus, editAccountSort } = api();
const colors = {
'Cash': 'success',
'Credit': 'warning',
Expand Down Expand Up @@ -125,6 +126,47 @@ const AccountsGrid = ({
valueGetter: (_, row) => row.transactions || 0,
sortable: false,
},
{
field: "sortOrder",
headerName: "Sort",
width: 200,
sortable: false,
renderCell: (params) => {
const reviseSort = (e, direction) => {
e.stopPropagation();
editAccountSort(params.row.id, direction, (newSortOrder) =>
setAccounts((accounts) => accounts
.map(account => ({ ...account, sortOrder: newSortOrder[account.id] ?? account.sortOrder}))
.sort((a, b) =>
a.type.localeCompare(b.type) ||
a.sortOrder - b.sortOrder ||
getIssuer(a.issuerId).name.localeCompare(getIssuer(b.issuerId).name) ||
a.name.localeCompare(b.name)
)
));
};
return (
<>
<Button
variant="contained"
onClick={(e) => reviseSort(e, 'up')}
sx={{ margin: 'auto .8rem auto 0' }}
disabled={params.row.sortOrder === 0}
>
Up
</Button>
<Button
variant="contained"
onClick={(e) => reviseSort(e, 'down')}
sx={{ margin: 'auto 0' }}
disabled={accounts.filter(a => a.type === params.row.type).slice(-1)[0].id === params.row.id}
>
Down
</Button>
</>
);
}
}
];

useEffect(() => {
Expand Down

0 comments on commit 9cf5490

Please sign in to comment.