Smol changes

This commit is contained in:
oitsjustjose 2021-06-22 10:18:26 -05:00
commit 8a6645f560
9 changed files with 68 additions and 23 deletions

View file

@ -1,4 +1,5 @@
import { Request, Response } from "express";
import shortid from "shortid";
import URLs from "../../db/url.schema";
const isUrlValid = (url: string) => {
@ -8,11 +9,33 @@ const isUrlValid = (url: string) => {
export default async (req: Request, res: Response) => {
try {
if (!isUrlValid(req.body.url)) {
return res.status(422).send("INVALID URL");
return res.status(422).send({ error: "URL Provided is Invalid" });
}
// Validate that there isn't a conflict
if (req.body.slug) {
const conflict = await URLs.findById(req.body.slug);
if (!!conflict) {
return res
.status(409)
.send({ error: `Slug '${req.body.slug}' is already registered.` });
}
} else {
// Only return existing matches if the slug isn't custom
// Custom ones can continue to conflict :)
const existing = await URLs.findOne({
url: req.body.url,
isSlugCustom: false,
});
if (existing) {
return res.status(200).send(existing._id);
}
}
const url = new URLs();
url.url = req.body.url;
url.isSlugCustom = !!req.body.slug;
url._id = req.body.slug || shortid.generate();
await url.save();
return res.status(200).send(url._id);

View file

@ -7,11 +7,16 @@ const UrlSchema: Schema = new Schema({
default: shortid.generate,
},
url: String,
isSlugCustom: {
type: Boolean,
default: false,
},
});
export type UrlModel = Document & {
_id: string;
url: string;
isSlugCustom: boolean;
};
export default mongoose.model<UrlModel>("urls", UrlSchema, "urls");

1
frontend/.gitignore vendored
View file

@ -21,3 +21,4 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*
**/.DS_Store

Binary file not shown.

Before

Width:  |  Height:  |  Size: 343 KiB

After

Width:  |  Height:  |  Size: 478 KiB

Before After
Before After

View file

@ -5,13 +5,10 @@
<link rel="icon" href="%PUBLIC_URL%/app-icon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#17a2b8" />
<meta
name="description"
content="Utils for Devs!"
/>
<meta name="description" content="🧰 Tools for Devs 👨‍💻" />
<link rel="apple-touch-icon" href="%PUBLIC_URL%/app-icon.png" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<title>dv2ls - Utils for Devs!</title>
<title>dv2ls: 🛠 for 👨‍💻</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>

View file

@ -1,3 +1,5 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow: /paste?id=*
Disallow: /code?id=*
Disallow: /f

View file

@ -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);

View file

@ -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>

View file

@ -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">