-
Notifications
You must be signed in to change notification settings - Fork 0
React pr #21
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
base: master
Are you sure you want to change the base?
React pr #21
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| { | ||
| "name": "user-profile-dashboard", | ||
| "version": "1.0.0", | ||
| "private": true, | ||
| "dependencies": { | ||
| "react": "^18.2.0", | ||
| "react-dom": "^18.2.0", | ||
| "react-router-dom": "^6.14.1" | ||
| }, | ||
| "scripts": { | ||
| "start": "react-scripts start", | ||
| "build": "react-scripts build", | ||
| "test": "react-scripts test", | ||
| "eject": "react-scripts eject" | ||
| }, | ||
| "devDependencies": { | ||
| "react-scripts": "5.0.1" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import React from 'react'; | ||
| import { BrowserRouter as Router, Routes, Route } from 'react-router-dom'; | ||
| import UserProfileDashboard from './components/UserProfileDashboard'; | ||
| import { AuthProvider } from './contexts/AuthContext'; | ||
|
|
||
| function App() { | ||
| return ( | ||
| <AuthProvider> | ||
| <Router> | ||
| <Routes> | ||
| <Route path="/" element={<UserProfileDashboard />} /> | ||
| <Route path="/settings" element={<div>Settings Page</div>} /> | ||
| <Route path="/messages" element={<div>Messages Page</div>} /> | ||
| </Routes> | ||
| </Router> | ||
| </AuthProvider> | ||
| ); | ||
| } | ||
|
|
||
| export default App; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| export async function fetchUserData(userId, token) { | ||
| const response = await fetch(`https://api.example.com/users/${userId}`, { | ||
| headers: { | ||
| Authorization: `Bearer ${token}` | ||
| } | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| throw new Error('API error'); | ||
| } | ||
|
|
||
| return await response.json(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import React from 'react'; | ||
|
|
||
| export default function UserInfo({ info }) { | ||
| return ( | ||
| <section> | ||
| <h2>User Information</h2> | ||
| <p>Email: {info.email}</p> | ||
| <p>Location: {info.location}</p> | ||
| </section> | ||
| ); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import React, { useContext, useEffect, useState } from 'react'; | ||
| import { AuthContext } from '../contexts/AuthContext'; | ||
| import { fetchUserData } from '../api/userApi'; | ||
| import UserInfo from './UserInfo'; | ||
| import UserStats from './UserStats'; | ||
| import { Link } from 'react-router-dom'; | ||
|
|
||
| export default function UserProfileDashboard() { | ||
| const { userId, token } = useContext(AuthContext); | ||
| const [userData, setUserData] = useState(null); | ||
| const [loading, setLoading] = useState(true); | ||
| const [error, setError] = useState(null); | ||
|
|
||
| useEffect(() => { | ||
| if (!userId || !token) { | ||
| setError('User not authenticated'); | ||
| setLoading(false); | ||
| return; | ||
| } | ||
|
|
||
| fetchUserData(userId, token) | ||
| .then(data => { | ||
| setUserData(data); | ||
| setLoading(false); | ||
| }) | ||
| .catch(err => { | ||
| setError('Failed to fetch user data'); | ||
| setLoading(false); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hetzelfde verhaal hier als de vorige comment. Dit zou ik uit de catch gooien. Het is sowieso dubbelop dat je setLoading in zowel de .then als in de .catch gebruikt. Logischer is een .finally() hier waar je de loading state terug naar false zet. |
||
| }); | ||
| }, [userId, token]); | ||
|
|
||
| if (loading) return <div>Loading user data...</div>; | ||
| if (error) return <div>Error: {error}</div>; | ||
|
|
||
| return ( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wat ik hier mis: stel dat loading false is en error niks bijzonders teruggeeft, maar userData alsnog null of leeg is. Dan loop je het risico dat die user profile niet correct rendert. Ik zou hier nog een derde guard toevoegen: if(!userData) { }. Puur om zeker te zijn. |
||
| <div className="user-profile-dashboard"> | ||
| <h1>Welcome, {userData.name}</h1> | ||
| <UserInfo info={userData.info} /> | ||
| <UserStats stats={userData.stats} /> | ||
|
|
||
| <nav> | ||
| <ul> | ||
| <li><Link to="/settings">Settings</Link></li> | ||
| <li><Link to="/messages">Messages</Link></li> | ||
| </ul> | ||
| </nav> | ||
| </div> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import React from 'react'; | ||
|
|
||
| export default function UserStats({ stats }) { | ||
| return ( | ||
| <section> | ||
| <h2>User Stats</h2> | ||
| <ul> | ||
| <li>Posts: {stats.posts}</li> | ||
| <li>Followers: {stats.followers}</li> | ||
| <li>Following: {stats.following}</li> | ||
| </ul> | ||
| </section> | ||
| ); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import React, { createContext, useState } from 'react'; | ||
|
|
||
| export const AuthContext = createContext({ | ||
| userId: null, | ||
| token: null, | ||
| }); | ||
|
|
||
| export function AuthProvider({ children }) { | ||
| const [userId, setUserId] = useState('12345'); | ||
| const [token, setToken] = useState(null); // TODO: Provide a valid token after user login | ||
|
|
||
| return ( | ||
| <AuthContext.Provider value={{ userId, token }}> | ||
| {children} | ||
| </AuthContext.Provider> | ||
| ); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Idealiter wil je loading states niet in een .then hebben. Je wil in een .then alleen logica hebben zitten die moeten afhangen van Promise resolve state. Ik zou setLoading(false) gebruiken in een .finally() waar je alleen dingen wil doen die je sowieso al wilde doen onafhankelijk van wat de Promise teruggeeft.