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

80 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 () {
2018-01-10 03:25:38 +01:00
if (!this.props.claim || this.props.claim === '') {
this.setClaimNameFromFileName();
}
}
componentWillReceiveProps ({claim: newClaim}) {
if (newClaim) {
this.checkClaimIsAvailable(newClaim);
} else {
this.props.onUrlError('Please enter a URL');
}
}
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;
}
setClaimNameFromFileName () {
const fileName = this.props.fileName;
const fileNameWithoutEnding = fileName.substring(0, fileName.lastIndexOf('.'));
const cleanClaimName = this.cleanseInput(fileNameWithoutEnding);
this.props.onClaimChange(cleanClaimName);
}
2018-01-05 19:31:49 +01:00
checkClaimIsAvailable (claim) {
request(`/api/claim-is-available/${claim}`)
.then(isAvailable => {
// console.log('checkClaimIsAvailable request response:', isAvailable);
if (isAvailable) {
2018-02-07 00:05:31 +01:00
this.props.onUrlError(null);
} else {
2018-02-07 00:05:31 +01:00
this.props.onUrlError('That url has already been claimed');
}
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 () {
return (
<div>
<p id="input-error-claim-name" className="info-message-placeholder info-message--failure">{this.props.urlError}</p>
2018-01-10 20:26:01 +01:00
<div className="column column--3 column--sml-10">
<label className="label">URL:</label>
</div><div className="column column--7 column--sml-10 input-text--primary span--relative">
2018-01-05 03:34:17 +01:00
<span className="url-text--secondary">spee.ch / </span>
2018-01-05 03:34:17 +01:00
<UrlMiddle
publishInChannel={this.props.publishInChannel}
selectedChannel={this.props.selectedChannel}
loggedInChannelName={this.props.loggedInChannelName}
loggedInChannelShortId={this.props.loggedInChannelShortId}
/>
2018-01-05 03:34:17 +01:00
2018-01-10 20:26:01 +01:00
<input type="text" id="claim-name-input" className="input-text" name='claim' placeholder="your-url-here" onChange={this.handleInput} value={this.props.claim}/>
{ (this.props.claim && !this.props.urlError) && <span id="input-success-claim-name" className="info-message--success span--absolute">{'\u2713'}</span> }
2018-01-19 23:53:31 +01:00
{ this.props.urlError && <span id="input-success-channel-name" className="info-message--failure span--absolute">{'\u2716'}</span> }
2018-01-05 03:34:17 +01:00
</div>
</div>
);
}
}
2018-01-17 19:49:57 +01:00
export default PublishUrlInput;