Skip to content

Commit

Permalink
Merge pull request #67 from hytech-racing/deletion
Browse files Browse the repository at this point in the history
deletion
  • Loading branch information
SreeDan authored Dec 4, 2024
2 parents 1f6acf7 + e28aeef commit f4c48df
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 19 deletions.
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions src/components/DataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ export default function DataTable({
<Table.Td>{getFileNameWithoutExtension(file.mcap_files[0].file_name)}</Table.Td>
<Table.Td>{file.date}</Table.Td>
<Table.Td>{file.location}</Table.Td>

{/* Change back to notes once notes field is implemented in the server */}
<Table.Td>{file.car_model}</Table.Td>
<Table.Td>{file.notes}</Table.Td>
</Table.Tr>
))
);
Expand Down
78 changes: 63 additions & 15 deletions src/components/PreviewCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Table,
ScrollArea,
TextInput,
Notification,
} from "@mantine/core";
import {
IconDownload,
Expand All @@ -23,6 +24,39 @@ interface PreviewCardProps {
}

function PreviewCard({ selectedData }: PreviewCardProps) {
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const [success, setSuccess] = useState<string | null>(null);

const handleDelete = async () => {
setLoading(true)
setError(null);
setSuccess(null)
try {
const response = await fetch(`${import.meta.env.VITE_API_URL}/mcaps/${selectedData?.id}`, {
method: 'DELETE',
});

if (!response.ok) {
if (response.status === 503) {
const errorMsg = await response.text();
setError(`Failed to delete: ${errorMsg} \nTry again in a few minutes!`);
} else {
const errorMsg = await response.text();
setError(`Failed to delete: ${errorMsg}`);
}
} else {
const result = await response.json();
setSuccess('File deleted successfully!');
console.log('Delete successful:', result);
}
} catch (error) {
console.error('Error sending Delete request:', error);
setError('An error occurred during file deletion.');
}
setLoading(false)
}

const formatDate = (dateString: string) => {
const date = new Date(dateString);
return date.toLocaleDateString("en-US", {
Expand Down Expand Up @@ -101,23 +135,37 @@ function PreviewCard({ selectedData }: PreviewCardProps) {
bottom: 0,
left: 0,
padding: 20,
gap: "10px",
gap: "8px",
}}
>
{selectedData.mcap_files.map((item) => (
<DownloadButton
buttonText="MCAP"
fileName={item.file_name}
signedUrl={item.signed_url ?? null}
/>
))}
{selectedData.mat_files.map((item) => (
<DownloadButton
buttonText="MAT"
fileName={item.file_name}
signedUrl={item.signed_url}
/>
))}
<div>
{selectedData.mcap_files.map((item) => (
<DownloadButton
buttonText="MCAP"
fileName={item.file_name}
signedUrl={item.signed_url ?? null}
/>
))}
{selectedData.mat_files.map((item) => (
<DownloadButton
buttonText="MAT"
fileName={item.file_name}
signedUrl={item.signed_url}
/>
))}
<Button loading={loading} loaderProps={{ type: 'dots' }} size="compact-md" color="red" onClick={handleDelete}>Delete</Button>
{success && (
<Notification color="green" onClose={() => setSuccess(null)} style={{ marginTop: 10 }}>
{success}
</Notification>
)}
{error && (
<Notification color="red" onClose={() => setError(null)} style={{ marginTop: 10 }}>
{error}
</Notification>
)}
</div>

</div>
</>
) : (
Expand Down

0 comments on commit f4c48df

Please sign in to comment.