Fetch Projects from API - needs fixing
This commit is contained in:
		
							parent
							
								
									90afe80408
								
							
						
					
					
						commit
						f61ef87d5e
					
				
					 1 changed files with 21 additions and 0 deletions
				
			
		|  | @ -7,6 +7,27 @@ const UserProjectListAdmin: React.FC = () => { | |||
|   // Define the functional component UserProjectListAdmin
 | ||||
|   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 ( | ||||
|     <div> | ||||
|       <h2>User Projects</h2> | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 pavel Hamawand
						pavel Hamawand