Skip to content

Commit

Permalink
refactor: use typed store.dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
pwltr committed Dec 12, 2023
1 parent 30013c5 commit d2bc694
Show file tree
Hide file tree
Showing 17 changed files with 19 additions and 51 deletions.
3 changes: 1 addition & 2 deletions __tests__/backups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import {
performWidgetsRestore,
} from '../src/store/actions/backup';
import {
dispatch,
getActivityStore,
getBlocktankStore,
getDispatch,
getMetaDataStore,
getSettingsStore,
getWidgetsStore,
Expand Down Expand Up @@ -274,7 +274,6 @@ describe('Remote backups', () => {
});

it('Backups and restores Blocktank orders', async () => {
const dispatch = getDispatch();
addPaidBlocktankOrder({ orderId: 'id', txid: 'txid' });
dispatch({
type: actions.UPDATE_BLOCKTANK_ORDER,
Expand Down
4 changes: 1 addition & 3 deletions src/store/actions/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
IActivityItem,
TLightningActivityItem,
} from '../types/activity';
import { getBlocktankStore, getDispatch } from '../helpers';
import { getBlocktankStore, dispatch } from '../helpers';
import { onChainTransactionToActivityItem } from '../../utils/activity';
import { getCurrentWallet } from '../../utils/wallet';
import { formatBoostedActivityItems } from '../../utils/boost';
Expand All @@ -17,8 +17,6 @@ import { checkPendingCJitEntries } from './blocktank';
import { getLightningChannels } from '../../utils/lightning';
import { updateSettings } from './settings';

const dispatch = getDispatch();

/**
* Adds the provided activity item to the activity list.
* @param {IActivityItem} activityItem
Expand Down
4 changes: 1 addition & 3 deletions src/store/actions/backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { err, ok, Result } from '@synonymdev/result';
import lm, { ldk, ENetworks, TLdkData } from '@synonymdev/react-native-ldk';

import actions from './actions';
import { getBackupStore, getDispatch } from '../helpers';
import { getBackupStore, dispatch } from '../helpers';
import {
EBackupCategories,
fetchBackup,
Expand Down Expand Up @@ -47,8 +47,6 @@ import {
__BACKUPS_SERVER_PUBKEY__,
} from '../../constants/env';

const dispatch = getDispatch();

/**
* Triggers a full remote backup
* @return {Promise<Result<string>>}
Expand Down
4 changes: 1 addition & 3 deletions src/store/actions/blocktank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { err, ok, Result } from '@synonymdev/result';
import actions from './actions';
import { resetSendTransaction, updateSendTransaction } from './wallet';
import { setLightningSettingUpStep } from './user';
import { getBlocktankStore, getDispatch, getWalletStore } from '../helpers';
import { getBlocktankStore, getWalletStore, dispatch } from '../helpers';
import * as blocktank from '../../utils/blocktank';
import {
createOrder,
Expand Down Expand Up @@ -42,8 +42,6 @@ import {
} from '@synonymdev/blocktank-lsp-http-client';
import { CJitStateEnum } from '@synonymdev/blocktank-lsp-http-client/dist/shared/CJitStateEnum';

const dispatch = getDispatch();

/**
* Retrieves & updates the status of stored orders that may have changed.
* @returns {Promise<Result<string>>}
Expand Down
4 changes: 1 addition & 3 deletions src/store/actions/checks.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import actions from './actions';
import { getDispatch } from '../helpers';
import { dispatch } from '../helpers';
import { TWalletName } from '../types/wallet';
import { getSelectedNetwork, getSelectedWallet } from '../../utils/wallet';
import { TAvailableNetworks } from '../../utils/networks';
import { TStorageWarning } from '../types/checks';
import { getWarnings } from '../../utils/checks';

const dispatch = getDispatch();

/**
* Adds a single warning.
* @param {TStorageWarning} warning
Expand Down
4 changes: 1 addition & 3 deletions src/store/actions/fees.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { ok, err, Result } from '@synonymdev/result';

import actions from './actions';
import { getDispatch, getFeesStore } from '../helpers';
import { dispatch, getFeesStore } from '../helpers';
import { getFeeEstimates } from '../../utils/wallet/transactions';
import { TAvailableNetworks } from '../../utils/networks';
import { IOnchainFees } from '../types/fees';

const dispatch = getDispatch();

export const REFRESH_INTERVAL = 60 * 30; // in seconds, 30 minutes

export const updateOnchainFeeEstimates = async ({
Expand Down
4 changes: 1 addition & 3 deletions src/store/actions/lightning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { TChannel, TInvoice } from '@synonymdev/react-native-ldk';
import { getLNURLParams, lnurlChannel } from '@synonymdev/react-native-lnurl';

import actions from './actions';
import { getDispatch, getLightningStore, getMetaDataStore } from '../helpers';
import { dispatch, getLightningStore, getMetaDataStore } from '../helpers';
import { TAvailableNetworks } from '../../utils/networks';
import { getActivityItemById } from '../../utils/activity';
import { getSelectedNetwork, getSelectedWallet } from '../../utils/wallet';
Expand All @@ -29,8 +29,6 @@ import {
import { EPaymentType, TWalletName } from '../types/wallet';
import { EActivityType, TLightningActivityItem } from '../types/activity';

const dispatch = getDispatch();

/**
* Attempts to update the node id for the given wallet and network.
* @param {string} nodeId
Expand Down
4 changes: 1 addition & 3 deletions src/store/actions/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { ok, Result } from '@synonymdev/result';
import actions from './actions';
import { getDispatch, getMetaDataStore } from '../helpers';
import { dispatch, getMetaDataStore } from '../helpers';
import { getCurrentWallet } from '../../utils/wallet';
import { EPaymentType } from '../types/wallet';
import { IMetadata } from '../types/metadata';

const dispatch = getDispatch();

/*
* This action updates transactions tags
*/
Expand Down
4 changes: 1 addition & 3 deletions src/store/actions/receive.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { ok, Result } from '@synonymdev/result';
import { ICJitEntry } from '@synonymdev/blocktank-lsp-http-client';
import actions from './actions';
import { getDispatch } from '../helpers';

const dispatch = getDispatch();
import { dispatch } from '../helpers';

export const updateInvoice = (payload: {
amount?: number;
Expand Down
6 changes: 2 additions & 4 deletions src/store/actions/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { err, ok, Result } from '@synonymdev/result';
import RNRestart from 'react-native-restart';

import actions from './actions';
import { getDispatch } from '../helpers';
import { dispatch } from '../helpers';
import i18n from '../../utils/i18n';
import { sleep } from '../../utils/helpers';
import { removePin } from '../../utils/settings';
Expand All @@ -14,8 +14,6 @@ import { getAllKeychainKeys, resetKeychainValue } from '../../utils/keychain';
import { ICustomElectrumPeer, ISettings, TChest } from '../types/settings';
import { TWalletName } from '../types/wallet';

const dispatch = getDispatch();

export const updateSettings = (payload: Partial<ISettings>): Result<string> => {
dispatch({
type: actions.UPDATE_SETTINGS,
Expand Down Expand Up @@ -84,7 +82,7 @@ export const wipeApp = async ({
showToast({
type: 'success',
title: i18n.t('security:wiped_title'),
description: i18n.t('security:wiped_description'),
description: i18n.t('security:wiped_message'),
});
}

Expand Down
4 changes: 1 addition & 3 deletions src/store/actions/slashtags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import b4a from 'b4a';

import { __SLASHTAGS_SEEDER_BASE_URL__ } from '../../constants/env';
import actions from './actions';
import { getDispatch, getSlashtagsStore } from '../helpers';
import { dispatch, getSlashtagsStore } from '../helpers';
import { BasicProfile, ISlashtags, Link, LocalLink } from '../types/slashtags';
import { seedDrives } from '../../utils/slashtags';

const dispatch = getDispatch();

/**
* Sets the onboarding profile state.
*/
Expand Down
4 changes: 1 addition & 3 deletions src/store/actions/todos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import { ok, Result } from '@synonymdev/result';
import { TChannel } from '@synonymdev/react-native-ldk';

import actions from './actions';
import { getDispatch } from '../helpers';
import { dispatch } from '../helpers';
import { TTodoType } from '../types/todos';

const dispatch = getDispatch();

export const hideTodo = (id: TTodoType): Result<string> => {
dispatch({
type: actions.HIDE_TODO,
Expand Down
4 changes: 1 addition & 3 deletions src/store/actions/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ import {
TProfileLink,
ViewControllerParamList,
} from '../types/ui';
import { getActivityStore, getDispatch } from '../helpers';
import { getActivityStore, dispatch } from '../helpers';
import actions from './actions';

const releaseUrl =
'https://github.com/synonymdev/bitkit/releases/download/updater/release.json';

const dispatch = getDispatch();

export const updateUi = (payload: Partial<IUi>): Result<string> => {
dispatch({
type: actions.UPDATE_UI,
Expand Down
4 changes: 1 addition & 3 deletions src/store/actions/user.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { ok, Result } from '@synonymdev/result';

import actions from './actions';
import { getDispatch } from '../helpers';
import { dispatch } from '../helpers';
import { isGeoBlocked } from '../../utils/blocktank';
import { IUser } from '../types/user';

const dispatch = getDispatch();

export const updateUser = (payload: Partial<IUser>): Result<string> => {
dispatch({
type: actions.UPDATE_USER,
Expand Down
4 changes: 1 addition & 3 deletions src/store/actions/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ import {
rescanAddresses,
} from '../../utils/wallet';
import {
dispatch,
getBlocktankStore,
getDispatch,
getFeesStore,
getSettingsStore,
getWalletStore,
Expand Down Expand Up @@ -87,8 +87,6 @@ import { getFakeTransaction } from '../../utils/wallet/testing';
import { EActivityType, TOnchainActivityItem } from '../types/activity';
import i18n from '../../utils/i18n';

const dispatch = getDispatch();

export const updateWallet = (
payload: Partial<IWalletStore>,
): Result<string> => {
Expand Down
4 changes: 1 addition & 3 deletions src/store/actions/widgets.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import actions from './actions';
import { getDispatch } from '../helpers';
import { dispatch } from '../helpers';
import {
IWidgetsStore,
SlashFeedJSON,
TWidgetSettings,
} from '../types/widgets';
import { ok, Result } from '@synonymdev/result';

const dispatch = getDispatch();

export const updateWidgets = (
payload: Partial<IWidgetsStore>,
): Result<string> => {
Expand Down
5 changes: 2 additions & 3 deletions src/store/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Dispatch } from 'redux';
import cloneDeep from 'lodash/cloneDeep';

import store from '../store';
Expand Down Expand Up @@ -81,6 +80,6 @@ export const getBackupStore = (): IBackup => {
};

/*
Used to get dispatch outside of a component.
Used to dispatch outside of a component.
*/
export const getDispatch = (): Dispatch<any> => store.dispatch;
export const { dispatch } = store;

0 comments on commit d2bc694

Please sign in to comment.