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

Issue 1872 - Antify Deposit/Withdraw Modals #2495

Merged
merged 5 commits into from
Mar 21, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
165 changes: 115 additions & 50 deletions app/components/DepositWithdraw/DepositWithdrawAssetSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,76 +2,141 @@ import React from "react";
import {connect} from "alt-react";
import BindToChainState from "../Utility/BindToChainState";
import GatewayStore from "stores/GatewayStore";
import TypeAhead from "../Utility/TypeAhead";
import counterpart from "counterpart";
import {Select} from "bitshares-ui-style-guide";

class DepositWithdrawAssetSelector extends React.Component {
constructor(props) {
super(props);
}

render() {
const {props} = this;
const {include} = props;
let idMap = {};

let getCoinOption = item => {
/* Gateway Specific Settings */
let [gateway, backedCoin] = item.symbol.split(".");

// Return null if backedCoin is already stored
if (!idMap[backedCoin] && backedCoin && gateway) {
idMap[backedCoin] = true;

return {
id: backedCoin,
label: backedCoin,
gateway: gateway,
gateFee: item.gateFee,
issuer: item.issuerId || "1.2.96397" //Fall back to open ledger
};
} else {
return null;
}
this.state = {
assets: []
};
}

let coinArr = [];
componentDidMount() {
this.getAssets();
}

if (!(this.props.includeBTS === false)) {
coinArr.push({id: "BTS", label: "BTS", gateway: ""});
}
getAssets() {
let {backedCoins, include, includeBTS} = this.props;
let {assets} = this.state;

props.backedCoins.forEach(coin => {
coinArr = coinArr
.concat(coin.map(getCoinOption))
.filter(item => {
return item;
let idMap = {};

backedCoins.forEach(coin => {
assets = assets
.concat(coin.map(
item => {
/* Gateway Specific Settings */
let [gateway, backedCoin] = item.symbol.split(".");

// Return null if backedCoin is already stored
if (!idMap[backedCoin] && backedCoin && gateway) {
idMap[backedCoin] = true;

return {
id: backedCoin,
label: backedCoin,
gateway: gateway,
gateFee: item.gateFee,
issuer: item.issuerId || "1.2.96397" //Fall back to open ledger
};
} else {
return null;
}
}
))
.filter(item => {
return item;
})
.filter(item => {
if (item.id == "BTS") return true;
if (include) {
return include.includes(item.id);
}
if (item.id == "BTS") { return true; }
if (include) { return include.includes(item.id); }
return true;
});
});

let coinItems = coinArr.sort(function(a, b) {
if (a.id && b.id) return a.id.localeCompare(b.id);
if (!(includeBTS === false)) {
assets.push({id: "BTS", label: "BTS", gateway: ""});
}

this.setState({
assets: assets
});

let i18n =
props.usageContext == "withdraw"
? "gateway.asset_search_withdraw"
: "gateway.asset_search_deposit";
}

getSelectedAssetArray(selectedAsset) {
let {assets} = this.state;

let asset;

assets.map(a => {
if(a.id == selectedAsset) {
asset = a;
}
});

return asset;
}

_onSelect(selectedAsset) {
let {onSelect} = this.props;
let asset = this.getSelectedAssetArray(selectedAsset);

if(onSelect) {
onSelect(asset);
}
}

_onInputChanged(selectedAsset) {
let {onChange} = this.props;
let asset = this.getSelectedAssetArray(selectedAsset);

if(onChange) {
onChange(asset.id);
}
}

render() {
let {usageContext} = this.props;

let coinItems = this.state.assets.sort(function(a, b) {
if (a.id && b.id) return a.id.localeCompare(b.id);
});

return (
<TypeAhead
items={coinItems}
{...this.props}
inputProps={{placeholder: counterpart.translate(i18n)}}
label="gateway.asset"
/>
<Select
onSelect={this._onSelect.bind(this)}
onChange={this._onInputChanged.bind(this)}
onSearch={this._onInputChanged.bind(this)}
placeholder={counterpart.translate(
usageContext == "withdraw"
? "gateway.asset_search_withdraw"
: "gateway.asset_search_deposit"
)}
value={this.props.defaultValue}
optionLabelProp={"value"}
showSearch
style={{width: "100%"}}
>
{/*
NOTE
On Deposit, it would be useful to view Min Deposit
and Gateway Fee to the right of the selection so the
user doesn't have to select a specific gateway to view
this information.
*/}
{coinItems.map(coin => (
<Select.Option
key={coin.id}
value={coin.label}
>
{coin.label}
</Select.Option>
))}
</Select>
);
}
}
Expand Down
28 changes: 14 additions & 14 deletions app/components/Modal/DepositModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,21 @@ class DepositModalContent extends DecimalChecker {
}

shouldComponentUpdate(np, ns) {
return !utils.are_equal_shallow(ns, this.state);
return !utils.are_equal_shallow(ns, this.state) ||
np.asset !== this.props.asset;
}

onGatewayChanged(e) {
if (!e.target.value) return;
this._getDepositAddress(this.state.selectedAsset, e.target.value);
onGatewayChanged(selectedGateway) {
this._getDepositAddress(this.state.selectedAsset, selectedGateway);
}

onAssetSelected(asset, assetDetails) {
if (assetDetails.gateway == "")
return this.setState({selectedAsset: asset, selectedGateway: null});
onAssetSelected(asset) {
if (asset.gateway == "")
return this.setState({selectedAsset: asset.id, selectedGateway: null});

let {selectedAsset, selectedGateway} = _onAssetSelected.call(
this,
asset,
asset.id,
"depositAllowed",
(availableGateways, balancesByGateway) => {
if (availableGateways && availableGateways.length == 1)
Expand Down Expand Up @@ -258,7 +258,7 @@ class DepositModalContent extends DecimalChecker {
const QR = isAddressValid ? (
<CryptoLinkFormatter
size={140}
address={depositAddress.address}
address={usingGateway ? depositAddress.address : account}
asset={selectedAsset}
/>
) : (
Expand All @@ -281,7 +281,7 @@ class DepositModalContent extends DecimalChecker {
<div className="no-margin no-padding">
<div className="inline-label input-wrapper">
<DepositWithdrawAssetSelector
defaultValue={selectedAsset}
defaultValue={this.state.selectedAsset}
onSelect={this.onAssetSelected.bind(this)}
selectOnBlur
/>
Expand All @@ -300,9 +300,9 @@ class DepositModalContent extends DecimalChecker {
: null}

{!fetchingAddress ? (
(!usingGateway ||
(usingGateway &&
selectedGateway &&
(!usingGateway ||
(usingGateway &&
selectedGateway &&
gatewayStatus[selectedGateway].options
.enabled)) &&
isAddressValid &&
Expand All @@ -317,7 +317,7 @@ class DepositModalContent extends DecimalChecker {
) : (
<div
className="container-row"
style={{textAlign: "center"}}
style={{textAlign: "center", paddingTop: 15}}
>
<LoadingIndicator type="three-bounce" />
</div>
Expand Down
Loading