forked from tanner/qotnews
refactor: Refactor Submit component to use hooks
This commit is contained in:
@@ -1,23 +1,17 @@
|
||||
import React, { Component } from 'react';
|
||||
import { withRouter } from 'react-router-dom';
|
||||
import React, { useState, useRef } from 'react';
|
||||
import { useHistory } from 'react-router-dom';
|
||||
|
||||
class Submit extends Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
function Submit() {
|
||||
const [progress, setProgress] = useState(null);
|
||||
const inputRef = useRef(null);
|
||||
const history = useHistory();
|
||||
|
||||
this.state = {
|
||||
progress: null,
|
||||
};
|
||||
|
||||
this.inputRef = React.createRef();
|
||||
}
|
||||
|
||||
submitArticle = (event) => {
|
||||
const submitArticle = (event) => {
|
||||
event.preventDefault();
|
||||
const url = event.target[0].value;
|
||||
this.inputRef.current.blur();
|
||||
inputRef.current.blur();
|
||||
|
||||
this.setState({ progress: 'Submitting...' });
|
||||
setProgress('Submitting...');
|
||||
|
||||
let data = new FormData();
|
||||
data.append('url', url);
|
||||
@@ -26,29 +20,25 @@ class Submit extends Component {
|
||||
.then(res => res.json())
|
||||
.then(
|
||||
(result) => {
|
||||
this.props.history.replace('/' + result.nid);
|
||||
history.replace('/' + result.nid);
|
||||
},
|
||||
(error) => {
|
||||
this.setState({ progress: 'Error' });
|
||||
setProgress('Error');
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
render() {
|
||||
const progress = this.state.progress;
|
||||
|
||||
return (
|
||||
<span className='search'>
|
||||
<form onSubmit={this.submitArticle}>
|
||||
<input
|
||||
placeholder='Submit URL'
|
||||
ref={this.inputRef}
|
||||
/>
|
||||
</form>
|
||||
{progress ? progress : ''}
|
||||
</span>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<span className='search'>
|
||||
<form onSubmit={submitArticle}>
|
||||
<input
|
||||
placeholder='Submit URL'
|
||||
ref={inputRef}
|
||||
/>
|
||||
</form>
|
||||
{progress ? progress : ''}
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
||||
export default withRouter(Submit);
|
||||
export default Submit;
|
||||
|
||||
Reference in New Issue
Block a user