├── .DS_Store
├── README.md
├── package-lock.json
├── package.json
├── public
├── favicon.ico
├── index.html
├── logo192.png
├── logo512.png
├── manifest.json
└── robots.txt
└── src
├── About.js
├── App.css
├── App.js
├── App.test.js
├── Create.js
├── Edit.js
├── Home.js
├── Navbar.js
├── PostInfo.js
├── Posts.js
├── PostsList.js
├── PostsModel.js
├── Search.js
├── Update.js
├── index.css
├── index.js
├── reportWebVitals.js
└── setupTests.js
/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/muhammedessa/reactJS/f2954bb758672616711789a81281c806f325ddd8/.DS_Store
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Getting Started with Create React App
2 |
3 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
4 |
5 | ## Available Scripts
6 |
7 | In the project directory, you can run:
8 |
9 | ### `npm start`
10 |
11 | Runs the app in the development mode.\
12 | Open [http://localhost:3000](http://localhost:3000) to view it in your browser.
13 |
14 | The page will reload when you make changes.\
15 | You may also see any lint errors in the console.
16 |
17 | ### `npm test`
18 |
19 | Launches the test runner in the interactive watch mode.\
20 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
21 |
22 | ### `npm run build`
23 |
24 | Builds the app for production to the `build` folder.\
25 | It correctly bundles React in production mode and optimizes the build for the best performance.
26 |
27 | The build is minified and the filenames include the hashes.\
28 | Your app is ready to be deployed!
29 |
30 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
31 |
32 | ### `npm run eject`
33 |
34 | **Note: this is a one-way operation. Once you `eject`, you can't go back!**
35 |
36 | If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
37 |
38 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own.
39 |
40 | You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it.
41 |
42 | ## Learn More
43 |
44 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
45 |
46 | To learn React, check out the [React documentation](https://reactjs.org/).
47 |
48 | ### Code Splitting
49 |
50 | This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)
51 |
52 | ### Analyzing the Bundle Size
53 |
54 | This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)
55 |
56 | ### Making a Progressive Web App
57 |
58 | This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)
59 |
60 | ### Advanced Configuration
61 |
62 | This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)
63 |
64 | ### Deployment
65 |
66 | This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)
67 |
68 | ### `npm run build` fails to minify
69 |
70 | This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
71 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "myapp",
3 | "version": "0.1.0",
4 | "private": true,
5 | "dependencies": {
6 | "@testing-library/jest-dom": "^5.16.4",
7 | "@testing-library/react": "^13.2.0",
8 | "@testing-library/user-event": "^13.5.0",
9 | "bootstrap": "^5.1.3",
10 | "jquery": "^3.6.0",
11 | "react": "^18.1.0",
12 | "react-dom": "^18.1.0",
13 | "react-router": "^6.3.0",
14 | "react-router-dom": "^6.3.0",
15 | "react-scripts": "5.0.1",
16 | "web-vitals": "^2.1.4"
17 | },
18 | "scripts": {
19 | "start": "react-scripts start",
20 | "build": "react-scripts build",
21 | "test": "react-scripts test",
22 | "eject": "react-scripts eject"
23 | },
24 | "eslintConfig": {
25 | "extends": [
26 | "react-app",
27 | "react-app/jest"
28 | ]
29 | },
30 | "browserslist": {
31 | "production": [
32 | ">0.2%",
33 | "not dead",
34 | "not op_mini all"
35 | ],
36 | "development": [
37 | "last 1 chrome version",
38 | "last 1 firefox version",
39 | "last 1 safari version"
40 | ]
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/muhammedessa/reactJS/f2954bb758672616711789a81281c806f325ddd8/public/favicon.ico
--------------------------------------------------------------------------------
/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
12 |
13 |
17 |
18 |
27 | Muhammed App
28 |
29 |
30 | You need to enable JavaScript to run this app.
31 |
32 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/public/logo192.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/muhammedessa/reactJS/f2954bb758672616711789a81281c806f325ddd8/public/logo192.png
--------------------------------------------------------------------------------
/public/logo512.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/muhammedessa/reactJS/f2954bb758672616711789a81281c806f325ddd8/public/logo512.png
--------------------------------------------------------------------------------
/public/manifest.json:
--------------------------------------------------------------------------------
1 | {
2 | "short_name": "React App",
3 | "name": "Create React App Sample",
4 | "icons": [
5 | {
6 | "src": "favicon.ico",
7 | "sizes": "64x64 32x32 24x24 16x16",
8 | "type": "image/x-icon"
9 | },
10 | {
11 | "src": "logo192.png",
12 | "type": "image/png",
13 | "sizes": "192x192"
14 | },
15 | {
16 | "src": "logo512.png",
17 | "type": "image/png",
18 | "sizes": "512x512"
19 | }
20 | ],
21 | "start_url": ".",
22 | "display": "standalone",
23 | "theme_color": "#000000",
24 | "background_color": "#ffffff"
25 | }
26 |
--------------------------------------------------------------------------------
/public/robots.txt:
--------------------------------------------------------------------------------
1 | # https://www.robotstxt.org/robotstxt.html
2 | User-agent: *
3 | Disallow:
4 |
--------------------------------------------------------------------------------
/src/About.js:
--------------------------------------------------------------------------------
1 | import { useState } from "react";
2 |
3 | const About = ()=>{
4 |
5 | // let firstname = 'Muhammed';
6 | const [firstname, setFirstname] = useState('Muhammed');
7 | const [secondname, setSecondname] = useState('Essa');
8 | const [age, setAge] = useState(37);
9 |
10 |
11 | const changeName = ()=>{
12 | setFirstname('Ahmed');
13 | setSecondname('Osama');
14 | setAge(40);
15 | }
16 | return(
17 |
18 |
About page
19 | {firstname}
20 | {secondname}
21 | {age}
22 |
23 | Primary
24 |
25 |
26 |
27 | );
28 | }
29 |
30 | export default About;
--------------------------------------------------------------------------------
/src/App.css:
--------------------------------------------------------------------------------
1 | .App {
2 | text-align: center;
3 | }
4 |
5 | .App-logo {
6 | height: 40vmin;
7 | pointer-events: none;
8 | }
9 |
10 | @media (prefers-reduced-motion: no-preference) {
11 | .App-logo {
12 | animation: App-logo-spin infinite 20s linear;
13 | }
14 | }
15 |
16 | .App-header {
17 | background-color: #282c34;
18 | min-height: 100vh;
19 | display: flex;
20 | flex-direction: column;
21 | align-items: center;
22 | justify-content: center;
23 | font-size: calc(10px + 2vmin);
24 | color: white;
25 | }
26 |
27 | .App-link {
28 | color: #61dafb;
29 | }
30 |
31 | @keyframes App-logo-spin {
32 | from {
33 | transform: rotate(0deg);
34 | }
35 | to {
36 | transform: rotate(360deg);
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/App.js:
--------------------------------------------------------------------------------
1 | import './App.css';
2 | import Home from './Home';
3 | import Navbar from './Navbar';
4 | import About from './About';
5 | import Posts from './Posts';
6 |
7 | function App() {
8 |
9 | const name = "Ahmed Essa Hameed";
10 | const age = 38;
11 | const salary = 3008;
12 | const mywebsite = "facebook.com"
13 | return (
14 |
15 |
16 |
17 | {/*
*/}
18 |
19 | {/*
*/}
20 |
21 | {/*
{name}
22 |
{ age }
23 |
{salary}
24 |
{[11,22,33,44]}
25 |
Facebook */}
26 | {/*
*/}
27 |
28 |
29 |
30 | );
31 | }
32 |
33 | export default App;
34 |
--------------------------------------------------------------------------------
/src/App.test.js:
--------------------------------------------------------------------------------
1 | import { render, screen } from '@testing-library/react';
2 | import App from './App';
3 |
4 | test('renders learn react link', () => {
5 | render( );
6 | const linkElement = screen.getByText(/learn react/i);
7 | expect(linkElement).toBeInTheDocument();
8 | });
9 |
--------------------------------------------------------------------------------
/src/Create.js:
--------------------------------------------------------------------------------
1 | import { useState } from "react";
2 | import { useNavigate } from "react-router-dom";
3 | const Create = ()=>{
4 |
5 | const [userId, setUserId] = useState('')
6 | const [title, setTitle] = useState('')
7 | const [body, setBody] = useState('')
8 | const [watingServer, setWatingServer] = useState(false)
9 | const navigator = useNavigate();
10 | const mySubmit = (e)=>{
11 | e.preventDefault();
12 | const mypost = {
13 | title ,
14 | body ,
15 | userId ,
16 | }
17 | console.log(mypost)
18 |
19 | setWatingServer(true);
20 | setTimeout(() => {
21 | fetch('https://jsonplaceholder.typicode.com/posts', {
22 | method: 'POST',
23 | body: JSON.stringify(mypost),
24 | headers: {
25 | 'Content-type': 'application/json; charset=UTF-8',
26 | },
27 | })
28 | .then((response) => response.json())
29 | .then((json) => console.log(json))
30 | .then(() => {
31 | console.log('new post added');
32 | setWatingServer(false);
33 | navigator('/posts')
34 | });
35 | }, 2000);
36 |
37 |
38 |
39 | }
40 | return(
41 |
90 | );
91 | }
92 |
93 | export default Create;
--------------------------------------------------------------------------------
/src/Edit.js:
--------------------------------------------------------------------------------
1 | import { useState, useEffect } from "react";
2 | import { useNavigate, useParams } from "react-router-dom";
3 | const Edit = ()=>{
4 |
5 | const { id } = useParams();
6 | const [isWaiting, setIsWaiting] = useState(true);
7 | const [userId, setUserId] = useState('')
8 | const [title, setTitle] = useState('')
9 | const [body, setBody] = useState('')
10 | const [watingServer, setWatingServer] = useState(false)
11 | const navigator = useNavigate();
12 |
13 | useEffect(() => {
14 | setTimeout(() => {
15 | fetch('https://jsonplaceholder.typicode.com/posts/' + id)
16 | .then(response => {
17 | console.log(response.ok)
18 | if (!response.ok) {
19 | throw Error('Can not connect to the server!.');
20 | }
21 | return response.json();
22 | })
23 | .then(data => {
24 | // console.log(data);
25 | setUserId(data.userId)
26 | setTitle(data.title)
27 | setBody(data.body)
28 | setIsWaiting(false)
29 | }).catch(e => {
30 | console.log(e.message);
31 | });
32 | }, 2000);
33 |
34 | }, []);
35 |
36 | const mySubmit = (e)=>{
37 | e.preventDefault();
38 | const mypost = {
39 | id,
40 | title ,
41 | body ,
42 | userId ,
43 | }
44 | console.log(mypost)
45 |
46 | setWatingServer(true);
47 | setTimeout(() => {
48 | fetch('https://jsonplaceholder.typicode.com/posts/' + id, {
49 | method: 'PUT',
50 | body: JSON.stringify(mypost),
51 | headers: {
52 | 'Content-type': 'application/json; charset=UTF-8',
53 | },
54 | })
55 | .then((response) => response.json())
56 | .then((json) => console.log(json))
57 | .then(() => {
58 | console.log('new post updated');
59 | setWatingServer(false);
60 | navigator('/posts')
61 | });
62 | }, 2000);
63 |
64 |
65 |
66 | }
67 | return(
68 |
69 | {isWaiting &&
Please wait to load data ... }
70 |
71 |
72 |
73 | User Id
74 | setUserId(e.target.value)}
80 | />
81 |
82 |
83 | Title
84 | setTitle(e.target.value)}
90 | />
91 |
92 |
93 | Body
94 | setBody(e.target.value)}
100 | >
101 |
102 |
103 |
104 | {!watingServer && Update }
105 | {watingServer && Please wait }
106 |
107 |
108 |
109 |
110 | {userId}
111 | {title}
112 | {body}
113 |
114 |
115 |
116 | );
117 | }
118 |
119 | export default Edit;
--------------------------------------------------------------------------------
/src/Home.js:
--------------------------------------------------------------------------------
1 | const Home = ()=>{
2 |
3 |
4 |
5 | const clickMe = (e)=>{
6 | console.log(e.target);
7 | }
8 |
9 | const clickWithParams = (firstname,e)=>{
10 | console.log('First name is ' + firstname + e);
11 | }
12 | const clickWithTwoParams = (firstname, secondName) => {
13 | console.log('First name is ' + firstname + ' And second name is ' + secondName);
14 | }
15 |
16 | return(
17 |
18 |
Home page
19 | Primary
20 |
21 | clickWithParams('Muhammed',e) } className="btn btn-danger">Danger
22 |
23 | clickWithTwoParams('Muhammed', 'Essa')} className="btn btn-info">Danger
24 |
25 |
26 | );
27 | }
28 |
29 | export default Home;
--------------------------------------------------------------------------------
/src/Navbar.js:
--------------------------------------------------------------------------------
1 | import { BrowserRouter,Link,Route,Routes } from "react-router-dom";
2 |
3 | import Home from './Home';
4 | import Posts from './Posts';
5 | import About from './About';
6 | import PostInfo from './PostInfo';
7 | import Create from './Create';
8 | import Edit from './Edit';
9 | import Search from './Search';
10 |
11 | const Navbar = ()=>{
12 | return(
13 |
14 |
15 |
16 |
17 |
18 |
Navbar
19 |
20 |
21 |
22 |
23 |
24 |
25 | Home
26 |
27 |
28 | Posts
29 |
30 |
31 | Create
32 |
33 |
34 | Search
35 |
36 |
37 | About
38 |
39 |
40 |
41 |
42 | Search
43 |
44 |
45 |
46 |
47 |
48 | }>
49 | }>
50 | }>
51 | }>
52 | }>
53 | }>
54 | }>
55 |
56 |
57 |
58 |
59 |
60 |
61 | );
62 | }
63 |
64 | export default Navbar;
--------------------------------------------------------------------------------
/src/PostInfo.js:
--------------------------------------------------------------------------------
1 | import { useParams } from "react-router-dom";
2 | import PostsModel from './PostsModel';
3 |
4 | const PostInfo = () => {
5 | const {id} = useParams();
6 | const { isWaiting, serverError, posts } = PostsModel('https://jsonplaceholder.typicode.com/posts/' + id);
7 |
8 | console.log(posts)
9 | return (
10 |
11 |
12 |
13 |
PostInfo - {id}
14 |
15 | {serverError &&
{serverError} ... }
16 | {isWaiting &&
Please wait to load data ... }
17 |
18 | {posts && (
19 |
28 | )
29 |
30 | }
31 |
32 |
33 | );
34 | }
35 |
36 | export default PostInfo;
37 |
--------------------------------------------------------------------------------
/src/Posts.js:
--------------------------------------------------------------------------------
1 | import { useEffect, useState } from "react";
2 | import PostsList from './PostsList';
3 | import PostsModel from './PostsModel';
4 |
5 | const Posts = ()=>{
6 |
7 | const { isWaiting, serverError, posts} = PostsModel('https://jsonplaceholder.typicode.com/posts');
8 |
9 | return(
10 |
11 |
12 | {serverError &&
{serverError} ... }
13 | {isWaiting &&
Please wait to load data ... }
14 | {posts &&
}
15 |
16 |
17 | );
18 | }
19 |
20 | export default Posts;
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 | {/* setCheckAuth('true')} className="btn btn-danger">Danger
31 | status: {checkAuth} */}
32 |
33 | {/* {posts && } */ }
34 |
35 | // const [checkAuth, setCheckAuth] = useState('false');
36 | // const [posts1, setPost1] = useState([
37 | // {
38 | // userId: 1,
39 | // id: 1,
40 | // title: "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
41 | // body: "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
42 | // },
43 | // {
44 | // userId: 1,
45 | // id: 2,
46 | // title: "qui est esse",
47 | // body: "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
48 | // },
49 | // {
50 | // userId: 1,
51 | // id: 3,
52 | // title: "ea molestias quasi exercitationem repellat qui ipsa sit aut",
53 | // body: "et iusto sed quo iure\nvoluptatem occaecati omnis eligendi aut ad\nvoluptatem doloribus vel accusantium quis pariatur\nmolestiae porro eius odio et labore et velit aut"
54 | // },
55 | // {
56 | // userId: 1,
57 | // id: 4,
58 | // title: "eum et est occaecati",
59 | // body: "ullam et saepe reiciendis voluptatem adipisci\nsit amet autem assumenda provident rerum culpa\nquis hic commodi nesciunt rem tenetur doloremque ipsam iure\nquis sunt voluptatem rerum illo velit"
60 | // },
61 | // {
62 | // userId: 1,
63 | // id: 5,
64 | // title: "nesciunt quas odio",
65 | // body: "repudiandae veniam quaerat sunt sed\nalias aut fugiat sit autem sed est\nvoluptatem omnis possimus esse voluptatibus quis\nest aut tenetur dolor neque"
66 | // },
67 | // ]);
68 | // const deleteAction = (id) => {
69 | // const updatedPosts = posts.filter(post=> post.id != id);
70 | // setPost(updatedPosts)
71 | // }
--------------------------------------------------------------------------------
/src/PostsList.js:
--------------------------------------------------------------------------------
1 | import { useState } from "react";
2 | import { useNavigate, Link } from "react-router-dom";
3 | const PostsList = ({ posts, name})=>{
4 |
5 |
6 |
7 | const navigator = useNavigate();
8 | const deleteAction = (id) => {
9 | fetch('https://jsonplaceholder.typicode.com/posts/' + id, {
10 | method: 'DELETE',
11 | }).then(() => {
12 |
13 | navigator('/')
14 | });
15 | // const updatedPosts = posts.filter(post=> post.id != id);
16 | // setPost(updatedPosts)
17 | }
18 |
19 | return(
20 |
21 |
22 |
{name}
23 |
24 |
25 |
26 | ID
27 | title
28 | body
29 | Delete
30 |
31 |
32 |
33 |
34 | { posts.map((post) => (
35 |
36 | {post.id}
37 | {post.title}
38 | {post.body}
39 |
40 | deleteAction(post.id)} type="button" className="btn btn-danger">Delete
41 |
42 |
43 | More
44 |
45 |
46 | Edit
47 |
48 |
49 |
50 |
51 | ))}
52 |
53 |
54 |
55 |
56 | );
57 | }
58 |
59 | export default PostsList;
--------------------------------------------------------------------------------
/src/PostsModel.js:
--------------------------------------------------------------------------------
1 | import { useEffect, useState } from "react";
2 | import { useNavigate } from "react-router-dom";
3 |
4 | const PostsModel = (url)=>{
5 | const [isWaiting, setIsWaiting] = useState(true);
6 | const [serverError, setServerError] = useState(null);
7 | const [posts, setPost] = useState(null);
8 | const navigator = useNavigate();
9 |
10 | const deleteAction = (id) => {
11 | fetch('https://jsonplaceholder.typicode.com/posts/' + id, {
12 | method: 'DELETE',
13 | }).then(()=>{
14 | navigator('/posts')
15 | });
16 | // const updatedPosts = posts.filter(post=> post.id != id);
17 | // setPost(updatedPosts)
18 | }
19 |
20 | useEffect(() => {
21 | setTimeout(() => {
22 | fetch(url)
23 | .then(response => {
24 | console.log(response.ok)
25 | if (!response.ok) {
26 | throw Error('Can not connect to the server!.');
27 | }
28 | return response.json();
29 | })
30 | .then(data => {
31 | console.log(data);
32 | setPost(data);
33 | setIsWaiting(false)
34 | }).catch(e => {
35 | console.log(e.message);
36 | setServerError(e.message);
37 | setIsWaiting(false)
38 | });
39 | }, 2000);
40 |
41 | }, [url]);
42 |
43 |
44 | return { isWaiting, serverError, posts}
45 | }
46 |
47 | export default PostsModel;
--------------------------------------------------------------------------------
/src/Search.js:
--------------------------------------------------------------------------------
1 | import { useState, useEffect } from "react";
2 |
3 | const Search = () => {
4 |
5 | const [query, setQuery] = useState("")
6 |
7 | const [posts, setPost] = useState(null);
8 | useEffect(() => {
9 | fetch('https://jsonplaceholder.typicode.com/posts')
10 | .then(response => {
11 | console.log(response.ok)
12 | if (!response.ok) {
13 | throw Error('Can not connect to the server!.');
14 | }
15 | return response.json();
16 | }).then(data => {
17 | console.log(data);
18 | setPost(data)
19 | }).catch(e => {
20 | console.log(e.message);
21 | });
22 | }, []);
23 |
24 | return (
25 | < div >
26 |
27 | setQuery(event.target.value)}/>
28 | {posts &&
29 | posts.filter(post => {
30 | if (query === '') {
31 | return post;
32 | } else if (post.title.toLowerCase().includes(query.toLowerCase())) {
33 | return post;
34 | }
35 | }).map((post, index) => (
36 |
37 |
{post.title}
38 |
{post.userId}
39 |
{post.body}
40 |
41 | ))
42 | }
43 |
44 | );
45 | }
46 |
47 |
48 |
49 | export default Search;
--------------------------------------------------------------------------------
/src/Update.js:
--------------------------------------------------------------------------------
1 | import { useState, useEffect } from "react";
2 | import { useNavigate, useParams } from "react-router-dom";
3 |
4 | const Update = ()=>{
5 |
6 | const { id } = useParams();
7 |
8 | useEffect(() => {
9 | fetch('https://jsonplaceholder.typicode.com/posts/' + id)
10 | .then(response => {
11 | console.log(response.ok)
12 | if (!response.ok) {
13 | throw Error('Can not connect to the server!.');
14 | }
15 | return response.json();
16 | }).then(data => {
17 | console.log(data);
18 | setUserId(data.userId)
19 | setTitle(data.title)
20 | setBody(data.body)
21 | }).catch(e => {
22 | console.log(e.message);
23 | });
24 | }, []);
25 |
26 |
27 | const [userId, setUserId] = useState('')
28 | const [title, setTitle] = useState('')
29 | const [body, setBody] = useState('')
30 | const [watingServer, setWatingServer] = useState(false)
31 | const navigator = useNavigate();
32 | const mySubmit = (e)=>{
33 | e.preventDefault();
34 | const mypost = {
35 | id,
36 | title ,
37 | body ,
38 | userId ,
39 | }
40 | console.log(mypost)
41 |
42 | setWatingServer(true);
43 | setTimeout(() => {
44 | fetch('https://jsonplaceholder.typicode.com/posts/' + id, {
45 | method: 'PUT',
46 | body: JSON.stringify(mypost),
47 | headers: {
48 | 'Content-type': 'application/json; charset=UTF-8',
49 | },
50 | })
51 | .then((response) => response.json())
52 | .then((json) => console.log(json))
53 | .then(() => {
54 | console.log('new post updated');
55 | setWatingServer(false);
56 | navigator('/posts')
57 | });
58 | }, 2000);
59 |
60 |
61 |
62 | }
63 | return(
64 |
65 |
66 |
67 | User Id
68 | setUserId(e.target.value)}
75 | />
76 |
77 |
78 | Title
79 | setTitle(e.target.value)}
86 | />
87 |
88 |
89 | Body
90 | setBody(e.target.value)}
97 | >
98 |
99 |
100 |
101 | {!watingServer &&Update }
102 | {watingServer && Please wait }
103 |
104 |
105 |
106 |
107 | {userId}
108 | {title}
109 | {body}
110 |
111 |
112 |
113 | );
114 | }
115 |
116 | export default Update;
--------------------------------------------------------------------------------
/src/index.css:
--------------------------------------------------------------------------------
1 | body {
2 | margin: 0;
3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
5 | sans-serif;
6 | -webkit-font-smoothing: antialiased;
7 | -moz-osx-font-smoothing: grayscale;
8 | }
9 |
10 | code {
11 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
12 | monospace;
13 | }
14 |
--------------------------------------------------------------------------------
/src/index.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import ReactDOM from 'react-dom/client';
3 | import './index.css';
4 | import App from './App';
5 | import reportWebVitals from './reportWebVitals';
6 |
7 | import 'bootstrap/dist/css/bootstrap.css';
8 | import 'bootstrap/dist/js/bootstrap.js';
9 |
10 | const root = ReactDOM.createRoot(document.getElementById('root'));
11 | root.render(
12 |
13 |
14 |
15 | );
16 |
17 | // If you want to start measuring performance in your app, pass a function
18 | // to log results (for example: reportWebVitals(console.log))
19 | // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
20 | reportWebVitals();
21 |
--------------------------------------------------------------------------------
/src/reportWebVitals.js:
--------------------------------------------------------------------------------
1 | const reportWebVitals = onPerfEntry => {
2 | if (onPerfEntry && onPerfEntry instanceof Function) {
3 | import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => {
4 | getCLS(onPerfEntry);
5 | getFID(onPerfEntry);
6 | getFCP(onPerfEntry);
7 | getLCP(onPerfEntry);
8 | getTTFB(onPerfEntry);
9 | });
10 | }
11 | };
12 |
13 | export default reportWebVitals;
14 |
--------------------------------------------------------------------------------
/src/setupTests.js:
--------------------------------------------------------------------------------
1 | // jest-dom adds custom jest matchers for asserting on DOM nodes.
2 | // allows you to do things like:
3 | // expect(element).toHaveTextContent(/react/i)
4 | // learn more: https://github.com/testing-library/jest-dom
5 | import '@testing-library/jest-dom';
6 |
--------------------------------------------------------------------------------