Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deletion #67

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading