Fetch Projects from API - needs fixing

This commit is contained in:
pavel Hamawand 2024-03-18 16:58:46 +01:00
parent 90afe80408
commit f61ef87d5e

View file

@ -7,6 +7,27 @@ const UserProjectListAdmin: React.FC = () => {
// Define the functional component UserProjectListAdmin // Define the functional component UserProjectListAdmin
const [projects, setProjects] = useState<Project[]>([]); // State management for projects const [projects, setProjects] = useState<Project[]>([]); // State management for projects
useEffect(() => {
const fetchProjects = async (): Promise<void> => {
// Define the fetchProjects async function
try {
const token = localStorage.getItem("accessToken") ?? "";
const username = getUsernameFromContext(); // Assuming you have a function to get the username from your context
const response = await api.getUserProjects(username, token); // Fetch projects from API
if (response.success) {
setProjects(response.data ?? []); // Update projects state with fetched data
} else {
console.error("Failed to fetch projects:", response.message);
}
} catch (error) {
console.error("Error fetching projects:", error);
}
};
void fetchProjects(); // Call fetchProjects when the component mounts
}, []); // Empty dependency array to run effect only once
return ( return (
<div> <div>
<h2>User Projects</h2> <h2>User Projects</h2>