Skip to content

Commit

Permalink
Add getAssetByUUID method to AssetProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
amacar committed May 4, 2020
1 parent c400eae commit acf2553
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as Yup from 'yup';

import translate, { translateRaw } from 'v2/translations';
import { SPACING } from 'v2/theme';
import { IAccount, Network, StoreAccount, Asset, TSymbol } from 'v2/types';
import { IAccount, Network, StoreAccount, Asset, TSymbol, TUuid } from 'v2/types';
import { AccountDropdown, InlineMessage, AmountInput, Button } from 'v2/components';
import { validateAmountField } from 'v2/features/SendAssets/components/validators/validators';
import { isEthereumAccount } from 'v2/services/Store/Account/helpers';
Expand Down Expand Up @@ -76,10 +76,9 @@ export const MembershipFormUI = ({
relevantAccounts,
onComplete
}: UIProps) => {
const { assets } = useContext(AssetContext);
const { getAssetByUUID } = useContext(AssetContext);
const defaultMembership = MEMBERSHIP_CONFIG[IMembershipId.sixmonths];
const defaultAsset = (assets.find((asset) => asset.uuid === defaultMembership.assetUUID) ||
{}) as Asset;
const defaultAsset = (getAssetByUUID(defaultMembership.assetUUID as TUuid) || {}) as Asset;
const initialFormikValues: MembershipSimpleTxFormFull = {
membershipSelected: defaultMembership,
account: {} as StoreAccount,
Expand Down Expand Up @@ -152,9 +151,7 @@ export const MembershipFormUI = ({
onSelect={(option: { label: string; value: IMembershipConfig }) => {
form.setFieldValue('membershipSelected', option.value); //if this gets deleted, it no longer shows as selected on interface, would like to set only object keys that are needed instead of full object
form.setFieldValue('amount', option.value.price);
const newAsset = assets.find(
(a) => a.uuid === option.value.assetUUID
) as Asset;
const newAsset = getAssetByUUID(option.value.assetUUID as TUuid);
form.setFieldValue('asset', newAsset);
}}
/>
Expand Down
25 changes: 10 additions & 15 deletions common/v2/services/Store/Asset/AssetProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,44 +1,39 @@
import React, { useContext, createContext } from 'react';
import clone from 'ramda/src/clone';

import { ExtendedAsset, LSKeys, TUuid, StoreAsset } from 'v2/types';
import { ExtendedAsset, LSKeys, TUuid, StoreAsset, Asset } from 'v2/types';

import { DataContext } from '../DataManager';
import { NetworkContext } from '../Network';
import { getAssetByUUID } from './helpers';

export interface IAssetContext {
assets: ExtendedAsset[];
createAssetWithID(assetData: ExtendedAsset, id: TUuid): void;
getAssetByUUID(uuid: TUuid): Asset | undefined;
addAssetsFromAPI(newAssets: Record<TUuid, StoreAsset>): void;
}

export const AssetContext = createContext({} as IAssetContext);

export const AssetProvider: React.FC = ({ children }) => {
const { createActions, assets } = useContext(DataContext);
const { getNetworkByChainId } = useContext(NetworkContext);
const model = createActions(LSKeys.ASSETS);

const state: IAssetContext = {
assets,
createAssetWithID: model.createWithID,
getAssetByUUID: (uuid) => getAssetByUUID(assets)(uuid),
addAssetsFromAPI: (newAssets) => {
const mappedAssets = Object.entries(newAssets).map(([uuid, asset]: [TUuid, any]) => {
const network = getNetworkByChainId(parseInt(asset.networkId, 10));
return {
...asset,
uuid,
contractAddress: asset.address,
decimal: asset.decimals,
networkId: network ? network.id : asset.networkId,
isCustom: false
};
});
const mappedAssets = Object.entries(newAssets).map(([uuid, asset]: [TUuid, StoreAsset]) => ({
...asset,
uuid,
isCustom: false
}));

// make a copy of current assets array and merge it with assets received from API
const assetsCopy = clone(assets);
mappedAssets.forEach((asset) => {
const existing = assetsCopy.find((x) => x.uuid === asset.uuid);
const existing = getAssetByUUID(assetsCopy)(asset.uuid);
if (existing) {
Object.assign(existing, asset);
} else {
Expand Down
9 changes: 5 additions & 4 deletions common/v2/services/Store/StoreProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
MEMBERSHIP_CONTRACTS
} from 'v2/features/PurchaseMembership/config';
import { DEFAULT_NETWORK } from 'v2/config';
import { useEffectOnce } from 'v2/vendor';

import { getAccountsAssetsBalances, nestedToBigNumberJS } from './BalanceService';
import { getStoreAccounts, getPendingTransactionsFromAccounts } from './helpers';
Expand Down Expand Up @@ -90,8 +91,8 @@ interface State {
getDeFiAssetReserveAssets(
asset: StoreAsset
): (
getPoolAssetReserveRate: (poolTokenUUID: string, assets: Asset[]) => ReserveAsset[]
) => StoreAsset[];
getPoolAssetReserveRate: (poolTokenUUID: string, assets: Asset[]) => ReserveAsset[]
) => StoreAsset[];
scanForMemberships(accounts: StoreAccount[]): void;
}
export const StoreContext = createContext({} as State);
Expand Down Expand Up @@ -141,8 +142,8 @@ export const StoreProvider: React.FC = ({ children }) => {

const membershipExpirations = memberships
? R.flatten(
Object.values(memberships).map((m) => Object.values(m.memberships).map((e) => e.expiry))
)
Object.values(memberships).map((m) => Object.values(m.memberships).map((e) => e.expiry))
)
: [];

const membershipState = (() => {
Expand Down

0 comments on commit acf2553

Please sign in to comment.