Step-by-step analysis of the problem:
- The API request is going to the catch block with an error: This suggests that there's an issue with the API request itself, such as a network error, invalid headers, or an issue with the request URL.
- The error is not being logged or displayed: Although you're logging the error to the console, you're not providing any additional information about the error. This makes it difficult to diagnose the issue.
- The request URL and headers seem unusual: The request URL is using a non-standard port number (9007) and the headers include custom IDs generated using UUID.
Fixed solution:
import { Box, Typography } from "@mui/material";
import { DataGrid, GridColDef } from "@mui/x-data-grid";
import axios from "axios";
import { useEffect, useState } from "react";
import { v4 as uuidv4 } from "uuid";
const SampleListingDataTable = () => {
const [rows, setRows] = useState<any[]>([]);
const [columns, setColumns] = useState<GridColDef[]>([]);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const fetchData = async () => {
setLoading(true);
setError(null);
const headers = {
"X-Message-Id": uuidv4().replace(/-/g, ""),
"X-Correlation-Id": uuidv4().replace(/-/g, ""),
};
try {
const response = await axios.get(
"https://testing-backend-sample-linc.medgenome.com:9007/v1/sampleManagement/sampleDetails",
{
headers,
// Add this to verify the SSL certificate
httpsAgent: new axios.Agent({
rejectUnauthorized: false,
}),
}
);
console.log("Raw response data:", response.data);
const data = response.data?.data?.samples || [];
if (!Array.isArray(data)) {
throw new Error("Invalid data format");
}
setRows(data);
if (data.length > 0) {
const firstRow = data[0];
const dynamicColumns: GridColDef[] = Object.keys(firstRow).map(
(key) => ({
field: key,
headerName: key
.replace(/([A-Z])/g, " $1")
.replace(/^./, (str) => str.toUpperCase()),
flex: 1,
minWidth: 150,
})
);
setColumns(dynamicColumns);
}
} catch (err) {
if (axios.isAxiosError(err)) {
console.error("API error:", err.message, err.response?.status, err.response?.data);
setError(`Failed to load sample data. Status code: ${err.response?.status}. Please try again later.`);
} else {
console.error("API error:", err);
setError("Failed to load sample data. Please try again later.");
}
} finally {
setLoading(false);
}
};
useEffect(() => {
fetchData();
}, []);
return (
<Box sx={{ height: 600, width: "100%" }}>
{error ? (
<Typography color="error" variant="body1" sx={{ p: 2 }}>
{error}
</Typography>
) : (
<DataGrid
rows={rows}
columns={columns}
loading={loading}
getRowId={(row) => row.sampleId || row.id || row._id || uuidv4()}
disableRowSelectionOnClick
checkboxSelection
pagination
pageSizeOptions={[10, 25, 50]}
/>
)}
</Box>
);
};
export default SampleListingDataTable;