Skip to content

Commit

Permalink
fix: use serviceLookup for most things (#3174)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshmadewell committed Jul 17, 2023
1 parent 819f3c2 commit 8e53f91
Show file tree
Hide file tree
Showing 19 changed files with 63 additions and 143 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Feature } from 'toolkit/extension/features/feature';
import { isCurrentRouteAccountsPage } from 'toolkit/extension/utils/ynab';
import { getAccountsService, isCurrentRouteAccountsPage } from 'toolkit/extension/utils/ynab';
import { serviceLookup } from 'toolkit/extension/utils/ember';

export class AutoEnableRunningBalance extends Feature {
Expand All @@ -8,7 +8,7 @@ export class AutoEnableRunningBalance extends Feature {
}

invoke() {
const { selectedAccountId } = serviceLookup('accounts');
const { selectedAccountId } = getAccountsService();
const registerGridService = serviceLookup('register-grid');
if (!registerGridService) {
return;
Expand Down
4 changes: 2 additions & 2 deletions src/extension/features/accounts/bulk-edit-memo/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Feature } from 'toolkit/extension/features/feature';
import { containerLookup } from 'toolkit/extension/utils/ember';
import { l10n } from 'toolkit/extension/utils/toolkit';
import { componentAfter } from 'toolkit/extension/utils/react';
import { getEntityManager, getAccountsController } from 'toolkit/extension/utils/ynab';
import { getEntityManager, getModalService } from 'toolkit/extension/utils/ynab';

const DEFAULT_DISPLAY_MODE = 'defaultDisplayMode';
const MENU_DISPLAY_MODE = 'menuDisplayMode';
Expand Down Expand Up @@ -44,7 +44,7 @@ const EditMemo = () => {
});

setDisplayMode(DEFAULT_DISPLAY_MODE);
getAccountsController().send('closeModal');
getModalService().closeModal();
};

const selectedTransactionsCount = containerLookup('service:accounts').areChecked.length;
Expand Down
7 changes: 3 additions & 4 deletions src/extension/features/accounts/calculate-irr/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Feature } from 'toolkit/extension/features/feature';
import { isCurrentRouteAccountsPage } from 'toolkit/extension/utils/ynab';
import { serviceLookup } from 'toolkit/extension/utils/ember';
import { getAccountsService, isCurrentRouteAccountsPage } from 'toolkit/extension/utils/ynab';
import { getEntityManager } from 'toolkit/extension/utils/ynab';

export class CalculateIRR extends Feature {
Expand All @@ -9,12 +8,12 @@ export class CalculateIRR extends Feature {
}

shouldInvoke() {
let { selectedAccount } = serviceLookup('accounts');
let { selectedAccount } = getAccountsService();
return isCurrentRouteAccountsPage() && selectedAccount && !selectedAccount.onBudget;
}

invoke() {
let { filters, selectedAccount, selectedAccountId } = serviceLookup('accounts');
let { filters, selectedAccount, selectedAccountId } = getAccountsService();
let { filterFrom, filterTo } = this._getFilterDates(filters);
let totalIrr = this._calculateIRR(selectedAccountId);
if (totalIrr === Infinity) {
Expand Down
63 changes: 0 additions & 63 deletions src/extension/features/accounts/clear-selection/index.js

This file was deleted.

9 changes: 0 additions & 9 deletions src/extension/features/accounts/clear-selection/settings.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { Feature } from 'toolkit/extension/features/feature';
import { isCurrentRouteAccountsPage } from 'toolkit/extension/utils/ynab';
import { serviceLookup } from 'toolkit/extension/utils/ember';
import { getAccountsService, isCurrentRouteAccountsPage } from 'toolkit/extension/utils/ynab';
import { formatCurrency, stripCurrency } from 'toolkit/extension/utils/currency';

export class DefaultCCToCleared extends Feature {
didClickRecord = false;

shouldInvoke() {
// grab the current account
let { selectedAccount } = serviceLookup('accounts');
let { selectedAccount } = getAccountsService();
return (
// only activate if we're on an accounts page and it's a credit card account
isCurrentRouteAccountsPage() &&
Expand Down Expand Up @@ -58,7 +57,7 @@ export class DefaultCCToCleared extends Feature {
// transaction entry model to appear
if (didChangeInput && this.didClickRecord) {
// grab amounts from account
let { selectedAccount } = serviceLookup('accounts');
let { selectedAccount } = getAccountsService();
const clearedBal = selectedAccount.accountCalculation.clearedBalance;

// we want to fill in the absolute value of cleared bal, since it's positive infow,
Expand Down
14 changes: 10 additions & 4 deletions src/extension/features/accounts/reconcile-balance/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Feature } from 'toolkit/extension/features/feature';
import { isCurrentRouteAccountsPage } from 'toolkit/extension/utils/ynab';
import { controllerLookup } from 'toolkit/extension/utils/ember';
import { serviceLookup } from 'toolkit/extension/utils/ember';
import { formatCurrency } from 'toolkit/extension/utils/currency';
import { getEntityManager } from 'toolkit/extension/utils/ynab';
const YNAB_ACCOUNTS_HEADER_BALANCES = '.accounts-header-balances';
Expand All @@ -17,9 +17,12 @@ export class ReconcileBalance extends Feature {

invoke() {
// Get the current account id and calculate the current reconciled balance
let accountsController = controllerLookup<YNABAccountsController>('accounts');
if (accountsController === undefined) return;
let { selectedAccountId } = accountsController;
let accountService = serviceLookup<YNABAccountsService>('accounts');
if (!accountService) {
return;
}

let { selectedAccountId } = accountService;
let reconciledBalance = formatCurrency(this._calculateReconciledBalance(selectedAccountId));
// Retrieve or create the reconcile balance container
let balanceContainer = $(`#${TK_RECONCILE_BALANCE_ID}`);
Expand Down Expand Up @@ -69,6 +72,9 @@ export class ReconcileBalance extends Feature {

_calculateReconciledBalance = (accountId: string) => {
const account = getEntityManager().getAccountById(accountId);
if (!account) {
return;
}

return account.getTransactions().reduce((reduced, transaction) => {
if (transaction.cleared && !transaction.isTombstone && transaction.isReconciled?.()) {
Expand Down
8 changes: 6 additions & 2 deletions src/extension/features/accounts/right-click-to-edit/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Feature } from 'toolkit/extension/features/feature';
import { serviceLookup } from 'toolkit/extension/utils/ember';
import { isCurrentRouteAccountsPage, ynabRequire } from 'toolkit/extension/utils/ynab';
import {
getAccountsService,
isCurrentRouteAccountsPage,
ynabRequire,
} from 'toolkit/extension/utils/ynab';

const { next } = ynabRequire('@ember/runloop');

Expand Down Expand Up @@ -30,7 +34,7 @@ export class RightClickToEdit extends Feature {
$row = $row.prevAll('.ynab-grid-body-parent:first');
}

const accountsService = serviceLookup('accounts');
const accountsService = getAccountsService();
const areChecked = accountsService.areChecked;
const visibleTransactionDisplayItems =
accountsService?.transactionEditorService?.visibleTransactionDisplayItems;
Expand Down
13 changes: 8 additions & 5 deletions src/extension/features/accounts/toggle-account-columns/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ import * as React from 'react';
import * as PropTypes from 'prop-types';
import { componentAppend } from 'toolkit/extension/utils/react';
import { Feature } from 'toolkit/extension/features/feature';
import { isCurrentRouteAccountsPage } from 'toolkit/extension/utils/ynab';
import {
getAccountsService,
getModalService,
isCurrentRouteAccountsPage,
} from 'toolkit/extension/utils/ynab';
import { l10n, getToolkitStorageKey, setToolkitStorageKey } from 'toolkit/extension/utils/toolkit';
import { controllerLookup } from 'toolkit/extension/utils/ember';

export const ShowMemoButton = ({ defaultIsShown, id, toggleState }) => {
const toggleHidden = () => {
toggleState(!defaultIsShown);
controllerLookup('application').send('closeModal');
getModalService().closeModal();
};

return (
Expand Down Expand Up @@ -77,7 +80,7 @@ export class ToggleAccountColumns extends Feature {
}

getShowMemoState = () => {
const { selectedAccountId } = controllerLookup('accounts');
const { selectedAccountId } = getAccountsService();
if (!selectedAccountId) {
return true;
}
Expand All @@ -86,7 +89,7 @@ export class ToggleAccountColumns extends Feature {
};

updateShowMemoState = (state) => {
const { selectedAccountId } = controllerLookup('accounts');
const { selectedAccountId } = getAccountsService();
if (!selectedAccountId) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as React from 'react';
import * as PropTypes from 'prop-types';
import { Feature } from 'toolkit/extension/features/feature';
import { serviceLookup } from 'toolkit/extension/utils/ember';
import { componentBefore } from 'toolkit/extension/utils/react';
import { getAccountsService } from 'toolkit/extension/utils/ynab';

const ToggleButton = ({ stateField }) => {
const accountsService = serviceLookup('accounts');
const accountsService = getAccountsService();
const [isShown, setIsShown] = React.useState(accountsService.get(`filters.${stateField}`));

React.useEffect(() => {
Expand Down
5 changes: 2 additions & 3 deletions src/extension/features/budget/date-of-money/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Feature } from 'toolkit/extension/features/feature';
import { isCurrentRouteBudgetPage } from 'toolkit/extension/utils/ynab';
import { controllerLookup } from 'toolkit/extension/utils/ember';
import { getBudgetService, isCurrentRouteBudgetPage } from 'toolkit/extension/utils/ynab';

export class DateOfMoney extends Feature {
injectCSS() {
Expand All @@ -27,7 +26,7 @@ export class DateOfMoney extends Feature {
return;
}

const budgetController = controllerLookup('budget');
const budgetController = getBudgetService();
const ageOfMoney = budgetController.get(
'budgetViewModel.monthlyBudgetCalculationForCurrentMonth.ageOfMoney'
);
Expand Down
15 changes: 6 additions & 9 deletions src/extension/features/budget/toggle-master-categories/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Feature } from 'toolkit/extension/features/feature';
import { isCurrentRouteBudgetPage } from 'toolkit/extension/utils/ynab';
import { controllerLookup, getEmberView } from 'toolkit/extension/utils/ember';
import { getBudgetService, isCurrentRouteBudgetPage } from 'toolkit/extension/utils/ynab';
import { getEmberView } from 'toolkit/extension/utils/ember';
import { getToolkitStorageKey, setToolkitStorageKey } from 'toolkit/extension/utils/toolkit';

export class ToggleMasterCategories extends Feature {
get isAnyCategoryCollapsed() {
const { budgetMonthDisplayItems } = controllerLookup('budget');
const { budgetMonthDisplayItems } = getBudgetService();
return budgetMonthDisplayItems.some(({ isCollapsed }) => isCollapsed);
}

Expand Down Expand Up @@ -56,10 +56,7 @@ export class ToggleMasterCategories extends Feature {
if (getToolkitStorageKey('catSoloMode', false)) {
this.handleToggleSoloMode(event);
} else {
controllerLookup('budget').send(
'toggleCollapseMasterCategories',
!this.isAnyCategoryCollapsed
);
getBudgetService().send('toggleCollapseMasterCategories', !this.isAnyCategoryCollapsed);
}

this.updateToggleIcon();
Expand Down Expand Up @@ -91,7 +88,7 @@ export class ToggleMasterCategories extends Feature {
this.handleRowExpandedToggle
);

const budgetController = controllerLookup('budget');
const budgetController = getBudgetService();
budgetController.send('toggleCollapseMasterCategories', true);
budgetController.budgetMonthDisplayItems
.find(({ isMasterCategory }) => isMasterCategory)
Expand Down Expand Up @@ -127,7 +124,7 @@ export class ToggleMasterCategories extends Feature {
const clickedMasterCategoryId = getEmberView(event.currentTarget.parentElement.id)
?.masterCategory?.entityId;

controllerLookup('budget').budgetMonthDisplayItems.forEach((item) => {
getBudgetService().budgetMonthDisplayItems.forEach((item) => {
if (!item.isMasterCategory) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { controllerLookup } from 'toolkit/extension/utils/ember';
import { getModalService } from 'toolkit/extension/utils/ynab';

export function showTransactionModal(title, transactions) {
const applicationController = controllerLookup('application');
const reportsController = controllerLookup('reports/spending');
reportsController.set('modalTitle', title);
reportsController.set('modalTransactions', transactions);
applicationController.send('openModal', 'modals/reports/transactions', {
getModalService().openModal('modals/reports/transactions', {
controller: reportsController,
});
}
4 changes: 2 additions & 2 deletions src/extension/features/toolkit-reports/utils/storage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getFirstMonthOfBudget, getToday } from 'toolkit/extension/utils/date';
import { getToolkitStorageKey, setToolkitStorageKey } from 'toolkit/extension/utils/toolkit';
import { controllerLookup } from 'toolkit/extension/utils/ember';
import { getApplicationService } from 'toolkit/extension/utils/ynab';

const FilterType = {
Account: 'account-filters',
Expand Down Expand Up @@ -38,7 +38,7 @@ export function storeDateFilters(reportKey, filters) {
}

function generateStorageKey(reportKey, filterType) {
const budgetVersionId = controllerLookup('application').get('budgetVersionId');
const budgetVersionId = getApplicationService().budgetVersionId;
return `${budgetVersionId}-${reportKey}-${filterType}`;
}

Expand Down
7 changes: 2 additions & 5 deletions src/extension/listeners/routeChangeListener.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { controllerLookup } from 'toolkit/extension/utils/ember';
import { withToolkitError } from 'toolkit/core/common/errors/with-toolkit-error';
import { getRouter } from 'toolkit/extension/utils/ember';
import { ynabRequire } from '../utils/ynab';
Expand All @@ -11,8 +10,7 @@ export class RouteChangeListener {
routeChangeListener.features = [];

function emitSameBudgetRouteChange() {
const applicationController = controllerLookup('application');
const currentRoute = applicationController.get('currentRouteName');
const currentRoute = getRouter().get('currentRouteName');
routeChangeListener.features.forEach((feature) => {
const observe = feature.onRouteChanged.bind(feature, currentRoute);
const wrapped = withToolkitError(observe, feature);
Expand All @@ -21,8 +19,7 @@ export class RouteChangeListener {
}

function emitBudgetRouteChange() {
const applicationController = controllerLookup('application');
const currentRoute = applicationController.get('currentRouteName');
const currentRoute = getRouter().get('currentRouteName');
routeChangeListener.features.forEach((feature) => {
const observe = feature.onBudgetChanged.bind(feature, currentRoute);
const wrapped = withToolkitError(observe, feature);
Expand Down

0 comments on commit 8e53f91

Please sign in to comment.