Skip to content

Commit

Permalink
frontend: only show buyable coins in moonpay dropdown
Browse files Browse the repository at this point in the history
The dropdown checks now each coin if buying is supported and
only shows those accounts.
  • Loading branch information
thisconnect committed Jan 14, 2021
1 parent 24387bd commit 9c2c669
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions frontends/web/src/routes/buy/info.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ import { load } from '../../decorators/load';
import { translate, TranslateProps } from '../../decorators/translate';
import { Button, Checkbox, Select } from '../../components/forms';
import { Devices } from '../device/deviceswitch';
import { AccountInterface } from '../account/account';
import { AccountInterface, CoinCode } from '../account/account';
import { setConfig } from '../../utils/config';
import { apiGet } from '../../utils/request';
import * as style from './info.css';

interface BuyInfoProps {
Expand All @@ -37,9 +38,15 @@ interface LoadedBuyInfoProps {
config: any;
}

interface Option {
text: string;
value: CoinCode;
}

interface State {
status: 'choose' | 'agree'
selected?: string;
options: Option[]
}

type Props = BuyInfoProps & LoadedBuyInfoProps & TranslateProps;
Expand All @@ -48,6 +55,11 @@ class BuyInfo extends Component<Props, State> {
public readonly state: State = {
status: this.props.config.frontend.skipBuyDisclaimer ? 'choose' : 'agree',
selected: this.props.code,
options: [],
}

public componentDidMount() {
this.checkSupportedCoins();
}

private handleProceed = () => {
Expand All @@ -59,17 +71,31 @@ class BuyInfo extends Component<Props, State> {
}
}

private checkSupportedCoins = () => {
Promise.all(
this.props.accounts.map((account) => (
apiGet(`account/${account.code}/exchange/moonpay/buy-supported`)
.then(isSupported => (isSupported ? account : false))
))
)
.then(results => results.filter(result => result))
// @ts-ignore
.then(accounts => accounts.map(({ name, code }) => ({ text: name, value: code })))
.then(options => () => this.setState({ options }))
.catch(console.error);
}

private handleSkipDisclaimer = (e) => {
setConfig({ frontend: { skipBuyDisclaimer: e.target.checked }});
}

public render(
{ accounts,
code,
{ code,
t }: RenderableProps<Props>,
{
status,
selected,
options,
}: State
) {
const name = (code === 'btc' || code === 'tbtc') ? 'Bitcoin' : 'crypto';
Expand Down Expand Up @@ -170,17 +196,14 @@ class BuyInfo extends Component<Props, State> {
label={t('buy.info.selectLabel')}
placeholder={t('buy.info.selectPlaceholder')}
options={[{
text: t('buy.info.selectPlaceholder'),
text: t(options.length ? 'buy.info.selectPlaceholder' : 'loading'),
disabled: true,
value: 'choose',
},
...accounts.map(({ code, name}) => ({
text: `${name} (${code})`,
value: code,
}))]
...options]
}
onChange={e => this.setState({ selected: e.target.value})}
value={selected}
value={options.length ? selected : 'choose'}
defaultValue={'choose'}
id="coinAndAccountCode"
/>
Expand Down

0 comments on commit 9c2c669

Please sign in to comment.