import React from 'react'; import request from 'utils/request'; import UrlMiddle from 'components/PublishUrlMiddleDisplay'; class PublishUrlInput extends React.Component { constructor (props) { super(props); this.handleInput = this.handleInput.bind(this); } componentDidMount () { const { claim, fileName } = this.props; if (!claim) { this.setClaimName(fileName); } } componentWillReceiveProps ({ claim, fileName }) { if (!claim) { return this.setClaimName(fileName); } this.checkClaimIsAvailable(claim); } handleInput (event) { let value = event.target.value; value = this.cleanseInput(value); // update the state this.props.onClaimChange(value); } cleanseInput (input) { input = input.replace(/\s+/g, '-'); // replace spaces with dashes input = input.replace(/[^A-Za-z0-9-]/g, ''); // remove all characters that are not A-Z, a-z, 0-9, or '-' return input; } setClaimName (fileName) { const fileNameWithoutEnding = fileName.substring(0, fileName.lastIndexOf('.')); const cleanClaimName = this.cleanseInput(fileNameWithoutEnding); this.props.onClaimChange(cleanClaimName); } checkClaimIsAvailable (claim) { request(`/api/claim/availability/${claim}`) .then(isAvailable => { console.log('checkClaimIsAvailable request response:', isAvailable); if (isAvailable) { this.props.onUrlError(null); } else { this.props.onUrlError('That url has already been claimed'); } }) .catch((error) => { this.props.onUrlError(error.message); }); } render () { return (
{this.props.urlError}