spee.ch/react/components/PublishTool.jsx

121 lines
3.8 KiB
React
Raw Normal View History

import React from 'react';
2018-01-09 02:06:31 +01:00
import PublishDropzone from './PublishDropzone.jsx';
import PublishForm from './PublishForm.jsx';
import PublishStatus from './PublishStatus.jsx';
import {connect} from 'react-redux';
2018-01-06 03:26:57 +01:00
class PublishTool extends React.Component {
constructor (props) {
super(props);
// bind class methods with `this`
this.updateUploaderState = this.updateUploaderState.bind(this);
2018-01-05 03:34:17 +01:00
this.makeGetRequest = this.makeGetRequest.bind(this);
this.makePostRequest = this.makePostRequest.bind(this);
2018-01-05 22:59:25 +01:00
this.cleanseInput = this.cleanseInput.bind(this);
this.getCookie = this.getCookie.bind(this);
}
componentDidMount () {
// check for whether a channel is logged in
// if so, setState loggedInChannel to the channel name
const loggedInChannelName = this.getCookie('channel_name');
this.setState({loggedInChannelName})
const loggedInChannelShortId = this.getCookie('short_channel_id');
this.setState({loggedInChannelShortId});
}
updateUploaderState (name, value) {
console.log(`updateUploaderState ${name} ${value}`);
this.setState({[name]: value});
}
2018-01-05 03:34:17 +01:00
makeGetRequest (url) {
return new Promise((resolve, reject) => {
let xhttp = new XMLHttpRequest();
xhttp.open('GET', url, true);
xhttp.responseType = 'json';
xhttp.onreadystatechange = () => {
2018-01-06 03:26:57 +01:00
if (xhttp.readyState === 4) {
if (xhttp.status === 200) {
2018-01-05 03:34:17 +01:00
resolve(xhttp.response);
2018-01-06 03:26:57 +01:00
} else if (xhttp.status === 401) {
2018-01-05 03:34:17 +01:00
reject('Wrong channel name or password');
} else {
reject('request failed with status:' + xhttp.status);
};
}
};
xhttp.send();
});
}
makePostRequest (url, params) {
return new Promise((resolve, reject) => {
let xhttp = new XMLHttpRequest();
xhttp.open('POST', url, true);
xhttp.responseType = 'json';
xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhttp.onreadystatechange = () => {
if (xhttp.readyState === 4) {
if (xhttp.status === 200) {
2018-01-05 03:34:17 +01:00
resolve(xhttp.response);
2018-01-06 03:26:57 +01:00
} else if (xhttp.status === 401) {
2018-01-05 03:34:17 +01:00
reject('Wrong channel name or password');
} else {
reject('request failed with status:' + xhttp.status);
};
}
};
xhttp.send(params);
});
}
2018-01-05 22:59:25 +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;
2018-01-05 19:31:49 +01:00
}
getCookie (cname) {
const name = cname + '=';
const decodedCookie = decodeURIComponent(document.cookie);
const ca = decodedCookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1);
}
if (c.indexOf(name) === 0) {
return c.substring(name.length, c.length);
}
}
return '';
}
render () {
return (
2018-01-04 21:04:23 +01:00
<div className="row row--tall flex-container--column">
2018-01-09 02:06:31 +01:00
{ !this.props.file &&
2018-01-06 03:26:57 +01:00
<PublishDropzone
2018-01-05 19:31:49 +01:00
updateUploaderState={this.updateUploaderState}
2018-01-05 22:59:25 +01:00
cleanseInput={this.cleanseInput}
2018-01-05 19:31:49 +01:00
/>
}
2018-01-09 02:06:31 +01:00
{ this.props.file &&
<PublishForm
updateUploaderState={this.updateUploaderState}
clearUploaderState={this.clearUploaderState}
2018-01-05 03:34:17 +01:00
makeGetRequest={this.makeGetRequest}
2018-01-06 00:11:45 +01:00
makePostRequest={this.makePostRequest}
2018-01-05 22:59:25 +01:00
cleanseInput={this.cleanseInput}
/>
}
2018-01-09 02:06:31 +01:00
{ this.props.publishStatus &&
<PublishStatus />
}
</div>
);
}
};
2018-01-09 02:06:31 +01:00
const mapStateToProps = state => {
return {
file: state.file,
};
};
export default connect(mapStateToProps, null)(PublishTool);