// @flow import React, { useState } from 'react'; import { FormField } from 'component/common/form'; import Button from 'component/button'; import SelectAsset from 'component/selectAsset'; import TagsSelect from 'component/tagsSelect'; import * as PAGES from 'constants/pages'; import { MINIMUM_PUBLISH_BID } from 'constants/claim'; type Props = { claim: ChannelClaim, title: ?string, amount: string, coverUrl: ?string, thumbnailUrl: ?string, location: { search: string }, description: string, website: string, email: string, balance: number, tags: Array, locations: Array, languages: Array, updateChannel: any => void, updateThumb: string => void, updateCover: string => void, setEditing: boolean => void, }; function ChannelForm(props: Props) { const { claim, title, coverUrl, description, website, email, thumbnailUrl, tags, locations, languages, amount, setEditing, updateChannel, updateThumb, updateCover, } = props; const { claim_id: claimId } = claim; // fill this in with sdk data const channelParams = { website, email, coverUrl, thumbnailUrl, description, title, amount, claim_id: claimId, languages: languages || [], locations: locations || [], tags: tags ? tags.map(tag => { return { name: tag }; }) : [], }; const [params, setParams] = useState(channelParams); const [bidError, setBidError] = useState(''); // If a user changes tabs, update the url so it stays on the same page if they refresh. // We don't want to use links here because we can't animate the tab change and using links // would alter the Tab label's role attribute, which should stay role="tab" to work with keyboards/screen readers. const handleBidChange = (bid: number) => { const { balance, amount } = props; const totalAvailableBidAmount = parseFloat(amount) + parseFloat(balance); setParams({ ...params, amount: bid }); setBidError(''); if (bid <= 0.0 || isNaN(bid)) { setBidError(__('Deposit cannot be 0')); } else if (totalAvailableBidAmount === bid) { setBidError(__('Please decrease your deposit to account for transaction fees')); } else if (totalAvailableBidAmount < bid) { setBidError(__('Deposit cannot be higher than your balance')); } else if (bid < MINIMUM_PUBLISH_BID) { setBidError(__('Your deposit must be higher')); } }; const handleThumbnailChange = (thumbnailUrl: string) => { setParams({ ...params, thumbnailUrl }); updateThumb(thumbnailUrl); }; const handleCoverChange = (coverUrl: string) => { setParams({ ...params, coverUrl }); updateCover(coverUrl); }; const handleSubmit = () => { updateChannel(params); setEditing(false); }; // TODO clear and bail after submit return (
handleThumbnailChange(v)} currentValue={params.thumbnailUrl} assetName={'Thumbnail'} recommended={'(Minimum size: 300x300, (1:1)'} /> handleCoverChange(v)} currentValue={params.coverUrl} assetName={'Cover'} recommended={'(Minimum size: 1000 x 160, (10:1.6)'} /> setParams({ ...params, title: e.target.value })} /> handleBidChange(parseFloat(event.target.value))} placeholder={0.1} /> setParams({ ...params, website: e.target.value })} /> setParams({ ...params, email: e.target.value })} /> setParams({ ...params, description: text })} /> { newTags.forEach(newTag => { if (!params.tags.map(savedTag => savedTag.name).includes(newTag.name)) { setParams({ ...params, tags: [...params.tags, newTag] }); } else { // If it already exists and the user types it in, remove it setParams({ ...params, tags: params.tags.filter(tag => tag.name !== newTag.name) }); } }); }} onRemove={clickedTag => { const newTags = params.tags.slice().filter(tag => tag.name !== clickedTag.name); setParams({ ...params, tags: newTags }); }} tagsChosen={params.tags || []} />
); } export default ChannelForm;