From 8985eecfeac5887f59fdf36d9b3f2584692ff5ed Mon Sep 17 00:00:00 2001 From: Parker Date: Sun, 10 Nov 2024 23:11:17 -0600 Subject: Add log routes and update dashboard --- app/src/components/Dashboard.tsx | 142 ++++++++++++++++++++++++++++++--------- 1 file changed, 111 insertions(+), 31 deletions(-) (limited to 'app/src/components/Dashboard.tsx') diff --git a/app/src/components/Dashboard.tsx b/app/src/components/Dashboard.tsx index bcab092..373a07f 100644 --- a/app/src/components/Dashboard.tsx +++ b/app/src/components/Dashboard.tsx @@ -1,46 +1,126 @@ -import { useState, useEffect } from 'react'; +import React, { useState, useEffect } from 'react'; import Axios from 'axios'; import styles from '../styles/Dashboard.module.css'; -// import { accessAPI } from '../helpers/api'; - +import { useNavigate } from 'react-router-dom'; +import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; +import { faTrash } from '@fortawesome/free-solid-svg-icons'; function Dashboard() { - // Get the links from the API - const [links, setLinks] = useState([]); + 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; + } + + const [links, setLinks] = useState([]); + const [logs, setLogs] = useState([]); + const [visibleLog, setVisibleLog] = useState(null); + const navigate = useNavigate(); + + // Fetch links from API useEffect(() => { - Axios.get('/api/links') - .then((res) => { + Axios.get('/api/links').then((res) => { + if (res.status === 200) { setLinks(res.data); - }) - .catch((err) => { - console.log(err); - }); + } 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); + }; + return ( -
- - - - - - - - - - - {/* {links.map((link: any) => ( - - - - +
LinkVisitsRedirectExpire Date
{link.url}{link.visits}{link.redirect}
+ + + + + + + + + + {/* For every link and its logs */} + {links.map((link) => ( + + + + + - ))} */} - -
LinkVisitsRedirectExpire Date
+ + {logs.filter((log) => log.link === link.link).length || 0}{link.redirect_link} {link.expire_date}
-
+ + {/* Conditionally render logs for this link */} + {visibleLog === link.link && ( + + + + + + + + + + + + + + {/* Render logs only if visibleLog matches the link */} + {logs + .filter((log) => log.link === link.link) + .map((log, index, filteredLogs) => ( + + + + + + + + + ))} + +
IDTimestampIPLocationISP
{filteredLogs.length - index}{log.timestamp}{log.ip}{log.location}{log.isp}
+ + + )} + + ))} + + ) } -- cgit v1.2.3-70-g09d2