Skip to content

Completed Code Along #7

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
21 changes: 17 additions & 4 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
import { useState, useEffect } from "react"
import { Outlet } from "react-router-dom"
import NavBar from "./components/NavBar"

function App() {
const [users, setUsers] = useState([])

useEffect(() => {
fetch("http://localhost:4000/users")
.then((r) => r.json())
.then((data) => setUsers(data))
.catch((error) => console.error(error))
}, [])

return (
<>
<header>

<NavBar />
</header>
<Outlet context={users} />
</>
);
};
)
}

export default App;
export default App
27 changes: 16 additions & 11 deletions src/components/UserCard.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { Link } from "react-router-dom";
import { Link, useOutletContext } from "react-router-dom"

function UserCard({user}) {
return (
<article>
<h2>{user.name}</h2>
<Link to={`/profile/${user.id}`}>View profile</Link>
</article>
);
};

export default UserCard;
function UserCard({ user }) {
const users = useOutletContext()
console.log(users)

return (
<article>
<h2>{user.name}</h2>
<p>
<Link to={`/profile/${user.id}`}>View profile</Link>
</p>
</article>
)
}

export default UserCard
20 changes: 10 additions & 10 deletions src/pages/About.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
function About() {
return (
<>
<main>
<h1>This is my about component!</h1>
</main>
</>
);
};
export default About;
return (
<>
<main>
<h1>This is my about component!</h1>
</main>
</>
)
}

export default About
14 changes: 7 additions & 7 deletions src/pages/ErrorPage.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { useRouteError } from "react-router-dom";
import { useRouteError } from "react-router-dom"

function ErrorPage() {
const error = useRouteError();
console.error(error);
const error = useRouteError()
console.error(error)

return (
<main>
<h1>Whoops! Something went wrong!</h1>
<h1>Whoops! Something went wrong!</h1>
</main>
);
};
)
}

export default ErrorPage;
export default ErrorPage
39 changes: 12 additions & 27 deletions src/pages/Home.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,17 @@
import { useState, useEffect } from "react";
import UserCard from "../components/UserCard";
import NavBar from "../components/NavBar";
import { Outlet, useOutletContext } from "react-router-dom"
import UserCard from "../components/UserCard"

function Home() {
const [users, setUsers] = useState([]);

useEffect(() =>{
fetch("http://localhost:4000/users")
.then(r => r.json())
.then(data => setUsers(data))
.catch(error => console.error(error));
}, []);

const userList = users.map(user =>{
return <UserCard key={user.id} user={user}/>;
});
const users = useOutletContext()
const userList = users.map((user) => <UserCard key={user.id} user={user} />)

return (
<>
<header>
<NavBar />
</header>
<main>
<h1>Home!</h1>
{userList}
</main>
</>
);
};
<main>
<h1>Home!</h1>
<Outlet context={users} />
{userList}
</main>
)
}

export default Home;
export default Home
38 changes: 19 additions & 19 deletions src/pages/Login.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
function Login() {
return (
<>
<main>
<h1>Login</h1>
<form>
<div>
<input type="text" name="username" placeholder="Username" />
</div>
<div>
<input type="password" name="password" placeholder="Password" />
</div>
<input type="submit" value="Submit" />
</form>
</main>
</>
);
};
export default Login;
return (
<>
<main>
<h1>Login</h1>
<form>
<div>
<input type="text" name="username" placeholder="Username" />
</div>
<div>
<input type="password" name="password" placeholder="Password" />
</div>
<input type="submit" value="Submit" />
</form>
</main>
</>
)
}

export default Login
35 changes: 14 additions & 21 deletions src/pages/UserProfile.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
import { useEffect, useState } from "react";
import { useParams } from "react-router-dom";
import { useParams, useOutletContext } from "react-router-dom"

function UserProfile() {
const [user, setUser] = useState({});
const params = useParams();
const userId = params.id;
const params = useParams()
const users = useOutletContext()

useEffect(() =>{
fetch(`http://localhost:4000/users/${userId}`)
.then(r => r.json())
.then(data => setUser(data))
.catch(error => console.error(error));
}, [userId]);
const user = users.find((user) => user.id == parseInt(params.id))

if(!user.name){
return <h1>Loading...</h1>;
};
if (!user) {
return <h1>Loading...</h1>
}

return(
<aside>
<h1>{user.name}</h1>
</aside>
);
};
return (
<aside>
<h1>{user.name}</h1>
</aside>
)
}

export default UserProfile;
export default UserProfile
54 changes: 30 additions & 24 deletions src/routes.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
import Home from "./pages/Home";
import About from "./pages/About";
import Login from "./pages/Login";
import UserProfile from "./pages/UserProfile";
import ErrorPage from "./pages/ErrorPage";
import App from "./App"
import Home from "./pages/Home"
import About from "./pages/About"
import Login from "./pages/Login"
import UserProfile from "./pages/UserProfile"
import ErrorPage from "./pages/ErrorPage"

const routes = [
{
path: "/",
element: <Home />,
errorElement: <ErrorPage />
},
{
path: "/about",
element: <About />,
errorElement: <ErrorPage />
},
{
path: "/login",
element: <Login />,
errorElement: <ErrorPage />
element: <App />,
errorElement: <ErrorPage />,
children: [
{
path: "/",
element: <Home />,
children: [
{
path: "/profile/:id",
element: <UserProfile />,
},
],
},
{
path: "/about",
element: <About />,
},
{
path: "/login",
element: <Login />,
},
],
},
{
path: "/profile/:id",
element: <UserProfile />,
errorElement: <ErrorPage />
}
];
]

export default routes;
export default routes