2018-01-05 03:34:17 +01:00
|
|
|
import React from 'react';
|
2018-01-11 20:41:12 +01:00
|
|
|
import { updateClaim } from '../actions/index';
|
2018-01-10 03:25:38 +01:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { makeGetRequest } from '../utils/xhr.js';
|
2018-01-11 20:41:12 +01:00
|
|
|
import UrlMiddle from '../components/PublishUrlMiddle.jsx';
|
2018-01-05 03:34:17 +01:00
|
|
|
|
2018-01-05 19:31:49 +01:00
|
|
|
class UrlChooser extends React.Component {
|
2018-01-05 03:34:17 +01:00
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
2018-01-05 22:59:25 +01:00
|
|
|
error : null,
|
|
|
|
host : 'spee.ch',
|
|
|
|
urlMiddle: null,
|
2018-01-05 03:34:17 +01:00
|
|
|
};
|
|
|
|
this.handleInput = this.handleInput.bind(this);
|
2018-01-10 03:25:38 +01:00
|
|
|
this.cleanseInput = this.cleanseInput.bind(this);
|
|
|
|
this.setClaimNameFromFileName = this.setClaimNameFromFileName.bind(this);
|
2018-01-05 19:31:49 +01:00
|
|
|
this.checkClaimIsAvailable = this.checkClaimIsAvailable.bind(this);
|
2018-01-05 03:34:17 +01:00
|
|
|
}
|
2018-01-10 03:25:38 +01:00
|
|
|
componentWillMount () {
|
|
|
|
if (!this.props.claim || this.props.claim === '') {
|
|
|
|
this.setClaimNameFromFileName();
|
|
|
|
}
|
|
|
|
}
|
2018-01-05 03:34:17 +01:00
|
|
|
handleInput (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
let value = event.target.value;
|
2018-01-10 03:25:38 +01:00
|
|
|
value = this.cleanseInput(value);
|
|
|
|
// update the state
|
|
|
|
this.props.onClaimChange(value);
|
|
|
|
// check to make sure claim name is available
|
2018-01-05 19:31:49 +01:00
|
|
|
this.checkClaimIsAvailable(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;
|
|
|
|
console.log('setClaimNameFromFileName', 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) {
|
2018-01-05 03:34:17 +01:00
|
|
|
const that = this;
|
2018-01-10 03:25:38 +01:00
|
|
|
makeGetRequest(`/api/claim-is-available/${claim}`)
|
2018-01-05 03:34:17 +01:00
|
|
|
.then(() => {
|
2018-01-05 22:59:25 +01:00
|
|
|
that.setState({'error': null});
|
2018-01-05 03:34:17 +01:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
2018-01-05 22:59:25 +01:00
|
|
|
that.setState({'error': error.message});
|
2018-01-05 03:34:17 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
render () {
|
|
|
|
return (
|
|
|
|
<div>
|
2018-01-10 20:26:01 +01:00
|
|
|
<p id="input-error-claim-name" className="info-message-placeholder info-message--failure">{this.state.error}</p>
|
|
|
|
<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
|
|
|
|
2018-01-10 20:26:01 +01:00
|
|
|
<span className="url-text--secondary">{this.state.host} / </span>
|
2018-01-05 03:34:17 +01:00
|
|
|
|
2018-01-10 20:26:01 +01:00
|
|
|
<UrlMiddle publishInChannel={this.props.publishInChannel} 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.state.error) && <span id="input-success-claim-name" className="info-message--success span--absolute">{'\u2713'}</span> }
|
2018-01-05 03:34:17 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-10 03:25:38 +01:00
|
|
|
const mapStateToProps = state => {
|
|
|
|
return {
|
|
|
|
fileName : state.file.name,
|
|
|
|
loggedInChannelName : state.loggedInChannel.name,
|
|
|
|
loggedInChannelShortId: state.loggedInChannel.shortId,
|
2018-01-10 03:49:26 +01:00
|
|
|
publishInChannel : state.publishInChannel,
|
2018-01-10 03:25:38 +01:00
|
|
|
claim : state.claim,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const mapDispatchToProps = dispatch => {
|
|
|
|
return {
|
|
|
|
onClaimChange: (value) => {
|
|
|
|
dispatch(updateClaim(value));
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(UrlChooser);
|