From 04cc3869c2844bb82ac6975ee218141104385c35 Mon Sep 17 00:00:00 2001 From: Parker Date: Fri, 15 Nov 2024 00:30:43 -0600 Subject: Extra route + Much more UI --- app/src/components/CreateLink.tsx | 100 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 app/src/components/CreateLink.tsx (limited to 'app/src/components/CreateLink.tsx') diff --git a/app/src/components/CreateLink.tsx b/app/src/components/CreateLink.tsx new file mode 100644 index 0000000..a6456f1 --- /dev/null +++ b/app/src/components/CreateLink.tsx @@ -0,0 +1,100 @@ +import { useState, FormEvent, useEffect } from 'react'; +import createStyles from '../styles/Create.module.css'; +import styles from '../styles/Auth.module.css'; +import { Link } from 'react-router-dom'; +import axios from 'axios'; +import Navbar from './Navbar'; +import { useNavigate } from 'react-router-dom'; + +function CreateLink() { + document.title = 'LinkLogger | Create Short Link'; + + const [link, setLink] = useState(''); + const [url, setURL] = useState(''); + const [isSubmitting, setIsSubmitting] = useState(false); + const [isCopied, setIsCopied] = useState(false); + const [error, setError] = useState(null); + const navigate = useNavigate(); + + // Get /api/users/me to make sure the user is logged in, and + // to get the username for rendering on screen + useEffect(() => { + axios + .get('/api/users/me') + .then((res) => { + if (res.status != 200) { + navigate('/login'); + } + }) + .catch(() => { + navigate('/login'); + }); + }, []); + + const handleSubmit = async (e: FormEvent) => { + e.preventDefault(); + setIsSubmitting(true); + try { + const res = await axios.post('/api/links', { url }); + if (res.status === 200) { + setLink(res.data.link); + } + } catch (error) { + setError('STRANGE'); + } + }; + + const copyLink = () => { + navigator.clipboard.writeText(`${window.location.origin}/c/${link}`); + setIsCopied(true); + // Wait 5 seconds, then set isCopied back to false + setTimeout(() => { + setIsCopied(false); + }, 5000); + }; + + return ( + <> + +
+

Create a new short link by entering the long URL below

+

+ {error} +

+
+
+ setURL(e.target.value)} + required + /> + {link.length === 0 ? ( + + ) : ( + + )} +
+
+

+ + Click here + {' '} + to visit your dashboard. +

+
+ + ); +} + +export default CreateLink; -- cgit v1.2.3-70-g09d2