// @flow import { CHANNEL_NEW, CHANNEL_ANONYMOUS } from 'constants/claim'; import React, { Fragment } from 'react'; import { FormField } from 'component/common/form'; import ChannelCreate from 'component/channelCreate'; type Props = { channel: string, // currently selected channel channels: ?Array, balance: number, onChannelChange: string => void, createChannel: (string, number) => Promise, fetchChannelListMine: () => void, fetchingChannels: boolean, hideAnon: boolean, hideNew: boolean, label?: string, injected?: Array, emailVerified: boolean, }; type State = { addingChannel: boolean, }; class ChannelSelection extends React.PureComponent { constructor(props: Props) { super(props); this.state = { addingChannel: false, }; (this: any).handleChannelChange = this.handleChannelChange.bind(this); (this: any).handleChangeToNewChannel = this.handleChangeToNewChannel.bind(this); } componentDidMount() { const { channels, fetchChannelListMine, fetchingChannels, emailVerified } = this.props; if (IS_WEB && !emailVerified) { return; } if ((!channels || !channels.length) && !fetchingChannels) { fetchChannelListMine(); } } componentDidUpdate() { const { channels, fetchingChannels } = this.props; if (!fetchingChannels && !channels) { this.setState({ addingChannel: true }); } } handleChannelChange(event: SyntheticInputEvent<*>) { const { onChannelChange } = this.props; const channel = event.target.value; if (channel === CHANNEL_NEW) { this.setState({ addingChannel: true }); onChannelChange(channel); } else { this.setState({ addingChannel: false }); onChannelChange(channel); } } handleChangeToNewChannel(props: Object) { const { onChannelChange } = this.props; const { newChannelName } = props; this.setState({ addingChannel: false }); const channelName = `@${newChannelName.trim()}`; onChannelChange(channelName); } render() { const channel = this.state.addingChannel ? 'new' : this.props.channel; const { fetchingChannels, channels = [], hideAnon, hideNew, label, injected = [] } = this.props; const { addingChannel } = this.state; return ( {!hideAnon && } {channels && channels.map(({ name, claim_id: claimId }) => ( ))} {injected && injected.map(item => ( ))} {!fetchingChannels && !hideNew && } {addingChannel && } ); } } export default ChannelSelection;