Skip to content

Commit d94e843

Browse files
committed
Adding Examples
1 parent c0a8777 commit d94e843

26 files changed

+29094
-0
lines changed

Sections/Section 10/01-starting-project/package-lock.json

Lines changed: 28500 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "react-complete-guide",
3+
"version": "0.1.0",
4+
"private": true,
5+
"dependencies": {
6+
"@testing-library/jest-dom": "^5.11.6",
7+
"@testing-library/react": "^11.2.2",
8+
"@testing-library/user-event": "^12.5.0",
9+
"react": "^18.0.0",
10+
"react-dom": "^18.0.0",
11+
"react-scripts": "^5.0.1",
12+
"web-vitals": "^0.2.4"
13+
},
14+
"scripts": {
15+
"start": "react-scripts start",
16+
"build": "react-scripts build",
17+
"test": "react-scripts test",
18+
"eject": "react-scripts eject"
19+
},
20+
"eslintConfig": {
21+
"extends": [
22+
"react-app",
23+
"react-app/jest"
24+
]
25+
},
26+
"browserslist": {
27+
"production": [
28+
">0.2%",
29+
"not dead",
30+
"not op_mini all"
31+
],
32+
"development": [
33+
"last 1 chrome version",
34+
"last 1 firefox version",
35+
"last 1 safari version"
36+
]
37+
}
38+
}
Binary file not shown.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1" />
7+
<meta name="theme-color" content="#000000" />
8+
<meta
9+
name="description"
10+
content="Web site created using create-react-app"
11+
/>
12+
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
13+
<!--
14+
manifest.json provides metadata used when your web app is installed on a
15+
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
16+
-->
17+
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
18+
<!--
19+
Notice the use of %PUBLIC_URL% in the tags above.
20+
It will be replaced with the URL of the `public` folder during the build.
21+
Only files inside the `public` folder can be referenced from the HTML.
22+
23+
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
24+
work correctly both with client-side routing and a non-root public URL.
25+
Learn how to configure a non-root public URL by running `npm run build`.
26+
-->
27+
<title>React App</title>
28+
</head>
29+
<body>
30+
<noscript>You need to enable JavaScript to run this app.</noscript>
31+
<div id="root"></div>
32+
<!--
33+
This HTML file is a template.
34+
If you open it directly in the browser, you will see an empty page.
35+
36+
You can add webfonts, meta tags, or analytics to this file.
37+
The build step will place the bundled scripts into the <body> tag.
38+
39+
To begin the development, run `npm start` or `yarn start`.
40+
To create a production bundle, use `npm run build` or `yarn build`.
41+
-->
42+
</body>
43+
</html>
Loading
Loading
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# https://www.robotstxt.org/robotstxt.html
2+
User-agent: *
3+
Disallow:
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React, { useContext } from 'react';
2+
3+
import Login from './components/Login/Login';
4+
import Home from './components/Home/Home';
5+
import MainHeader from './components/MainHeader/MainHeader';
6+
import AuthContext from './store/auth-context';
7+
8+
function App() {
9+
const ctx = useContext(AuthContext)
10+
return (
11+
<React.Fragment>
12+
<MainHeader/>
13+
<main>
14+
{!ctx.isLoggedIn && <Login/>}
15+
{ctx.isLoggedIn && <Home />}
16+
</main>
17+
</React.Fragment> );
18+
}
19+
20+
export default App;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React from 'react';
2+
import AuthContext from '../../store/auth-context';
3+
import { useContext } from 'react';
4+
import Card from '../UI/Card/Card';
5+
import classes from './Home.module.css';
6+
import Button from '../UI/Button/Button';
7+
8+
const Home = () => {
9+
const authCtx = useContext(AuthContext)
10+
return (
11+
<Card className={classes.home}>
12+
<h1>Welcome back!</h1>
13+
<Button onClick={authCtx.onLogout}>Logout</Button>
14+
</Card>
15+
);
16+
};
17+
18+
export default Home;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.home {
2+
width: 90%;
3+
max-width: 40rem;
4+
padding: 3rem;
5+
margin: 2rem auto;
6+
text-align: center;
7+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import React, { useContext, useEffect, useReducer, useState } from 'react';
2+
3+
import Card from '../UI/Card/Card';
4+
import classes from './Login.module.css';
5+
import Button from '../UI/Button/Button';
6+
import AuthContext from '../../store/auth-context';
7+
import Input from '../UI/Input/Input';
8+
9+
// For useReducer Example
10+
const emailReducer = (state, action) => {
11+
if(action.type === 'USER_INPUT') {
12+
return {value: action.val, isValid: action.val.includes('@')}
13+
}
14+
if(action.type === 'INPUT_BLUR') {
15+
return {value: state.value, isValid: state.value.includes('@')}
16+
17+
}
18+
return {value:'', isValid: false}
19+
}
20+
21+
const passwordReducer = (state, action) => {
22+
if(action.type === "USER_INPUT"){
23+
return { value: action.val, isValid: action.val.trim().length > 6 }
24+
}
25+
26+
if(action.type === 'INPUT_BLUR'){
27+
return {value: state.value, isValid: state.value.trim().length > 6}
28+
}
29+
}
30+
31+
const Login = () => {
32+
// This is for the useEffect Example
33+
// const [enteredEmail, setEnteredEmail] = useState('');
34+
// const [emailIsValid, setEmailIsValid] = useState();
35+
// const [enteredPassword, setEnteredPassword] = useState('');
36+
// const [passwordIsValid, setPasswordIsValid] = useState();
37+
const [formIsValid, setFormIsValid] = useState(false);
38+
39+
// This is for useReducer Example
40+
const [emailState, dispatchEmail] = useReducer(emailReducer,{value:'', isValid: false})
41+
const [passwordState, dispatchPassword] = useReducer(passwordReducer,{value:'', isValid: false})
42+
43+
// useEffect(() => {console.log('Effect Running')}, [])
44+
// console.log("Component Rendering...")
45+
// To reduce unneccessary useEffect
46+
// Desctructs the object and adds an alias to the destructured values. We use these object properties as dependencies in the useEffect hook
47+
const { isValid: emailIsValid } = emailState
48+
const { isValid: passwordIsValid } = passwordState
49+
50+
const authCtx = useContext(AuthContext)
51+
52+
useEffect(()=>{
53+
const identifier = setTimeout(() => {
54+
// We only care about whether the isValid state updates
55+
setFormIsValid(emailIsValid && passwordIsValid)
56+
},500)
57+
58+
// Clean Up function that runs before useEffect and before component is removed. Does not run before the first side effect function execution.
59+
// Clears the timer from the last side effect execution and sets a new on
60+
return () => {clearTimeout(identifier); console.log("Cleanup")}
61+
},[emailIsValid, passwordIsValid])
62+
// this will make sure that the useEffect hook only runs whe the validity of the password or email is updated. Otherwise it would have ran after every password or email change
63+
// which is not ideal
64+
65+
const emailChangeHandler = (event) => {
66+
dispatchEmail({type:"USER_INPUT", val: event.target.value});
67+
// setEnteredEmail(event.target.value);
68+
69+
setFormIsValid(
70+
event.target.value.includes('@') && passwordState.isValid
71+
);
72+
};
73+
74+
const passwordChangeHandler = (event) => {
75+
dispatchPassword({type:"USER_INPUT", val: event.target.value})
76+
// setEnteredPassword(event.target.value);
77+
78+
setFormIsValid(
79+
emailState.isValid && event.target.value.trim().length > 6
80+
);
81+
};
82+
83+
const validateEmailHandler = () => {
84+
// setEmailIsValid(emailState.isValid);
85+
dispatchEmail({type: 'INPUT_BLUR'})
86+
};
87+
88+
const validatePasswordHandler = () => {
89+
// setPasswordIsValid(enteredPassword.trim().length > 6);
90+
dispatchPassword({type: 'INPUT_BLUR'})
91+
92+
};
93+
94+
const submitHandler = (event) => {
95+
event.preventDefault();
96+
authCtx.onLogin(emailState.value, passwordState.value)
97+
};
98+
99+
return (
100+
<Card className={classes.login}>
101+
<form onSubmit={submitHandler}>
102+
<Input
103+
type="email"
104+
label="Email"
105+
id="email"
106+
isValid={emailIsValid}
107+
value={emailState.value}
108+
onChangeHandler={emailChangeHandler}
109+
onBlurHandler={validateEmailHandler}
110+
/>
111+
<Input
112+
type="password"
113+
label="Password"
114+
id="password"
115+
isValid={passwordIsValid}
116+
value={passwordState.value}
117+
onChangeHandler={passwordChangeHandler}
118+
onBlurHandler={validatePasswordHandler}
119+
/>
120+
<div className={classes.actions}>
121+
<Button type="submit" className={classes.btn} disabled={!formIsValid}>
122+
Login
123+
</Button>
124+
</div>
125+
</form>
126+
</Card>
127+
);
128+
};
129+
130+
export default Login;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.login {
2+
width: 90%;
3+
max-width: 40rem;
4+
margin: 2rem auto;
5+
padding: 2rem;
6+
}
7+
8+
.actions {
9+
text-align: center;
10+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React from 'react';
2+
3+
import Navigation from './Navigation';
4+
import classes from './MainHeader.module.css';
5+
6+
const MainHeader = () => {
7+
return (
8+
<header className={classes['main-header']}>
9+
<h1>A Typical Page</h1>
10+
<Navigation />
11+
</header>
12+
);
13+
};
14+
15+
export default MainHeader;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.main-header {
2+
position: fixed;
3+
top: 0;
4+
left: 0;
5+
width: 100%;
6+
height: 5rem;
7+
display: flex;
8+
justify-content: space-between;
9+
align-items: center;
10+
background: #741188;
11+
padding: 0 2rem;
12+
}
13+
14+
.main-header h1 {
15+
color: white;
16+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import React, { useContext } from 'react';
2+
import AuthContext from '../../store/auth-context';
3+
4+
import classes from './Navigation.module.css';
5+
6+
const Navigation = () => {
7+
const ctx = useContext(AuthContext);
8+
return(
9+
<nav className={classes.nav}>
10+
<ul>
11+
{ctx.isLoggedIn && (
12+
<li>
13+
<a href="/">Users</a>
14+
</li>
15+
)}
16+
{ctx.isLoggedIn && (
17+
<li>
18+
<a href="/">Admin</a>
19+
</li>
20+
)}
21+
{ctx.isLoggedIn && (
22+
<li>
23+
<button onClick={ctx.onLogout}>Logout</button>
24+
</li>
25+
)}
26+
</ul>
27+
</nav>
28+
);
29+
};
30+
31+
export default Navigation;

0 commit comments

Comments
 (0)