Skip to content
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

completed nested dynamic routes exercise #4

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
61 changes: 61 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.21.1",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
Expand Down
10 changes: 10 additions & 0 deletions src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
pointer-events: none;
}

.nav-links {
width: 40%;
display: flex;
justify-content: space-around;
align-items: center;
list-style: none;
}


@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
Expand All @@ -24,6 +33,7 @@
color: white;
}


.App-link {
color: #61dafb;
}
Expand Down
35 changes: 20 additions & 15 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
import logo from './logo.svg';
import { BrowserRouter, Routes, Route, Link} from 'react-router-dom';
import './App.css';
import Blog from './components/Blog';
import Nav from './components/Nav';



function App() {
return (
<BrowserRouter>
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
<Nav />

<Blog />
{/* <h1>Home Page</h1> */}
</div>
</BrowserRouter>
);
}

// const Home = () => {
// return (
// <div>
// <h1>Home</h1>
// </div>
// )
// }

export default App;

29 changes: 29 additions & 0 deletions src/components/Blog.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Routes, Route } from 'react-router-dom';
import PostDetails from './PostDetails'; // Import PostDetail component
import PostList from './PostList';

function Blog() {

return (

<main className="blog-main">
<Routes>
{/* <Route path="/" element={Home} /> */}
<Route path="/blog" element={<PostList />} /> {/* Base route for post list */}
<Route path="/blog/:postId" element={<PostDetails />} /> {/* Dynamic route for post detail */}
</Routes>
</main>
// ...
);
}

const Home = () => {
return (
<div>
<h1>Home</h1>
</div>
)
}


export default Blog;
23 changes: 23 additions & 0 deletions src/components/Nav.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from "react";
import '../App.css';
import { Link } from "react-router-dom";
import Blog from "./Blog";
import './nav.css'

function Nav () {
return (
<nav>

{/* <li>Blog</li> */}
<Link to={'/'}>Home Page</Link>
<Link to={`/blog/`}>Blog</Link>

{/* <div>
<Blog />
</div> */}

</nav>
)
}

export default Nav
32 changes: 32 additions & 0 deletions src/components/PostDetails.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from 'react';
import { useParams, Link } from 'react-router-dom';
import blogData from '../data/blog.json';

function PostDetails() {
const { postId } = useParams();
const post = blogData.posts[postId];


return (
<div className="post-detail">
<h1>{post.title}</h1>
<p className="post-author">By {post.author} on {post.date}</p>
<div className="post-tags">
{post.tags.map((tag) => (
<span key={tag} style={{ color:"red"}}className="tag">Tag:{tag} </span>
))}
</div>

<div className="post-content">
<p style={{ textDecoration: "underline"}}> {post.content} </p>
</div>



</div>


);
}

export default PostDetails;
24 changes: 24 additions & 0 deletions src/components/PostList.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from 'react';
import { Link } from 'react-router-dom';
import blogData from '../data/blog.json';

function PostList() {
// const posts = Object.values(blogData.posts);
const posts = blogData.posts;

return (
<React.Fragment>
<h1>BLOG, BLOG, BLOG </h1>
<ul style={{ listStyle: 'none'}}>
{Object.keys(posts).map((post) => (
<li key={post}>
<Link to={`/blog/${post}`}>{posts[post].title}</Link>
<p>{posts[post].summary}</p>
</li>
))}
</ul>
</React.Fragment>
);
}

export default PostList;
9 changes: 9 additions & 0 deletions src/components/nav.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
nav {
display: flex;
justify-content: space-around;
align-items: center;
min-height: 10vh;
/* list-style: none; */
background: royalblue;
color: white;
}