Skip to content

Commit

Permalink
Adding Examples
Browse files Browse the repository at this point in the history
  • Loading branch information
svwoodhouse committed Dec 18, 2022
1 parent c0a8777 commit d94e843
Show file tree
Hide file tree
Showing 26 changed files with 29,094 additions and 0 deletions.
28,500 changes: 28,500 additions & 0 deletions Sections/Section 10/01-starting-project/package-lock.json

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions Sections/Section 10/01-starting-project/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "react-complete-guide",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.11.6",
"@testing-library/react": "^11.2.2",
"@testing-library/user-event": "^12.5.0",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"react-scripts": "^5.0.1",
"web-vitals": "^0.2.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Binary file not shown.
43 changes: 43 additions & 0 deletions Sections/Section 10/01-starting-project/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions Sections/Section 10/01-starting-project/public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
3 changes: 3 additions & 0 deletions Sections/Section 10/01-starting-project/public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:
20 changes: 20 additions & 0 deletions Sections/Section 10/01-starting-project/src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React, { useContext } from 'react';

import Login from './components/Login/Login';
import Home from './components/Home/Home';
import MainHeader from './components/MainHeader/MainHeader';
import AuthContext from './store/auth-context';

function App() {
const ctx = useContext(AuthContext)
return (
<React.Fragment>
<MainHeader/>
<main>
{!ctx.isLoggedIn && <Login/>}
{ctx.isLoggedIn && <Home />}
</main>
</React.Fragment> );
}

export default App;
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from 'react';
import AuthContext from '../../store/auth-context';
import { useContext } from 'react';
import Card from '../UI/Card/Card';
import classes from './Home.module.css';
import Button from '../UI/Button/Button';

const Home = () => {
const authCtx = useContext(AuthContext)
return (
<Card className={classes.home}>
<h1>Welcome back!</h1>
<Button onClick={authCtx.onLogout}>Logout</Button>
</Card>
);
};

export default Home;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.home {
width: 90%;
max-width: 40rem;
padding: 3rem;
margin: 2rem auto;
text-align: center;
}
130 changes: 130 additions & 0 deletions Sections/Section 10/01-starting-project/src/components/Login/Login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import React, { useContext, useEffect, useReducer, useState } from 'react';

import Card from '../UI/Card/Card';
import classes from './Login.module.css';
import Button from '../UI/Button/Button';
import AuthContext from '../../store/auth-context';
import Input from '../UI/Input/Input';

// For useReducer Example
const emailReducer = (state, action) => {
if(action.type === 'USER_INPUT') {
return {value: action.val, isValid: action.val.includes('@')}
}
if(action.type === 'INPUT_BLUR') {
return {value: state.value, isValid: state.value.includes('@')}

}
return {value:'', isValid: false}
}

const passwordReducer = (state, action) => {
if(action.type === "USER_INPUT"){
return { value: action.val, isValid: action.val.trim().length > 6 }
}

if(action.type === 'INPUT_BLUR'){
return {value: state.value, isValid: state.value.trim().length > 6}
}
}

const Login = () => {
// This is for the useEffect Example
// const [enteredEmail, setEnteredEmail] = useState('');
// const [emailIsValid, setEmailIsValid] = useState();
// const [enteredPassword, setEnteredPassword] = useState('');
// const [passwordIsValid, setPasswordIsValid] = useState();
const [formIsValid, setFormIsValid] = useState(false);

// This is for useReducer Example
const [emailState, dispatchEmail] = useReducer(emailReducer,{value:'', isValid: false})
const [passwordState, dispatchPassword] = useReducer(passwordReducer,{value:'', isValid: false})

// useEffect(() => {console.log('Effect Running')}, [])
// console.log("Component Rendering...")
// To reduce unneccessary useEffect
// Desctructs the object and adds an alias to the destructured values. We use these object properties as dependencies in the useEffect hook
const { isValid: emailIsValid } = emailState
const { isValid: passwordIsValid } = passwordState

const authCtx = useContext(AuthContext)

useEffect(()=>{
const identifier = setTimeout(() => {
// We only care about whether the isValid state updates
setFormIsValid(emailIsValid && passwordIsValid)
},500)

// Clean Up function that runs before useEffect and before component is removed. Does not run before the first side effect function execution.
// Clears the timer from the last side effect execution and sets a new on
return () => {clearTimeout(identifier); console.log("Cleanup")}
},[emailIsValid, passwordIsValid])
// 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
// which is not ideal

const emailChangeHandler = (event) => {
dispatchEmail({type:"USER_INPUT", val: event.target.value});
// setEnteredEmail(event.target.value);

setFormIsValid(
event.target.value.includes('@') && passwordState.isValid
);
};

const passwordChangeHandler = (event) => {
dispatchPassword({type:"USER_INPUT", val: event.target.value})
// setEnteredPassword(event.target.value);

setFormIsValid(
emailState.isValid && event.target.value.trim().length > 6
);
};

const validateEmailHandler = () => {
// setEmailIsValid(emailState.isValid);
dispatchEmail({type: 'INPUT_BLUR'})
};

const validatePasswordHandler = () => {
// setPasswordIsValid(enteredPassword.trim().length > 6);
dispatchPassword({type: 'INPUT_BLUR'})

};

const submitHandler = (event) => {
event.preventDefault();
authCtx.onLogin(emailState.value, passwordState.value)
};

return (
<Card className={classes.login}>
<form onSubmit={submitHandler}>
<Input
type="email"
label="Email"
id="email"
isValid={emailIsValid}
value={emailState.value}
onChangeHandler={emailChangeHandler}
onBlurHandler={validateEmailHandler}
/>
<Input
type="password"
label="Password"
id="password"
isValid={passwordIsValid}
value={passwordState.value}
onChangeHandler={passwordChangeHandler}
onBlurHandler={validatePasswordHandler}
/>
<div className={classes.actions}>
<Button type="submit" className={classes.btn} disabled={!formIsValid}>
Login
</Button>
</div>
</form>
</Card>
);
};

export default Login;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.login {
width: 90%;
max-width: 40rem;
margin: 2rem auto;
padding: 2rem;
}

.actions {
text-align: center;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react';

import Navigation from './Navigation';
import classes from './MainHeader.module.css';

const MainHeader = () => {
return (
<header className={classes['main-header']}>
<h1>A Typical Page</h1>
<Navigation />
</header>
);
};

export default MainHeader;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.main-header {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 5rem;
display: flex;
justify-content: space-between;
align-items: center;
background: #741188;
padding: 0 2rem;
}

.main-header h1 {
color: white;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React, { useContext } from 'react';
import AuthContext from '../../store/auth-context';

import classes from './Navigation.module.css';

const Navigation = () => {
const ctx = useContext(AuthContext);
return(
<nav className={classes.nav}>
<ul>
{ctx.isLoggedIn && (
<li>
<a href="/">Users</a>
</li>
)}
{ctx.isLoggedIn && (
<li>
<a href="/">Admin</a>
</li>
)}
{ctx.isLoggedIn && (
<li>
<button onClick={ctx.onLogout}>Logout</button>
</li>
)}
</ul>
</nav>
);
};

export default Navigation;
Loading

0 comments on commit d94e843

Please sign in to comment.