Smol changes
This commit is contained in:
parent
658a783f6a
commit
8a6645f560
9 changed files with 68 additions and 23 deletions
|
|
@ -1,3 +1,3 @@
|
|||
import axios from 'axios';
|
||||
|
||||
export default (url) => axios.put('/api/url', { url }).then((resp) => resp.data);
|
||||
export default (url, slug) => axios.put('/api/url', { url, slug }).then((resp) => resp.data);
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import putUrlHandler from '../axios/putUrlHandler';
|
|||
export default () => {
|
||||
const [state, setState] = useState({
|
||||
url: null,
|
||||
slug: null,
|
||||
loading: false,
|
||||
result: null,
|
||||
error: false,
|
||||
|
|
@ -17,7 +18,9 @@ export default () => {
|
|||
evt.preventDefault();
|
||||
if (state.url) {
|
||||
setState({ ...state, loading: true });
|
||||
putUrlHandler(state.url).then((data) => {
|
||||
putUrlHandler(
|
||||
state.url, state.slug,
|
||||
).then((data) => {
|
||||
setState({
|
||||
...state,
|
||||
result: `${window.location.origin}/${data}`,
|
||||
|
|
@ -25,8 +28,13 @@ export default () => {
|
|||
error: false,
|
||||
});
|
||||
}).catch((ex) => {
|
||||
if (ex.response && ex.response.data === 'INVALID URL') {
|
||||
setState({ ...state, error: true });
|
||||
if (ex.response?.data?.error) {
|
||||
setState({
|
||||
...state,
|
||||
result: null,
|
||||
loading: false,
|
||||
error: ex.response.data.error,
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -35,8 +43,7 @@ export default () => {
|
|||
return (
|
||||
<CSSTransition classNames="react-router" appear in timeout={300}>
|
||||
<Container className="perfect-width">
|
||||
<h2 className="text-center mt-10 py-3">URL Shortener</h2>
|
||||
|
||||
<h2 className="text-center mt-10 py-3">Link Shortener</h2>
|
||||
<Form onSubmit={submit} className="perfect-width">
|
||||
<Form.Group>
|
||||
<InputGroup>
|
||||
|
|
@ -47,27 +54,37 @@ export default () => {
|
|||
onChange={(evt) => setState({ ...state, url: evt.target.value })}
|
||||
required
|
||||
/>
|
||||
<InputGroup.Append>
|
||||
<Form.Control
|
||||
type="text"
|
||||
style={{ width: '8rem', borderRadius: '0' }}
|
||||
value={state.slug}
|
||||
placeholder="Optional Slug"
|
||||
onChange={(evt) => setState({ ...state, slug: evt.target.value })}
|
||||
/>
|
||||
</InputGroup.Append>
|
||||
<InputGroup.Append>
|
||||
<Button type="submit" disabled={state.loading || !state.url}>
|
||||
{state.loading ? 'Loading...' : 'Submit'}
|
||||
</Button>
|
||||
</InputGroup.Append>
|
||||
</InputGroup>
|
||||
{state.error && (
|
||||
<Form.Text className="text-danger">
|
||||
Please enter a valid URL
|
||||
</Form.Text>
|
||||
)}
|
||||
<Form.Text className="danger" />
|
||||
</Form.Group>
|
||||
</Form>
|
||||
|
||||
{state.error && (
|
||||
<h5 className="text-center text-danger">
|
||||
{state.error}
|
||||
</h5>
|
||||
)}
|
||||
|
||||
{state.result && (
|
||||
<h5 className="text-center">
|
||||
<a rel="noopener noreferrer" href={state.result} target="_blank">
|
||||
{state.result}
|
||||
</a>
|
||||
</h5>
|
||||
<h5 className="text-center">
|
||||
<a rel="noopener noreferrer" href={state.result} target="_blank">
|
||||
{state.result}
|
||||
</a>
|
||||
</h5>
|
||||
)}
|
||||
</Container>
|
||||
</CSSTransition>
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import NavLinkItem from './NavLinkItem';
|
|||
|
||||
export default () => (
|
||||
<Navbar expand="lg" collapseOnSelect bg="dark" variant="dark" fixed="top">
|
||||
<Navbar.Brand>{window.location.origin.substr(window.location.origin.indexOf('//') + 2)}</Navbar.Brand>
|
||||
<Navbar.Brand>dv2ls: Dev Tools</Navbar.Brand>
|
||||
<Navbar.Toggle aria-controls="responsive-navbar-nav" />
|
||||
<Navbar.Collapse>
|
||||
<Nav className="mr-auto">
|
||||
|
|
|
|||
Loading…
Reference in a new issue