import React, { useState, useEffect } from "react"; import axios from "axios"; import {…
- User
import React, { useState, useEffect } from "react"; import axios from "axios"; import { useNavigate, useParams } from "react-router-dom"; import Sidebar from "../../Sidebar"; import Header from "../../Header";
function ProjectDetails() { const [sidebarOpen, setSidebarOpen] = useState(false); const token = localStorage.getItem("token"); const [user, setUser] = useState(null); const [selectedRole, setSelectedRole] = useState(""); const { projectId } = useParams(); const navigate = useNavigate(); const [loading, setLoading] = useState(true); const [error, setError] = useState(null);
useEffect(() => { const fetchUserDetails = async () => { try { const response = await axios.get(
http://localhost:5000/apiTender/projects/${projectId}, { headers: { "Content-Type": "application/json", auth: token, }, } ); setUser(response.data.User[0]); setSelectedRole(response.data.User[0].userRole); setLoading(false); } catch (error) { console.error(error); setError("Failed to fetch project details"); setLoading(false); } };fetchUserDetails();}, [projectId, token]);
const handleDelete = async () => { try { // Make API call to delete the user await axios.delete(
http://localhost:5000/apiTender/projects/${projectId}, { headers: { "Content-Type": "application/json", auth: token, }, });alert("Deleted"); navigate("/dashboard/projects"); } catch (error) { console.error(error); setError("Failed to delete the project"); }};
const handleRoleChange = (event) => { setSelectedRole(event.target.value); };
const handleUpdateRole = async () => { try { // Make API call to update the user role await axios.put(
http://localhost:5000/apiTender/projects/${projectId}, { projectId: projectId, userRole: selectedRole, }, { headers: { "Content-Type": "application/json", auth: token, }, } );alert("Role updated successfully"); } catch (error) { console.error(error); setError("Failed to update the role"); }};
if (loading) { return Loading...; }
if (error) { return Error: {error} {projectId}; }
return ( {/* Sidebar */}
{/* Content area */} <div className="relative flex flex-col flex-1 overflow-y-auto overflow-x-hidden"> <main> {/* Site header */} <Header sidebarOpen={sidebarOpen} setSidebarOpen={setSidebarOpen} /> <div className="px-4 sm:px-6 lg:px-8 py-8 w-full max-w-9xl mx-auto"> {/* Dashboard actions */} <div className="container mx-auto py-8 px-4"> <div className="max-w-lg mx-auto bg-white shadow-lg rounded-lg p-6 flex flex-col"> <div> <h1 className="text-3xl font-bold mb-6 text-gray-800"> Project Details </h1> <p className="mb-2 text-gray-700"> <strong className="text-gray-900">PNR:</strong>{" "} <span className="ml-2">{user.pnr}</span> </p> <p className="mb-2 text-gray-700"> <strong className="text-gray-900">Company Name:</strong>{" "} <span className="ml-2">{user.companyname}</span> </p> <p className="mb-2 text-gray-700"> <strong className="text-gray-900">User Role:</strong>{" "} <span className="ml-2">{user.userRole}</span> </p> <p className="mb-2 text-gray-700"> <strong className="text-gray-900">Detail:</strong>{" "} <span className="ml-2">{user.detail}</span> </p> <p className="mb-2 text-gray-700"> <strong className="text-gray-900">Value:</strong>{" "} <span className="ml-2">{user.value}</span> </p> <p className="mb-2 text-gray-700"> <strong className="text-gray-900">Status:</strong>{" "} <span className="ml-2">{user.status}</span> </p> <p className="mb-2 text-gray-700"> <strong className="text-gray-900">Country:</strong>{" "} <span className="ml-2">{user.country}</span> </p> <p className="mb-2 text-gray-700"> <strong className="text-gray-900">State:</strong>{" "} <span className="ml-2">{user.state}</span> </p> <p className="mb-2 text-gray-700"> <strong className="text-gray-900">City:</strong>{" "} <span className="ml-2">{user.city}</span> </p> </div> <div className="mt-4"> <label className="block text-gray-700 text-sm font-bold mb-2" htmlFor="userRole" > Update Role: </label> <select className="w-1/4 px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-red-500 mb-2" id="userRole" name="userRole" value={selectedRole} onChange={handleRoleChange} > <option value="hr">HR</option> <option value="admin">Admin</option> <option value="employee">Employee</option> <option value="user">User</option> </select> <div className="mt-4 flex justify-between"> <button className="bg-[#182235] hover:bg-[#111a2b] text-white font-bold py-2 px-4 rounded" onClick={handleUpdateRole} > Update Role </button> <button className="bg-[#182235] hover:bg-[#111a2b] text-white font-bold py-2 px-4 rounded" onClick={handleDelete} > Delete </button> </div> </div> </div> </div> </div> </main> </div> </div> - Assistant
The error message "Failed to fetch project details" indicates that there was an issue in fetching the project details from the API. This could be due to various reasons such as network connectivity issues, API not responding, incorrect API endpoint, or incorrect authentication.
Here are some troubleshooting steps you can try:
-
Check the API endpoint: Make sure that the API endpoint
http://localhost:5000/apiTender/projects/${projectId}is correct. You can test the API endpoint using tools like Postman or cURL to see if it returns the expected data. -
Verify authentication: Ensure that the authentication token (
token) is valid and passed correctly in the headers of the API request. You can check the token value and confirm that it is being set correctly in the headers. -
Check network connectivity: Verify that you have a stable network connection and can access the API endpoint. Try accessing the API endpoint directly in your browser or using a tool like cURL to check if it returns the expected data.
-