2018-01-03 02:12:57 +01:00
|
|
|
import React from 'react';
|
|
|
|
import ReactDOM from 'react-dom';
|
|
|
|
import Dropzone from './components/dropzone.jsx';
|
2018-01-05 01:10:25 +01:00
|
|
|
import PublishForm from './components/publishForm.jsx';
|
2018-01-03 02:12:57 +01:00
|
|
|
import PublishStatus from './components/publishStatus.jsx';
|
|
|
|
|
|
|
|
const DROPZONE = 'DROPZONE';
|
|
|
|
const DETAILS = 'DETAILS';
|
|
|
|
const STATUS = 'STATUS';
|
2018-01-04 19:39:51 +01:00
|
|
|
const initialState = {
|
2018-01-05 03:34:17 +01:00
|
|
|
showComponent : DROPZONE, // DROPZONE, DETAILS, or PUBLISHING
|
|
|
|
loggedInChannelName : null,
|
|
|
|
loggedInChannelShortId: null,
|
|
|
|
publishToChannel : false,
|
|
|
|
file : null,
|
|
|
|
title : '',
|
|
|
|
channel : null,
|
|
|
|
claim : '',
|
|
|
|
thumbnail : '',
|
|
|
|
description : '',
|
|
|
|
license : '',
|
|
|
|
nsfw : '',
|
2018-01-05 01:10:25 +01:00
|
|
|
};
|
2018-01-03 02:12:57 +01:00
|
|
|
|
|
|
|
class Uploader extends React.Component {
|
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
2018-01-04 19:39:51 +01:00
|
|
|
this.state = initialState;
|
2018-01-03 02:12:57 +01:00
|
|
|
// bind class methods with `this`
|
|
|
|
this.updateUploaderState = this.updateUploaderState.bind(this);
|
2018-01-04 19:39:51 +01:00
|
|
|
this.clearUploaderState = this.clearUploaderState.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 19:31:49 +01:00
|
|
|
this.cleanseClaimName = this.cleanseClaimName.bind(this);
|
2018-01-03 02:12:57 +01:00
|
|
|
}
|
2018-01-05 01:10:25 +01:00
|
|
|
componentDidMount () {
|
|
|
|
// check for whether a channel is logged in
|
|
|
|
// if so, setState loggedInChannel to the channel name
|
2018-01-05 03:34:17 +01:00
|
|
|
// const loggedInChannel = getCookie('channel_name');
|
|
|
|
// this.setState({loggedInChannel})
|
|
|
|
// const loggedInChannelShortId = getCookie('short_channel_id');
|
|
|
|
// this.setState({loggedInChannelShortId})
|
2018-01-05 01:10:25 +01:00
|
|
|
}
|
2018-01-03 02:12:57 +01:00
|
|
|
updateUploaderState (name, value) {
|
|
|
|
console.log(`updateUploaderState ${name} ${value}`);
|
|
|
|
this.setState({[name]: value});
|
|
|
|
}
|
2018-01-04 19:39:51 +01:00
|
|
|
clearUploaderState () {
|
|
|
|
this.setState(initialState);
|
|
|
|
}
|
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 = () => {
|
|
|
|
if (xhttp.readyState == 4 ) {
|
|
|
|
if ( xhttp.status == 200) {
|
|
|
|
resolve(xhttp.response);
|
|
|
|
} else if (xhttp.status == 403) {
|
|
|
|
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) {
|
|
|
|
resolve(xhttp.response);
|
|
|
|
} else if (xhttp.status == 403) {
|
|
|
|
reject('Wrong channel name or password');
|
|
|
|
} else {
|
|
|
|
reject('request failed with status:' + xhttp.status);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
xhttp.send(params);
|
|
|
|
});
|
|
|
|
}
|
2018-01-05 19:31:49 +01:00
|
|
|
cleanseClaimName (name) {
|
|
|
|
name = name.replace(/\s+/g, '-'); // replace spaces with dashes
|
|
|
|
name = name.replace(/[^A-Za-z0-9-]/g, ''); // remove all characters that are not A-Z, a-z, 0-9, or '-'
|
|
|
|
return name;
|
|
|
|
}
|
2018-01-03 02:12:57 +01:00
|
|
|
render () {
|
|
|
|
return (
|
2018-01-04 21:04:23 +01:00
|
|
|
<div className="row row--tall flex-container--column">
|
2018-01-03 02:12:57 +01:00
|
|
|
{ this.state.showComponent === DROPZONE &&
|
2018-01-05 19:31:49 +01:00
|
|
|
<Dropzone
|
|
|
|
updateUploaderState={this.updateUploaderState}
|
|
|
|
cleanseClaimName={this.cleanseClaimName}
|
|
|
|
/>
|
2018-01-03 02:12:57 +01:00
|
|
|
}
|
|
|
|
{ this.state.showComponent === DETAILS &&
|
2018-01-05 01:10:25 +01:00
|
|
|
<PublishForm
|
2018-01-03 02:12:57 +01:00
|
|
|
updateUploaderState={this.updateUploaderState}
|
2018-01-04 19:39:51 +01:00
|
|
|
clearUploaderState={this.clearUploaderState}
|
2018-01-05 03:34:17 +01:00
|
|
|
makeGetRequest={this.makeGetRequest}
|
2018-01-05 19:31:49 +01:00
|
|
|
cleanseClaimName={this.cleanseClaimName}
|
2018-01-05 03:34:17 +01:00
|
|
|
loggedInChannelName={this.state.loggedInChannelName}
|
|
|
|
loggedInChannelShortId={this.state.loggedInChannelShortId}
|
2018-01-05 01:10:25 +01:00
|
|
|
publishToChannel={this.state.publishToChannel}
|
2018-01-03 02:12:57 +01:00
|
|
|
file={this.state.file}
|
|
|
|
title={this.state.title}
|
|
|
|
channel={this.state.channel}
|
2018-01-05 03:34:17 +01:00
|
|
|
claim={this.state.claim}
|
2018-01-03 02:12:57 +01:00
|
|
|
thumbnail={this.state.thumbnail}
|
|
|
|
description={this.state.description}
|
|
|
|
license={this.state.license}
|
|
|
|
nsfw={this.state.nsfw}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
{ this.state.showComponent === STATUS &&
|
|
|
|
<PublishStatus />
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ReactDOM.render(
|
|
|
|
<Uploader />,
|
|
|
|
document.getElementById('react-uploader')
|
|
|
|
);
|