Skip to content

Commit c1df3ed

Browse files
committed
ContextAPI has been implemented.
1 parent 5e52d3d commit c1df3ed

File tree

7 files changed

+98
-21
lines changed

7 files changed

+98
-21
lines changed

day-09/api/src/main/java/com/bookstore/api/controllers/AuthorController.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import org.springframework.http.HttpStatus;
66
import org.springframework.http.ResponseEntity;
7+
import org.springframework.web.bind.annotation.CrossOrigin;
78
import org.springframework.web.bind.annotation.DeleteMapping;
89
import org.springframework.web.bind.annotation.GetMapping;
910
import org.springframework.web.bind.annotation.PathVariable;
@@ -19,6 +20,7 @@
1920

2021
@RestController
2122
@RequestMapping("api/v1/authors")
23+
@CrossOrigin
2224
public class AuthorController {
2325

2426
private final AuthorService authorService;

day-09/bs-store/package-lock.json

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

day-09/bs-store/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"@testing-library/jest-dom": "^5.16.4",
77
"@testing-library/react": "^13.3.0",
88
"@testing-library/user-event": "^13.5.0",
9+
"axios": "^0.27.2",
910
"react": "^18.2.0",
1011
"react-dom": "^18.2.0",
1112
"react-router-dom": "^6.3.0",

day-09/bs-store/src/App.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@ import ListAuthor from "./adminpages/authors/ListAuthor";
33
import ListBook from "./adminpages/books/ListBook";
44
import ListCategory from "./adminpages/categories/ListCategory";
55
import TopLink from "./components/links/TopLink";
6-
6+
import AppContext from "./context/AppContext";
7+
import { useContext } from "react";
78
import Home from "./pages/home/Home";
89

9-
import data from "./data";
10-
import { useState } from "react";
11-
1210

1311
function App() {
14-
const [authors, setAuthors] = useState(data);
12+
const {authors, setAuthors} = useContext(AppContext);
1513

1614
return (
1715
<div>
@@ -21,7 +19,7 @@ function App() {
2119
<Route path='/admin/categories/list' element={<ListCategory />} />
2220
<Route
2321
path='/admin/authors/list'
24-
element={<ListAuthor authors={authors} setAuthors={setAuthors} />}
22+
element={<ListAuthor />}
2523
/>
2624
<Route path='/' element={<Home />} />
2725
</Routes>

day-09/bs-store/src/adminpages/authors/AddAuthor.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
import React from "react";
1+
import React, { useContext } from "react";
2+
import AppContext from "../../context/AppContext";
23

3-
export default function AddAuthor({authors, setAuthors}) {
4+
export default function AddAuthor() {
5+
6+
const {authors,setAuthors} = useContext(AppContext);
47

58
const handleClick = () => {
69
const entity = {
Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,45 @@
11
import React from "react";
22
import { Link } from "react-router-dom";
3+
import AppContext from "../../context/AppContext";
34
import AddAuthor from "./AddAuthor";
45

5-
export default function ListAuthor({authors, setAuthors}) {
6+
export default function ListAuthor() {
7+
const { authors, setAuthors } = React.useContext(AppContext);
8+
9+
const removeAuthor = (id) => {
10+
// let arr = [];
11+
12+
// for(const author of authors){
13+
// if(author.id!==id){
14+
// arr.push(author);
15+
// }
16+
// }
17+
// setAuthors(arr);
18+
19+
20+
let afterRemove = authors.filter(author => author.id!==id)
21+
setAuthors(afterRemove);
22+
}
23+
624
return (
725
<div>
826
Author List {authors.length}
9-
10-
{
11-
authors.map((author)=> {
12-
const {id,firstName, lastName} = author
13-
return(
14-
<p key={id} >{`${id} ${firstName} ${lastName}`}</p>
15-
)
16-
})
17-
}
18-
27+
{authors.map((author,index) => {
28+
const { id, firstName, lastName } = author;
29+
return <p
30+
key={index}>
31+
{`${id} ${firstName} ${lastName}`}
32+
<button onClick={() => removeAuthor(id)} >Remove</button>
33+
</p>;
34+
})}
1935
{/* <Link to='/admin/authors/add'>Add</Link> */}
20-
<AddAuthor authors={authors} setAuthors={setAuthors} />
36+
<div>
37+
<button onClick={() => setAuthors([])} >Clear all</button>
38+
</div>
39+
<div>
40+
<AddAuthor />
41+
42+
</div>
2143
</div>
2244
);
2345
}

day-09/bs-store/src/context/AppContext.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
import React, { createContext, useState } from "react";
1+
import axios from "axios";
2+
import React, { createContext, useState, useEffect } from "react";
23
import data from "../data";
34
const AppContext = createContext();
45

56
export const AppContextProvider = ({ children }) => {
67

78
const [authors, setAuthors] = useState(data);
9+
10+
useEffect(() => {
11+
const url = "http://localhost:8080/api/v1/authors";
12+
axios.get(url).then(resp=> {setAuthors(resp.data.data)});
13+
14+
}, [])
815

916
const values = {
1017
message: "selam",

0 commit comments

Comments
 (0)