diff options
Diffstat (limited to 'app/src/components')
-rw-r--r-- | app/src/components/CreateLink.tsx | 100 | ||||
-rw-r--r-- | app/src/components/Dashboard.tsx | 71 | ||||
-rw-r--r-- | app/src/components/Signup.tsx | 4 |
3 files changed, 72 insertions, 103 deletions
diff --git a/app/src/components/CreateLink.tsx b/app/src/components/CreateLink.tsx deleted file mode 100644 index a6456f1..0000000 --- a/app/src/components/CreateLink.tsx +++ /dev/null @@ -1,100 +0,0 @@ -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<string | null>(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<HTMLFormElement>) => { - 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 ( - <> - <Navbar /> - <div className={styles.container}> - <h1>Create a new short link by entering the long URL below</h1> - <p className={error ? styles.errorVisible : styles.errorHidden}> - {error} - </p> - <hr></hr> - <form onSubmit={handleSubmit}> - <input - className={createStyles.createInput} - type="text" - placeholder="Full URL" - value={url} - onChange={(e) => setURL(e.target.value)} - required - /> - {link.length === 0 ? ( - <button type="submit" disabled={isSubmitting}> - {isSubmitting ? 'Creating...' : 'Create'} - </button> - ) : ( - <button type="button" onClick={copyLink}> - {isCopied ? ( - <em>Copied!</em> - ) : ( - `Click to copy: ${window.location.origin}/c/${link}` - )} - </button> - )} - </form> - <hr></hr> - <p className={styles.footnote}> - <Link to="/dashboard" className={styles.footnoteLink}> - Click here - </Link>{' '} - to visit your dashboard. - </p> - </div> - </> - ); -} - -export default CreateLink; diff --git a/app/src/components/Dashboard.tsx b/app/src/components/Dashboard.tsx index d699efb..913b566 100644 --- a/app/src/components/Dashboard.tsx +++ b/app/src/components/Dashboard.tsx @@ -1,4 +1,4 @@ -import React, { useState, useEffect } from 'react'; +import React, { useState, useEffect, FormEvent } from 'react'; import axios from 'axios'; import styles from '../styles/Dashboard.module.css'; import { useNavigate, Link } from 'react-router-dom'; @@ -28,6 +28,14 @@ function Dashboard() { expire_date: string; } + // Link creation states + const [link, setLink] = useState(''); + const [url, setURL] = useState(''); + const [isSubmitting, setIsSubmitting] = useState(false); + const [isCopied, setIsCopied] = useState(false); + const [error, setError] = useState<string | null>(null); + + // Log and link states const [links, setLinks] = useState<Link[]>([]); const [logs, setLogs] = useState<Log[]>([]); const [visibleLog, setVisibleLog] = useState<string | null>(null); @@ -35,6 +43,37 @@ function Dashboard() { const [loadingLogs, setLoadingLogs] = useState<boolean>(true); // Track loading state for logs const navigate = useNavigate(); + // Handle form submission to create a new link + const handleSubmit = async (e: FormEvent<HTMLFormElement>) => { + e.preventDefault(); + setIsSubmitting(true); + try { + const res = await axios.post('/api/links', { url }); + if (res.status === 200) { + setLink(res.data.link); + } + } catch (error: unknown) { + if (axios.isAxiosError(error)) { + const customErrorMessage = error.response?.data?.error || null; + setError(customErrorMessage || 'An error occurred. Please try again.'); + setIsSubmitting(false); + } else { + setError('Unknown error. Please try again.'); + setIsSubmitting(false); + } + } + }; + + // Copy the link to the clipboard + 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); + }; + // Fetch links from API useEffect(() => { axios @@ -130,6 +169,36 @@ function Dashboard() { return ( <> <Navbar /> + + <div className={styles.createContainer}> + <h1>Create a new short link by entering the long URL below</h1> + <form onSubmit={handleSubmit}> + <input + type="text" + placeholder="Full URL" + value={url} + onChange={(e) => setURL(e.target.value)} + required + /> + {link.length === 0 ? ( + <button type="submit" disabled={isSubmitting}> + {isSubmitting ? 'Creating...' : 'Create'} + </button> + ) : ( + <button type="button" onClick={copyLink}> + {isCopied ? ( + <em>Copied!</em> + ) : ( + `Click to copy: ${window.location.origin}/c/${link}` + )} + </button> + )} + </form> + <p className={error ? styles.errorVisible : styles.errorHidden}> + {error} + </p> + </div> + {/* Show loading spinner if either links or logs are still loading */} {loadingLinks || loadingLogs ? ( <LoadingSpinner /> diff --git a/app/src/components/Signup.tsx b/app/src/components/Signup.tsx index 0822c46..300f189 100644 --- a/app/src/components/Signup.tsx +++ b/app/src/components/Signup.tsx @@ -66,9 +66,9 @@ function Signup() { <Navbar /> <div className={styles.container}> <h1>Create Account</h1> - <h2 className={error ? styles.errorVisible : styles.errorHidden}> + <p className={error ? styles.errorVisible : styles.errorHidden}> {error} - </h2> + </p> <hr></hr> <form onSubmit={handleSubmit}> <input |