spee.ch/client/containers/PublishUrlInput/view.jsx

83 lines
2.9 KiB
React
Raw Normal View History

2018-01-05 03:34:17 +01:00
import React from 'react';
import request from 'utils/request';
2018-01-24 20:09:20 +01:00
import UrlMiddle from 'components/PublishUrlMiddleDisplay';
2018-01-05 03:34:17 +01:00
2018-01-17 19:49:57 +01:00
class PublishUrlInput extends React.Component {
2018-01-05 03:34:17 +01:00
constructor (props) {
super(props);
this.handleInput = this.handleInput.bind(this);
}
componentDidMount () {
const { claim, fileName } = this.props;
if (!claim) {
this.setClaimName(fileName);
2018-01-10 03:25:38 +01:00
}
}
componentWillReceiveProps ({ claim, fileName }) {
2018-03-06 07:45:39 +01:00
// if a new file was chosen, update the claim name
if (fileName !== this.props.fileName) {
return this.setClaimName(fileName);
}
2018-03-06 07:45:39 +01:00
// if the claim has updated, check its availability
if (claim !== this.props.claim) {
this.validateClaim(claim);
}
}
2018-01-05 03:34:17 +01:00
handleInput (event) {
let value = event.target.value;
2018-01-10 03:25:38 +01:00
value = this.cleanseInput(value);
// update the state
this.props.onClaimChange(value);
2018-01-05 03:34:17 +01:00
}
2018-01-10 03:25:38 +01:00
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) {
2018-01-10 03:25:38 +01:00
const fileNameWithoutEnding = fileName.substring(0, fileName.lastIndexOf('.'));
const cleanClaimName = this.cleanseInput(fileNameWithoutEnding);
this.props.onClaimChange(cleanClaimName);
}
2018-03-06 07:45:39 +01:00
validateClaim (claim) {
if (!claim) {
return this.props.onUrlError('Enter a url above');
}
2018-02-07 06:55:04 +01:00
request(`/api/claim/availability/${claim}`)
2018-03-06 07:55:56 +01:00
.then(() => {
this.props.onUrlError(null);
2018-01-05 03:34:17 +01:00
})
.catch((error) => {
2018-02-07 00:05:31 +01:00
this.props.onUrlError(error.message);
2018-01-05 03:34:17 +01:00
});
}
render () {
2018-03-06 07:45:39 +01:00
const { claim, loggedInChannelName, loggedInChannelShortId, publishInChannel, selectedChannel, urlError } = this.props;
2018-01-05 03:34:17 +01:00
return (
2018-03-06 07:45:39 +01:00
<div className='column column--10 column--sml-10'>
<div className='input-text--primary span--relative'>
<span className='url-text--secondary'>spee.ch / </span>
<UrlMiddle
2018-03-06 07:45:39 +01:00
publishInChannel={publishInChannel}
selectedChannel={selectedChannel}
loggedInChannelName={loggedInChannelName}
loggedInChannelShortId={loggedInChannelShortId}
/>
2018-03-06 07:45:39 +01:00
<input type='text' id='claim-name-input' className='input-text' name='claim' placeholder='your-url-here' onChange={this.handleInput} value={claim} />
{ (claim && !urlError) && <span id='input-success-claim-name' className='info-message--success span--absolute'>{'\u2713'}</span> }
{ urlError && <span id='input-success-channel-name' className='info-message--failure span--absolute'>{'\u2716'}</span> }
</div>
<div>
{ urlError ? (
<p id='input-error-claim-name' className='info-message--failure'>{urlError}</p>
) : (
<p className='info-message'>Choose a custom url</p>
)}
2018-01-05 03:34:17 +01:00
</div>
</div>
);
}
}
2018-01-17 19:49:57 +01:00
export default PublishUrlInput;