Fix formatting - mainly spacing

This commit is contained in:
Parker M. 2024-11-11 20:29:56 -06:00
parent c0b5500f00
commit 918a04076f
Signed by: parker
GPG Key ID: 505ED36FC12B5D5E
8 changed files with 323 additions and 320 deletions

6
app/.prettierrc Normal file
View File

@ -0,0 +1,6 @@
{
"semi": true,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "es5"
}

View File

@ -1 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>

Before

Width:  |  Height:  |  Size: 1.5 KiB

View File

@ -1,18 +1,18 @@
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
import Login from './components/Login'
import Signup from './components/Signup'
import Dashboard from './components/Dashboard'
import Login from './components/Login';
import Signup from './components/Signup';
import Dashboard from './components/Dashboard';
function App() {
return (
<Router>
<Routes>
<Route path="/login" element={<Login />} />
<Route path="/signup" element={<Signup />} />
<Route path="/dashboard" element={<Dashboard />} />
</Routes>
</Router>
)
return (
<Router>
<Routes>
<Route path="/login" element={<Login />} />
<Route path="/signup" element={<Signup />} />
<Route path="/dashboard" element={<Dashboard />} />
</Routes>
</Router>
);
}
export default App
export default App;

View File

@ -6,124 +6,139 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faTrash } from '@fortawesome/free-solid-svg-icons';
function Dashboard() {
document.title = 'LinkLogger | Dashboard'
document.title = 'LinkLogger | Dashboard';
interface Log {
id: number;
link: string;
timestamp: string;
ip: string;
location: string;
browser: string;
os: string;
userAgent: string;
isp: string;
}
interface Log {
id: number;
link: string;
timestamp: string;
ip: string;
location: string;
browser: string;
os: string;
userAgent: string;
isp: string;
}
interface Link {
link: string;
owner: number;
redirect_link: string;
expire_date: string;
}
interface Link {
link: string;
owner: number;
redirect_link: string;
expire_date: string;
}
const [links, setLinks] = useState<Link[]>([]);
const [logs, setLogs] = useState<Log[]>([]);
const [visibleLog, setVisibleLog] = useState<string | null>(null);
const navigate = useNavigate();
const [links, setLinks] = useState<Link[]>([]);
const [logs, setLogs] = useState<Log[]>([]);
const [visibleLog, setVisibleLog] = useState<string | null>(null);
const navigate = useNavigate();
// Fetch links from API
useEffect(() => {
Axios.get('/api/links').then((res) => {
if (res.status === 200) {
setLinks(res.data);
} else {
navigate('/login');
}
}).catch(() => {
navigate('/login');
});
}, []);
// Fetch links from API
useEffect(() => {
Axios.get('/api/links')
.then((res) => {
if (res.status === 200) {
setLinks(res.data);
} else {
navigate('/login');
}
})
.catch(() => {
navigate('/login');
});
}, []);
// Fetch logs from API
useEffect(() => {
Axios.get('/api/logs').then((res) => {
if (res.status === 200) {
setLogs(res.data);
} else {
navigate('/login');
}
}).catch(() => {
navigate('/login');
});
}, []);
// Fetch logs from API
useEffect(() => {
Axios.get('/api/logs')
.then((res) => {
if (res.status === 200) {
setLogs(res.data);
} else {
navigate('/login');
}
})
.catch(() => {
navigate('/login');
});
}, []);
const toggleLogRow = (link: string) => {
setVisibleLog(visibleLog === link ? null : link);
};
const toggleLogRow = (link: string) => {
setVisibleLog(visibleLog === link ? null : link);
};
return (
<table id={styles.mainTable}>
<thead>
<tr style={{ border: '2px solid #ccc' }}>
<th>Link</th>
<th>Visits</th>
<th>Redirect</th>
<th>Expire Date</th>
</tr>
</thead>
<tbody>
{/* For every link and its logs */}
{links.map((link) => (
<React.Fragment key={link.link}>
<tr className={styles.linkTableRow}>
<td>
<button
onClick={() => toggleLogRow(link.link)}
className={styles.linkButton}
>
{link.link}
</button>
</td>
<td>
{logs.filter((log) => log.link === link.link).length || 0}
</td>
<td>{link.redirect_link}</td>
<td>{link.expire_date}</td>
</tr>
return (
<table id={styles.mainTable}>
<thead>
<tr style={{ border: '2px solid #ccc' }}>
<th>Link</th>
<th>Visits</th>
<th>Redirect</th>
<th>Expire Date</th>
</tr>
</thead>
<tbody>
{/* For every link and its logs */}
{links.map((link) => (
<React.Fragment key={link.link}>
<tr className={styles.linkTableRow}>
{/* Conditionally render logs for this link */}
{visibleLog === link.link && (
<tr className={styles.logTableRow}>
<td colSpan={6}>
<table>
<thead>
<tr>
<th>ID</th>
<th>Timestamp</th>
<th>IP</th>
<th>Location</th>
<th colSpan={2}>ISP</th>
</tr>
</thead>
<tbody>
{/* Render logs only if visibleLog matches the link */}
{logs
.filter((log) => log.link === link.link)
.map((log, index, filteredLogs) => (
<tr key={log.id}>
<td>{filteredLogs.length - index}</td>
<td>{log.timestamp}</td>
<td>{log.ip}</td>
<td>{log.location}</td>
<td>{log.isp}</td>
<td>
<button onClick={() => toggleLogRow(link.link)} className={styles.linkButton}>{link.link}</button>
<FontAwesomeIcon
icon={faTrash}
className={styles.trashBin}
/>
</td>
<td>{logs.filter((log) => log.link === link.link).length || 0}</td>
<td>{link.redirect_link}</td>
<td>{link.expire_date}</td>
</tr>
{/* Conditionally render logs for this link */}
{visibleLog === link.link && (
<tr className={styles.logTableRow}>
<td colSpan={6}>
<table>
<thead>
<tr>
<th>ID</th>
<th>Timestamp</th>
<th>IP</th>
<th>Location</th>
<th colSpan={2}>ISP</th>
</tr>
</thead>
<tbody>
{/* Render logs only if visibleLog matches the link */}
{logs
.filter((log) => log.link === link.link)
.map((log, index, filteredLogs) => (
<tr key={log.id}>
<td>{filteredLogs.length - index}</td>
<td>{log.timestamp}</td>
<td>{log.ip}</td>
<td>{log.location}</td>
<td>{log.isp}</td>
<td><FontAwesomeIcon icon={faTrash} className={styles.trashBin}/></td>
</tr>
))}
</tbody>
</table>
</td>
</tr>
)}
</React.Fragment>
))}
</tbody>
</table>
)
</tr>
))}
</tbody>
</table>
</td>
</tr>
)}
</React.Fragment>
))}
</tbody>
</table>
);
}
export default Dashboard;
export default Dashboard;

View File

@ -5,82 +5,87 @@ import { useNavigate } from 'react-router-dom';
import axios from 'axios';
function Login() {
document.title = 'LinkLogger | Login'
document.title = 'LinkLogger | Login';
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [isSubmitting, setIsSubmitting] = useState(false);
const [error, setError] = useState<string | null>(null);
const navigate = useNavigate();
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [isSubmitting, setIsSubmitting] = useState(false);
const [error, setError] = useState<string | null>(null);
const navigate = useNavigate();
const handleSubmit = async (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
setIsSubmitting(true);
try {
const res = await axios.post(
'/api/auth/token',
new URLSearchParams({
username: username,
password: password,
}),
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
}
);
if (res.status === 200) {
navigate('/dashboard');
}
} catch (error: unknown) {
if (axios.isAxiosError(error)) {
const customErrorMessage = error.response?.data?.detail || null;
setPassword('');
setError(customErrorMessage || 'An error occurred. Please try again.');
} else {
setPassword('');
setError('Unknown error. Please try again.');
}
} finally {
setIsSubmitting(false);
const handleSubmit = async (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
setIsSubmitting(true);
try {
const res = await axios.post(
'/api/auth/token',
new URLSearchParams({
username: username,
password: password,
}),
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
}
};
);
return (
<div id={styles.container}>
<p id={styles.loginText}>Log In</p>
<p id={styles.error} className={error ? 'visible' : 'hidden'}>
{error}
</p>
<div>
<header>
<hr></hr>
<form onSubmit={handleSubmit}>
<input
type="text"
placeholder="username"
value={username}
onChange={(e) => setUsername(e.target.value)}
required
/>
<input
type="password"
placeholder="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
required
/>
<button type="submit" disabled={isSubmitting}>
{isSubmitting ? 'Submitting...' : 'Submit'}
</button>
</form>
<hr></hr>
<p id={styles.bottomText}>Don't have an account? <Link to="/signup" className={styles.link}>Create one now</Link></p>
</header>
</div>
</div>
);
if (res.status === 200) {
navigate('/dashboard');
}
} catch (error: unknown) {
if (axios.isAxiosError(error)) {
const customErrorMessage = error.response?.data?.detail || null;
setPassword('');
setError(customErrorMessage || 'An error occurred. Please try again.');
} else {
setPassword('');
setError('Unknown error. Please try again.');
}
} finally {
setIsSubmitting(false);
}
};
return (
<div id={styles.container}>
<p id={styles.loginText}>Log In</p>
<p id={styles.error} className={error ? 'visible' : 'hidden'}>
{error}
</p>
<div>
<header>
<hr></hr>
<form onSubmit={handleSubmit}>
<input
type="text"
placeholder="username"
value={username}
onChange={(e) => setUsername(e.target.value)}
required
/>
<input
type="password"
placeholder="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
required
/>
<button type="submit" disabled={isSubmitting}>
{isSubmitting ? 'Submitting...' : 'Submit'}
</button>
</form>
<hr></hr>
<p id={styles.bottomText}>
Don't have an account?{' '}
<Link to="/signup" className={styles.link}>
Create one now
</Link>
</p>
</header>
</div>
</div>
);
}
export default Login;
export default Login;

View File

@ -5,104 +5,109 @@ import { useNavigate } from 'react-router-dom';
import axios from 'axios';
function Signup() {
document.title = 'LinkLogger | Signup'
document.title = 'LinkLogger | Signup';
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [passwordConfirm, setPasswordConfirm] = useState('');
const [isSubmitting, setIsSubmitting] = useState(false);
const [error, setError] = useState<string | null>(null);
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [passwordConfirm, setPasswordConfirm] = useState('');
const [isSubmitting, setIsSubmitting] = useState(false);
const [error, setError] = useState<string | null>(null);
const navigate = useNavigate();
const navigate = useNavigate();
const handleSubmit = async (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
setIsSubmitting(true);
const handleSubmit = async (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
setIsSubmitting(true);
if (password !== passwordConfirm) {
setPassword('');
setPasswordConfirm('');
return setError('Passwords do not match.');
if (password !== passwordConfirm) {
setPassword('');
setPasswordConfirm('');
return setError('Passwords do not match.');
}
try {
const res = await axios.post(
'/api/users/register',
new URLSearchParams({
username: username,
password: password,
}),
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
}
);
try {
const res = await axios.post(
'/api/users/register',
new URLSearchParams({
username: username,
password: password,
}),
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
}
);
if (res.status === 200) {
navigate('/login');
}
} catch (error: unknown) {
if (axios.isAxiosError(error)) {
const customErrorMessage = error.response?.data?.detail || null;
setUsername('');
setPassword('');
setPasswordConfirm('');
setError(customErrorMessage || 'An error occurred. Please try again.');
} else {
setUsername('');
setPassword('');
setPasswordConfirm('');
setError('Unknown error. Please try again.');
}
} finally {
setIsSubmitting(false);
}
};
if (res.status === 200) {
navigate('/login');
}
} catch (error: unknown) {
if (axios.isAxiosError(error)) {
const customErrorMessage = error.response?.data?.detail || null;
setUsername('');
setPassword('');
setPasswordConfirm('');
setError(customErrorMessage || 'An error occurred. Please try again.');
} else {
setUsername('');
setPassword('');
setPasswordConfirm('');
setError('Unknown error. Please try again.');
}
} finally {
setIsSubmitting(false);
}
};
return (
<div id={styles.container}>
<p id={styles.signupText}>Sign up</p>
<p id={styles.error} className={error ? 'visible' : 'hidden'}>
{error}
</p>
<div>
<header>
<hr></hr>
<form onSubmit={handleSubmit}>
<input
type="text"
placeholder="username"
value={username}
onChange={(e) => setUsername(e.target.value)}
required
/>
<input
type="password"
placeholder="password"
value={password}
minLength={8}
onChange={(e) => setPassword(e.target.value)}
required
/>
<input
type="password"
placeholder="confirm password"
value={passwordConfirm}
minLength={8}
onChange={(e) => setPasswordConfirm(e.target.value)}
required
/>
<button type="submit" disabled={isSubmitting}>
{isSubmitting ? 'Submitting...' : 'Submit'}
</button>
</form>
<hr></hr>
<p id={styles.bottomText}>Already have an account? <Link to="/login" className={styles.link}>Log in here.</Link></p>
</header>
</div>
</div>
);
return (
<div id={styles.container}>
<p id={styles.signupText}>Sign up</p>
<p id={styles.error} className={error ? 'visible' : 'hidden'}>
{error}
</p>
<div>
<header>
<hr></hr>
<form onSubmit={handleSubmit}>
<input
type="text"
placeholder="username"
value={username}
onChange={(e) => setUsername(e.target.value)}
required
/>
<input
type="password"
placeholder="password"
value={password}
minLength={8}
onChange={(e) => setPassword(e.target.value)}
required
/>
<input
type="password"
placeholder="confirm password"
value={passwordConfirm}
minLength={8}
onChange={(e) => setPasswordConfirm(e.target.value)}
required
/>
<button type="submit" disabled={isSubmitting}>
{isSubmitting ? 'Submitting...' : 'Submit'}
</button>
</form>
<hr></hr>
<p id={styles.bottomText}>
Already have an account?{' '}
<Link to="/login" className={styles.link}>
Log in here.
</Link>
</p>
</header>
</div>
</div>
);
}
export default Signup;
export default Signup;

View File

@ -1,27 +0,0 @@
/**
* Accept an API endpoint, method, and body to send to the API.
* - If successful, return the response
* - If not, return false
* @param {*} endpoint API endpoint
* @param {*} method String (GET, POST, PUT, DELETE)
* @param {*} body Data to send to the API
* @returns response.json or false
*/
async function accessAPI(endpoint, method, body) {
let response = await fetch(`http://127.0.0.1:5252/api${endpoint}`, {
method: method,
credentials: 'include',
body: body,
});
if (response.ok) {
let data = await response.json();
data = await data;
return data;
}
return false;
}
export { accessAPI };

View File

@ -1,9 +1,9 @@
import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import App from './App.tsx'
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import App from './App.tsx';
createRoot(document.getElementById('root')!).render(
<StrictMode>
<App />
</StrictMode>,
)
</StrictMode>
);