Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions package.json
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"
}
}
20 changes: 20 additions & 0 deletions src/App.jsx
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;
13 changes: 13 additions & 0 deletions src/api/userApi.js
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();
}
Binary file added src/components/.DS_Store
Binary file not shown.
11 changes: 11 additions & 0 deletions src/components/UserInfo.jsx
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>
);
}
49 changes: 49 additions & 0 deletions src/components/UserProfileDashboard.jsx
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);
Copy link
Copy Markdown

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.

})
.catch(err => {
setError('Failed to fetch user data');
setLoading(false);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 (
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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>
);
}
14 changes: 14 additions & 0 deletions src/components/UserStats.jsx
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>
);
}
17 changes: 17 additions & 0 deletions src/contexts/AuthContext.jsx
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>
);
}