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

Add support for tags in transactions #77

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
25 changes: 25 additions & 0 deletions Coronado.ConsoleApp/Commands/NewTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public async Task Execute(Datastore context, params string[] args)
ReadLine.AutoCompletionHandler = null;
return;
};
ReadLine.AutoCompletionHandler = new TagCompletionHandler(context);
if (!GetInput("Tags (comma-separated)", out var tags))
{
ReadLine.AutoCompletionHandler = null;
return;
};
ReadLine.AutoCompletionHandler = null;
if (!GetInput("Description", out var description)) return;
if (!GetInput("Amount", out var amount)) return;
Expand All @@ -41,6 +47,7 @@ public async Task Execute(Datastore context, params string[] args)
TransactionDate = DateTime.Parse(date),
Vendor = vendor,
CategoryName = category,
Tags = tags.Split(',').Select(tag => tag.Trim()).ToList(),
Amount = decimal.Parse(amount),
Description = description,
AccountId = context.SelectedAccount.AccountId,
Expand Down Expand Up @@ -115,4 +122,22 @@ public string[] GetSuggestions(string text, int index)
return null;
}
}

class TagCompletionHandler : IAutoCompleteHandler
{
private readonly Datastore _context;

public TagCompletionHandler(Datastore context) : base()
{
_context = context;
}

public char[] Separators { get; set; } = new char[] { '\t' };
public string[] GetSuggestions(string text, int index)
{
var tags = _context.Transactions.SelectMany(t => t.Tags).Where(tag => tag.Contains(text)).Distinct();
if (tags.Any()) return tags.ToArray();
return null;
}
}
}
2 changes: 1 addition & 1 deletion Coronado.Web/ClientApp/src/api/transactionApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@ class TransactionApi {
}
}

export default TransactionApi;
export default TransactionApi;
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { IconButton, makeStyles, TableRow, TableCell } from '@material-ui/core';
import CancelIcon from '@material-ui/icons/Cancel'
import moment from 'moment';
import { TransactionInput } from '../TransactionInput';
import Autocomplete from '@material-ui/lab/Autocomplete';

const styles = theme => ({
input: {
Expand Down Expand Up @@ -36,6 +37,7 @@ export default function EditableTransaction(props) {
debit: props.transaction.debit ? Number(props.transaction.debit).toFixed(2) : '',
credit: props.transaction.credit ? Number(props.transaction.credit).toFixed(2) : '',
categoryName: props.transaction.categoryDisplay,
tags: props.transaction.tags || [],
});
const vendors = useSelector(state => state.vendors);
const categories = useSelector(state => getCategoriesForDropdown(state.categories, state.accounts, state.invoices));
Expand All @@ -53,6 +55,7 @@ export default function EditableTransaction(props) {
debit: transaction.debit ? Number(transaction.debit).toFixed(2) : '',
credit: transaction.credit ? Number(transaction.credit).toFixed(2) : '',
categoryName: transaction.categoryDisplay,
tags: transaction.tags || [],
});
setSelectedCategory({
categoryId: transaction.categoryId,
Expand Down Expand Up @@ -139,6 +142,13 @@ export default function EditableTransaction(props) {
}
}

const handleChangeTags = (event, newValue) => {
setTrx({
...trx,
tags: newValue,
});
}

const updateTransaction = () => {
dispatch(transactionActions.updateTransaction(trx));
props.onFinishEdit();
Expand Down Expand Up @@ -206,7 +216,22 @@ export default function EditableTransaction(props) {
onKeyPress={handleKeyPress}
/>
</TableCell>
<TableCell />
<TableCell>
<Autocomplete
multiple
freeSolo
options={[]}
value={trx.tags}
onChange={handleChangeTags}
renderInput={(params) => (
<TransactionInput
{...params}
className={classes.input}
placeholder="Tags"
/>
)}
/>
</TableCell>
</TableRow>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ const styles = theme => ({
width: 100,
maxWidth: 100,
},
"& td:nth-child(9)": {
width: 200,
maxWidth: 200,
},
"& input": {
width: "100%",
}
Expand Down Expand Up @@ -69,13 +73,14 @@ export default function TransactionList(props) {
<TableCell>Description</TableCell>
<TableCell>Debit</TableCell>
<TableCell>Credit</TableCell>
<TableCell>Tags</TableCell>
<TableCell></TableCell>
</TableRow>
</TableHead>
<TableBody>
<NewTransactionRow
account={props.account} />
{isLoading ? <tr><td colSpan="8"><Spinner /></td></tr> :
{isLoading ? <tr><td colSpan="9"><Spinner /></td></tr> :
transactions.map(trx =>
<TransactionRow
key={trx.transactionId}
Expand All @@ -87,4 +92,4 @@ export default function TransactionList(props) {
</TableBody>
</Table>
);
}
}