spee.ch/react/containers/PublishUrlInput.jsx

118 lines
4.1 KiB
React
Raw Normal View History

2018-01-05 03:34:17 +01:00
import React from 'react';
import { updateClaim } from '../actions/index';
2018-01-10 03:25:38 +01:00
import { connect } from 'react-redux';
import { makeGetRequest } from '../utils/xhr.js';
import UrlMiddle from '../components/PublishUrlMiddle.jsx';
import {updateError} from '../actions';
2018-01-13 00:02:42 +01:00
import PropTypes from 'prop-types';
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
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();
}
}
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) {
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);
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) {
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}`)
.then(response => {
if (response) {
that.props.onUrlError(null);
} else {
that.props.onUrlError('That url has already been claimed');
}
2018-01-05 03:34:17 +01:00
})
.catch((error) => {
that.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
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.props.urlError) && <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,
publishInChannel : state.publishInChannel,
2018-01-10 03:25:38 +01:00
claim : state.claim,
urlError : state.error.url,
2018-01-10 03:25:38 +01:00
};
};
const mapDispatchToProps = dispatch => {
return {
onClaimChange: (value) => {
dispatch(updateClaim(value));
dispatch(updateError('publishSubmit', null));
2018-01-10 03:25:38 +01:00
},
onUrlError: (value) => {
dispatch(updateError('url', value));
},
2018-01-10 03:25:38 +01:00
};
2018-01-13 00:02:42 +01:00
};
UrlChooser.propTypes = {
fileName : PropTypes.string.isRequired,
loggedInChannelName : PropTypes.string.isRequired,
loggedInChannelShortId: PropTypes.string.isRequired,
publishInChannel : PropTypes.bool.isRequired,
claim : PropTypes.string.isRequired,
urlError : PropTypes.string,
};
2018-01-10 03:25:38 +01:00
export default connect(mapStateToProps, mapDispatchToProps)(UrlChooser);