diff --git a/package-lock.json b/package-lock.json index 3424db4..76ce624 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,6 +14,7 @@ "axios": "^1.6.2", "react": "^18.2.0", "react-dom": "^18.2.0", + "react-router-dom": "^6.20.0", "react-scripts": "5.0.1", "web-vitals": "^2.1.4" } @@ -3262,6 +3263,14 @@ } } }, + "node_modules/@remix-run/router": { + "version": "1.13.0", + "resolved": "https://registry.npmjs.org/@remix-run/router/-/router-1.13.0.tgz", + "integrity": "sha512-5dMOnVnefRsl4uRnAdoWjtVTdh8e6aZqgM4puy9nmEADH72ck+uXwzpJLEKE9Q6F8ZljNewLgmTfkxUrBdv4WA==", + "engines": { + "node": ">=14.0.0" + } + }, "node_modules/@rollup/plugin-babel": { "version": "5.3.1", "resolved": "https://registry.npmjs.org/@rollup/plugin-babel/-/plugin-babel-5.3.1.tgz", @@ -14768,6 +14777,36 @@ "node": ">=0.10.0" } }, + "node_modules/react-router": { + "version": "6.20.0", + "resolved": "https://registry.npmjs.org/react-router/-/react-router-6.20.0.tgz", + "integrity": "sha512-pVvzsSsgUxxtuNfTHC4IxjATs10UaAtvLGVSA1tbUE4GDaOSU1Esu2xF5nWLz7KPiMuW8BJWuPFdlGYJ7/rW0w==", + "dependencies": { + "@remix-run/router": "1.13.0" + }, + "engines": { + "node": ">=14.0.0" + }, + "peerDependencies": { + "react": ">=16.8" + } + }, + "node_modules/react-router-dom": { + "version": "6.20.0", + "resolved": "https://registry.npmjs.org/react-router-dom/-/react-router-dom-6.20.0.tgz", + "integrity": "sha512-CbcKjEyiSVpA6UtCHOIYLUYn/UJfwzp55va4yEfpk7JBN3GPqWfHrdLkAvNCcpXr8QoihcDMuk0dzWZxtlB/mQ==", + "dependencies": { + "@remix-run/router": "1.13.0", + "react-router": "6.20.0" + }, + "engines": { + "node": ">=14.0.0" + }, + "peerDependencies": { + "react": ">=16.8", + "react-dom": ">=16.8" + } + }, "node_modules/react-scripts": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/react-scripts/-/react-scripts-5.0.1.tgz", diff --git a/package.json b/package.json index e232f7a..8be18df 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "axios": "^1.6.2", "react": "^18.2.0", "react-dom": "^18.2.0", + "react-router-dom": "^6.20.0", "react-scripts": "5.0.1", "web-vitals": "^2.1.4" }, diff --git a/src/App.js b/src/App.js index 7dbbc3d..5b6da58 100644 --- a/src/App.js +++ b/src/App.js @@ -1,31 +1,24 @@ import './App.css'; -import Character from './components/character'; -import charactersData from './charactersData'; -import CharacterList from './components/character-list'; +import {Routes, Route} from 'react-router-dom'; +import HomePage from './pages/home-page'; +import CharacterOverview from './pages/character-overview'; +import CharacterDetail from './pages/character-detail'; // import something from 'some place' -// Creating a new function component in the same file -// Normally, we want to create this component in a separate file -// Then export it, and import it -const AppNumberTwo = () => { - return ( -
-

my title!!!

-

Heres some paragraph text

-
- ) -} // App.js is the entry point to the rest of your app +// You can see how this behavior is set up in index.js // This is default behavior for create-react-app function App() { return (
- { /* The below array iterator (map), loops over every element in the charactersData array */} - { /* The result of map is another array with new data */} - { /* In our case, this new data is a Character component */} - - { /* Using array iterator (map) instead of repeating like below: */ } +

Here is my header

+ + } /> + } /> + } /> + +

Here is my footer

); } diff --git a/src/components/character.js b/src/components/character.js index d6069cf..fc2cc19 100644 --- a/src/components/character.js +++ b/src/components/character.js @@ -1,6 +1,7 @@ // You can import other components and use them too! import Image from './image'; import LikeCounter from './like-counter'; +import {Link} from 'react-router-dom'; // A React component is just a function that returns some JSX // Specifically, it returns some JSX with only one parent element @@ -15,15 +16,10 @@ const Character = (props) => { return ( <>

{props.name}

-

Blood type

-

{props.blood}

-

Birthday

-

{props.birthday}

-

Quote

-

{props.quote}

{/* Passing props from CharacterList even further down into LikeCounter */} + Go to character detail page of character with id {props.id}
) diff --git a/src/index.js b/src/index.js index d563c0f..03976ff 100644 --- a/src/index.js +++ b/src/index.js @@ -3,11 +3,14 @@ import ReactDOM from 'react-dom/client'; import './index.css'; import App from './App'; import reportWebVitals from './reportWebVitals'; +import {BrowserRouter} from 'react-router-dom'; const root = ReactDOM.createRoot(document.getElementById('root')); root.render( - + + + ); diff --git a/src/pages/character-detail.js b/src/pages/character-detail.js new file mode 100644 index 0000000..a89a790 --- /dev/null +++ b/src/pages/character-detail.js @@ -0,0 +1,41 @@ +import {useParams} from 'react-router-dom'; +import axios from 'axios'; +import {useState, useEffect} from 'react'; + +const CharacterDetail = () => { + const params = useParams(); + //console.log(params) + const id = params.id; + console.log(id) + + const [details, setDetails] = useState() + + const getDetails = async () => { + const response = await axios.get(`https://my-json-server.typicode.com/TechmongersNL/fs03-react/characters/${id}`); + console.log(response.data); + setDetails(response.data); + } + + useEffect(() => { + getDetails(); + }, []) + + return ( + details ? + <> +

{details.name}

+

Blood type

+

{details.blood}

+

Birthday

+

{details.born}

+

Patronus

+

{details.patronus}

+

Quote

+

{details.quote}

+ {details.name} + : + 'Loading...' + ) +} + +export default CharacterDetail; \ No newline at end of file diff --git a/src/pages/character-overview.js b/src/pages/character-overview.js new file mode 100644 index 0000000..d72f9e0 --- /dev/null +++ b/src/pages/character-overview.js @@ -0,0 +1,11 @@ +import CharacterList from "../components/character-list"; + +const CharacterOverview = () => { + return ( +
+ +
+ ) +} + +export default CharacterOverview; \ No newline at end of file diff --git a/src/pages/home-page.js b/src/pages/home-page.js new file mode 100644 index 0000000..f22241d --- /dev/null +++ b/src/pages/home-page.js @@ -0,0 +1,13 @@ +import {Link} from 'react-router-dom'; + +const HomePage = () => { + return ( +
+

Home page

+

This is the Harry Potter home page!

+ +
+ ) +} + +export default HomePage; \ No newline at end of file