Functionality for log deletion
This commit is contained in:
parent
8d3edb4ffe
commit
2ed699d2cb
@ -1,5 +1,5 @@
|
|||||||
import React, { useState, useEffect } from 'react';
|
import React, { useState, useEffect } from 'react';
|
||||||
import Axios from 'axios';
|
import axios from 'axios';
|
||||||
import styles from '../styles/Dashboard.module.css';
|
import styles from '../styles/Dashboard.module.css';
|
||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from 'react-router-dom';
|
||||||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
||||||
@ -34,7 +34,8 @@ function Dashboard() {
|
|||||||
|
|
||||||
// Fetch links from API
|
// Fetch links from API
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
Axios.get('/api/links')
|
axios
|
||||||
|
.get('/api/links')
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
if (res.status === 200) {
|
if (res.status === 200) {
|
||||||
setLinks(res.data);
|
setLinks(res.data);
|
||||||
@ -42,6 +43,9 @@ function Dashboard() {
|
|||||||
navigate('/login');
|
navigate('/login');
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Catch 404 error = user has no links
|
||||||
|
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
navigate('/login');
|
navigate('/login');
|
||||||
});
|
});
|
||||||
@ -49,7 +53,8 @@ function Dashboard() {
|
|||||||
|
|
||||||
// Fetch logs from API
|
// Fetch logs from API
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
Axios.get('/api/logs')
|
axios
|
||||||
|
.get('/api/logs')
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
if (res.status === 200) {
|
if (res.status === 200) {
|
||||||
setLogs(res.data);
|
setLogs(res.data);
|
||||||
@ -57,15 +62,52 @@ function Dashboard() {
|
|||||||
navigate('/login');
|
navigate('/login');
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Catch 404 error = user has no logs
|
||||||
|
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
navigate('/login');
|
navigate('/login');
|
||||||
});
|
});
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Display or hide logs for a specific link
|
||||||
|
* @param link The link to toggle logs for
|
||||||
|
*/
|
||||||
const toggleLogRow = (link: string) => {
|
const toggleLogRow = (link: string) => {
|
||||||
setVisibleLog(visibleLog === link ? null : link);
|
setVisibleLog(visibleLog === link ? null : link);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a specific log
|
||||||
|
* Gets the log ID from the SVG element's ID attribute
|
||||||
|
* @param e The event object from the click
|
||||||
|
* @returns void - updates state (setLogs) if successful
|
||||||
|
*/
|
||||||
|
const deleteLog = (e: React.MouseEvent<SVGSVGElement>) => {
|
||||||
|
const confirmDelete = confirm('Are you sure you want to delete this log?');
|
||||||
|
if (!confirmDelete) return;
|
||||||
|
|
||||||
|
const id = parseInt(e.currentTarget.id);
|
||||||
|
axios
|
||||||
|
.delete(`/api/logs/${id}`)
|
||||||
|
.then((res) => {
|
||||||
|
if (res.status === 200) {
|
||||||
|
setLogs(logs.filter((log) => log.id !== id));
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch((error: unknown) => {
|
||||||
|
if (axios.isAxiosError(error)) {
|
||||||
|
// Return to login if we are unauthorized for some reason
|
||||||
|
if (error.response?.status === 401) {
|
||||||
|
navigate('/login');
|
||||||
|
} else {
|
||||||
|
alert('Failed to delete log. Please try again.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<table id={styles.mainTable}>
|
<table id={styles.mainTable}>
|
||||||
<thead>
|
<thead>
|
||||||
@ -125,6 +167,8 @@ function Dashboard() {
|
|||||||
<FontAwesomeIcon
|
<FontAwesomeIcon
|
||||||
icon={faTrash}
|
icon={faTrash}
|
||||||
className={styles.trashBin}
|
className={styles.trashBin}
|
||||||
|
id={log.id.toString()}
|
||||||
|
onClick={deleteLog}
|
||||||
/>
|
/>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user