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

fix(reconiled-balance):Re-render on reconcile #2989

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,30 @@ export class ReconcileBalance extends Feature {
}

shouldInvoke() {
let { selectedAccountId } = controllerLookup('accounts');
return selectedAccountId && isCurrentRouteAccountsPage();
return isCurrentRouteAccountsPage();
}

invoke() {
// Get the current account id and calculate the current reconciled balance
let { selectedAccountId } = controllerLookup('accounts');
let reconciledBalance = formatCurrency(this._calculateReconciledBalance(selectedAccountId));
let accountsController = controllerLookup<YNABAccountsController>('accounts');
if (accountsController !== undefined) {
williammck marked this conversation as resolved.
Show resolved Hide resolved
let { selectedAccountId } = accountsController;
let reconciledBalance = formatCurrency(this._calculateReconciledBalance(selectedAccountId));
// Retrieve or create the reconcile balance container
let balanceContainer = $(`#${TK_RECONCILE_BALANCE_ID}`);
if (!balanceContainer || balanceContainer.length === 0) {
$(YNAB_ACCOUNTS_HEADER_BALANCES).prepend(
`<div class="tk-accounts-header-balances-reconciled">
<span id="${TK_RECONCILE_BALANCE_ID}">${reconciledBalance}</span>
<div class="tk-accounts-header-reconcile-balance-label">Reconciled Balance</div>
</div>`
);
}

// Retrieve or create the reconcile balance container
let balanceContainer = $(`#${TK_RECONCILE_BALANCE_ID}`);
if (!balanceContainer || balanceContainer.length === 0) {
$(YNAB_ACCOUNTS_HEADER_BALANCES).prepend(
`<div class="tk-accounts-header-balances-reconciled">
<span id="${TK_RECONCILE_BALANCE_ID}">${reconciledBalance}</span>
<div class="tk-accounts-header-reconcile-balance-label">Reconciled Balance</div>
</div>`
);
// Update the reconcile balance with the most up to date balance
balanceContainer.text(reconciledBalance);
this._setFeatureVisibility(true);
}

// Update the reconcile balance with the most up to date balance
balanceContainer.text(reconciledBalance);
this._setFeatureVisibility(true);
}

destroy() {
Expand All @@ -49,37 +50,39 @@ export class ReconcileBalance extends Feature {
}
}

observe(changedNodes) {
observe(changedNodes: Set<string>) {
if (!this.shouldInvoke()) return;

console.log(changedNodes);
williammck marked this conversation as resolved.
Show resolved Hide resolved

// When the reconciled balance icon changes, reevaluate our balance
if (changedNodes.has('is-reconciled-icon svg-icon lock')) {
if (
changedNodes.has('is-reconciled-icon svg-icon lock') ||
changedNodes.has('accounts-header-reconcile button')
williammck marked this conversation as resolved.
Show resolved Hide resolved
) {
this.invoke();
}
}

/**
*
* Calculate the a given accounts reconciled balance
* @param {String} accountId The account id to get the reconciled balance for
* @returns {Number} balance The reconciled balance of the account
*
*/
_calculateReconciledBalance = (accountId) => {

_calculateReconciledBalance = (accountId: string) => {
const account = getEntityManager().getAccountById(accountId);

return account.getTransactions().reduce((reduced, transaction) => {
if (transaction.cleared && !transaction.isTombstone && transaction.isReconciled()) {
if (transaction.cleared && !transaction.isTombstone && transaction.isReconciled?.()) {
williammck marked this conversation as resolved.
Show resolved Hide resolved
return reduced + transaction.amount;
}

return reduced;
}, 0);
};

/**
* Helper method to show and hide the reconcile balance container
* @param {Boolean} visible True to show the container, false to hide
*/
_setFeatureVisibility = (visible) => {
_setFeatureVisibility = (visible: boolean) => {
let featureContainer = $('.tk-accounts-header-balances-reconciled');
if (featureContainer && featureContainer.length) {
featureContainer.toggle(visible);
Expand Down
2 changes: 1 addition & 1 deletion src/extension/features/feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class Feature {
});
}

observe(): void {
observe(changedNodes: Set<string>): void {
/* stubbed listener function */
}

Expand Down
1 change: 1 addition & 0 deletions src/types/ynab/data/transaction.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ export interface YNABTransaction {
ynabId: string | null;

isUncleared?: () => boolean;
isReconciled?: () => boolean;
}