refactor: Refactor Submit component to use hooks

This commit is contained in:
2025-07-07 19:47:00 +00:00
committed by Tanner Collin
parent c4f2e7d595
commit 1df1c59d61

View File

@@ -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}>
<form onSubmit={submitArticle}>
<input
placeholder='Submit URL'
ref={this.inputRef}
ref={inputRef}
/>
</form>
{progress ? progress : ''}
</span>
);
}
}
export default withRouter(Submit);
export default Submit;