├── public ├── robots.txt ├── favicon.ico ├── favicon.png ├── logo192.png ├── logo512.png ├── complete.png ├── motoboy.jpeg ├── manifest.json └── index.html ├── src ├── Form │ ├── DatosEntrega │ │ ├── validaciones.js │ │ └── index.js │ ├── DatosUsuario │ │ ├── validaciones.js │ │ └── index.js │ ├── styles.js │ ├── DatosPersonales │ │ ├── validaciones.js │ │ └── index.js │ ├── Complete │ │ └── index.js │ ├── Step │ │ └── index.js │ └── index.js ├── setupTests.js ├── App.test.js ├── Hooks │ └── useAuth.js ├── index.css ├── reportWebVitals.js ├── Stepper │ └── index.js ├── index.js ├── App.js ├── App.css ├── styles.js ├── Context.js └── logo.svg ├── .gitignore ├── package.json └── README.md /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alura-es-cursos/1857-ReactHooks-Context/master/public/favicon.ico -------------------------------------------------------------------------------- /public/favicon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alura-es-cursos/1857-ReactHooks-Context/master/public/favicon.png -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alura-es-cursos/1857-ReactHooks-Context/master/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alura-es-cursos/1857-ReactHooks-Context/master/public/logo512.png -------------------------------------------------------------------------------- /public/complete.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alura-es-cursos/1857-ReactHooks-Context/master/public/complete.png -------------------------------------------------------------------------------- /public/motoboy.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alura-es-cursos/1857-ReactHooks-Context/master/public/motoboy.jpeg -------------------------------------------------------------------------------- /src/Form/DatosEntrega/validaciones.js: -------------------------------------------------------------------------------- 1 | export const validarInput = (input) => { 2 | return input.length > 3 ? true : false; 3 | }; 4 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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/Hooks/useAuth.js: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect } from "react"; 2 | 3 | const useAuth = (jwt) => { 4 | const [isAuth, setIsAuth] = useState(true); 5 | 6 | useEffect(() => { 7 | if (jwt.length > 25) { 8 | setIsAuth(true); 9 | } else { 10 | setIsAuth(false); 11 | } 12 | }, []); 13 | 14 | return isAuth; 15 | }; 16 | 17 | export default useAuth; 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /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/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/Form/DatosUsuario/validaciones.js: -------------------------------------------------------------------------------- 1 | export const validarEmail = (email) => { 2 | const length = email.length; 3 | if (length > 8 && length < 50 && email.includes("@")) { 4 | return true; 5 | } else { 6 | return false; 7 | } 8 | }; 9 | 10 | export function validarPassword(password) { 11 | const length = password.length; 12 | if (length >= 8 && length < 20) { 13 | return true; 14 | } else { 15 | return false; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/Form/styles.js: -------------------------------------------------------------------------------- 1 | import styled from "styled-components"; 2 | 3 | export const MainSpace = styled.div` 4 | width: 100%; 5 | `; 6 | 7 | export const FormSpace = styled.div` 8 | width: 100%; 9 | `; 10 | 11 | export const LogoSpace = styled.div` 12 | display: flex; 13 | align-items: center; 14 | justify-content: center; 15 | padding-bottom: 5rem; 16 | `; 17 | 18 | export const Img = styled.img` 19 | width: 50px; 20 | height: 50px; 21 | `; 22 | -------------------------------------------------------------------------------- /src/Form/DatosPersonales/validaciones.js: -------------------------------------------------------------------------------- 1 | export const validarNombre = (nombre) => { 2 | const length = nombre.length; 3 | return length > 1 && length < 30 ? true : false; 4 | }; 5 | 6 | export const validarApellidos = (apellidos) => { 7 | const length = apellidos.length; 8 | return length > 1 && length < 50 ? true : false; 9 | }; 10 | 11 | export const validarTelefono = (telefono) => { 12 | const length = telefono.length; 13 | return length >= 8 && length <= 14 ? true : false; 14 | }; 15 | -------------------------------------------------------------------------------- /src/Stepper/index.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { Box, Stepper, Step, StepLabel } from "@mui/material"; 3 | 4 | const StepperComponent = (props) => { 5 | const steps = ["Datos de usuario", "Datos personales", "Datos de entrega"]; 6 | 7 | return ( 8 | 9 | 10 | {steps.map((step) => ( 11 | 12 | {step} 13 | 14 | ))} 15 | 16 | 17 | ); 18 | }; 19 | 20 | export default StepperComponent; 21 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /src/Form/Complete/index.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import styled from "styled-components"; 3 | import { Box, Typography } from "@mui/material"; 4 | 5 | const Img = styled.img` 6 | width: 70%; 7 | `; 8 | 9 | const Complete = () => { 10 | return ( 11 | 19 | !Gracias por tu registro¡ 20 | 21 | 22 | ); 23 | }; 24 | 25 | export default Complete; 26 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import ReactDOM from "react-dom"; 3 | import "./index.css"; 4 | import App from "./App"; 5 | import reportWebVitals from "./reportWebVitals"; 6 | import { CounterProvider } from "./Context"; 7 | 8 | ReactDOM.render( 9 | 10 | 11 | 12 | 13 | , 14 | document.getElementById("root") 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/App.js: -------------------------------------------------------------------------------- 1 | import { useContext } from "react"; 2 | import { MainSpace, ImageSpace, FormSpace } from "./styles"; 3 | import { Button } from "@mui/material"; 4 | import Form from "./Form"; 5 | import { CounterContext } from "./Context"; 6 | 7 | function App() { 8 | const counterData = useContext(CounterContext); 9 | console.log(counterData); 10 | return ( 11 | 12 | 13 | 14 |
15 |
16 | 17 | 18 |
19 | 20 | 21 | ); 22 | } 23 | 24 | export default App; 25 | -------------------------------------------------------------------------------- /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/styles.js: -------------------------------------------------------------------------------- 1 | import styled from "styled-components"; 2 | 3 | export const MainSpace = styled.div` 4 | width: 100vw; 5 | height: 100vh; 6 | display: flex; 7 | align-items: center; 8 | justify-content: space-between; 9 | `; 10 | 11 | export const ImageSpace = styled.div` 12 | width: 55vw; 13 | height: 100vh; 14 | background-image: url("/motoboy.jpeg"); 15 | background-repeat: no-repeat; 16 | background-size: cover; 17 | background-position: center; 18 | @media (max-width: 768px) { 19 | display: none; 20 | } 21 | `; 22 | 23 | export const FormSpace = styled.div` 24 | width: 45vw; 25 | height: 100vh; 26 | display: flex; 27 | flex-direction: column; 28 | @media (max-width: 768px) { 29 | width: 100vw; 30 | } 31 | `; 32 | -------------------------------------------------------------------------------- /src/Context.js: -------------------------------------------------------------------------------- 1 | import React, { useState, createContext } from "react"; 2 | 3 | export const CounterContext = createContext(); 4 | 5 | export const CounterProvider = ({ children }) => { 6 | const [count, setCount] = useState(0); 7 | const values = { 8 | count, 9 | suma() { 10 | setCount((val) => val + 1); 11 | }, 12 | resta() { 13 | setCount((val) => val - 1); 14 | }, 15 | user: { 16 | status: "Online", 17 | jwt: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJzdGF0dXMiOiJvbmxpbmUifQ.ndsqj1chCCgt-EBeuiFZGd8W9_wYn0RCMkHziKZY3LQ", 18 | theme: "light", 19 | }, 20 | }; 21 | 22 | return ( 23 | {children} 24 | ); 25 | }; 26 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "1857-reacthooks-context", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@emotion/react": "^11.4.1", 7 | "@emotion/styled": "^11.3.0", 8 | "@mui/material": "^5.0.4", 9 | "@mui/styled-engine-sc": "^5.0.3", 10 | "@testing-library/jest-dom": "^5.11.4", 11 | "@testing-library/react": "^11.1.0", 12 | "@testing-library/user-event": "^12.1.10", 13 | "react": "^17.0.2", 14 | "react-dom": "^17.0.2", 15 | "react-scripts": "^5.0.1", 16 | "styled-components": "^5.3.1", 17 | "web-vitals": "^1.0.1" 18 | }, 19 | "scripts": { 20 | "start": "react-scripts start ", 21 | "build": "react-scripts build", 22 | "test": "react-scripts test", 23 | "eject": "react-scripts eject" 24 | }, 25 | "eslintConfig": { 26 | "extends": [ 27 | "react-app", 28 | "react-app/jest" 29 | ] 30 | }, 31 | "browserslist": { 32 | "production": [ 33 | ">0.2%", 34 | "not dead", 35 | "not op_mini all" 36 | ], 37 | "development": [ 38 | "last 1 chrome version", 39 | "last 1 firefox version", 40 | "last 1 safari version" 41 | ] 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Form/Step/index.js: -------------------------------------------------------------------------------- 1 | import React, { useState, useContext } from "react"; 2 | import { TextField, Button, Box } from "@mui/material"; 3 | import { CounterContext } from "../../Context"; 4 | import useAuth from "../../Hooks/useAuth"; 5 | 6 | const Step = ({ data, step, pasos }) => { 7 | const { inputs, buttonText, onSubmit } = data; 8 | 9 | const counterData = useContext(CounterContext); 10 | 11 | const access = useAuth("counterData.user.jwt"); 12 | console.log(access); 13 | 14 | return ( 15 | onSubmit(e, step, pasos)} 25 | > 26 | El valor del contador es: {counterData.count} 27 | {inputs.map((input, i) => { 28 | const { label, type, value, valid, onChange, helperText, validator } = 29 | input; 30 | 31 | return ( 32 | onChange(e, i, step, validator, pasos)} 43 | /> 44 | ); 45 | })} 46 | 47 | 50 | 51 | ); 52 | }; 53 | 54 | export default Step; 55 | -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 14 | 18 | 22 | 23 | 32 | AluraFood 33 | 34 | 35 | 36 |
37 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /src/Form/DatosUsuario/index.js: -------------------------------------------------------------------------------- 1 | import React, { useState } from "react"; 2 | import { TextField, Button, Box } from "@mui/material"; 3 | import { validarEmail, validarPassword } from "./validaciones"; 4 | 5 | const DatosUsuario = ({ updateStep }) => { 6 | const [email, setEmail] = useState({ 7 | value: "", 8 | valid: null, 9 | }); 10 | const [password, setPassword] = useState({ value: "", valid: null }); 11 | 12 | return ( 13 | { 23 | e.preventDefault(); 24 | if (email.valid && password.valid) { 25 | console.log("Siguiente formulario"); 26 | console.log(email, password); 27 | updateStep(1); 28 | } else { 29 | console.log("No hacer nada"); 30 | } 31 | }} 32 | > 33 | { 45 | const email = input.target.value; 46 | const valido = validarEmail(email); 47 | setEmail({ value: email, valid: valido }); 48 | }} 49 | /> 50 | { 63 | const password = input.target.value; 64 | setPassword({ value: password, valid: validarPassword(password) }); 65 | }} 66 | /> 67 | 70 | 71 | ); 72 | }; 73 | 74 | export default DatosUsuario; 75 | -------------------------------------------------------------------------------- /src/logo.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/Form/DatosEntrega/index.js: -------------------------------------------------------------------------------- 1 | import React, { useState } from "react"; 2 | import { TextField, Button, Box } from "@mui/material"; 3 | import { validarInput } from "./validaciones"; 4 | 5 | const DatosEntrega = ({ updateStep }) => { 6 | const [address, setAddress] = useState({ value: "", valid: null }); 7 | const [city, setCity] = useState({ value: "", valid: null }); 8 | const [province, setProvince] = useState({ value: "", valid: null }); 9 | 10 | return ( 11 | { 21 | e.preventDefault(); 22 | updateStep(3); 23 | console.log(address, city, province); 24 | }} 25 | > 26 | { 34 | const value = input.target.value; 35 | const valid = validarInput(value); 36 | setAddress({ value, valid }); 37 | }} 38 | error={address.valid === false} 39 | helperText={address.valid === false && "Ingresa al menos 4 caracteres."} 40 | /> 41 | { 49 | const value = input.target.value; 50 | const valid = validarInput(value); 51 | setCity({ value, valid }); 52 | }} 53 | error={city.valid === false} 54 | helperText={city.valid === false && "Ingresa al menos 4 caracteres."} 55 | /> 56 | { 64 | const value = input.target.value; 65 | const valid = validarInput(value); 66 | setProvince({ value, valid }); 67 | }} 68 | error={province.valid === false} 69 | helperText={ 70 | province.valid === false && "Ingresa al menos 4 caracteres." 71 | } 72 | /> 73 | 76 | 77 | ); 78 | }; 79 | 80 | export default DatosEntrega; 81 | -------------------------------------------------------------------------------- /src/Form/DatosPersonales/index.js: -------------------------------------------------------------------------------- 1 | import React, { useState } from "react"; 2 | import { TextField, Button, Box } from "@mui/material"; 3 | import { 4 | validarNombre, 5 | validarApellidos, 6 | validarTelefono, 7 | } from "./validaciones"; 8 | 9 | const DatosPersonales = ({ updateStep }) => { 10 | const [name, setName] = useState({ value: "", valid: null }); 11 | const [lastName, setLastName] = useState({ value: "", valid: null }); 12 | const [phone, setPhone] = useState({ value: "", valid: null }); 13 | 14 | return ( 15 | { 25 | e.preventDefault(); 26 | updateStep(2); 27 | }} 28 | > 29 | { 37 | const value = input.target.value; 38 | const valid = validarNombre(value); 39 | setName({ value, valid }); 40 | console.log(value, valid); 41 | }} 42 | error={name.valid === false} 43 | helperText={ 44 | name.valid === false && 45 | "Ingresa al menos 2 caracteres y máximo 30 caracteres." 46 | } 47 | /> 48 | { 56 | const value = input.target.value; 57 | const valid = validarApellidos(value); 58 | setLastName({ value, valid }); 59 | console.log(value, valid); 60 | }} 61 | error={lastName.valid === false} 62 | helperText={ 63 | lastName.valid === false && 64 | "Ingresa al menos 2 caracteres y máximo 50 caracteres." 65 | } 66 | /> 67 | { 76 | const value = input.target.value; 77 | const valid = validarTelefono(value); 78 | setPhone({ value, valid }); 79 | console.log(value, valid); 80 | }} 81 | error={phone.valid === false} 82 | helperText={ 83 | phone.valid === false && 84 | "Ingresa al menos 8 digitos y máximo 14 digitos." 85 | } 86 | /> 87 | 90 | 91 | ); 92 | }; 93 | 94 | export default DatosPersonales; 95 | -------------------------------------------------------------------------------- /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 | ### `yarn start` 10 | 11 | Runs the app in the development mode.\ 12 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 13 | 14 | The page will reload if you make edits.\ 15 | You will also see any lint errors in the console. 16 | 17 | ### `yarn 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 | ### `yarn 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 | ### `yarn 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 | ### `yarn 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 | -------------------------------------------------------------------------------- /src/Form/index.js: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect } from "react"; 2 | import { Box, Typography } from "@mui/material"; 3 | import { LogoSpace, FormSpace, Img } from "./styles"; 4 | import DatosUsuario from "./DatosUsuario"; 5 | import DatosPersonales from "./DatosPersonales"; 6 | import DatosEntrega from "./DatosEntrega"; 7 | import Complete from "./Complete"; 8 | import Stepper from "../Stepper"; 9 | import Step from "./Step"; 10 | 11 | //Validaciones 12 | import { validarEmail, validarPassword } from "./DatosUsuario/validaciones"; 13 | import { 14 | validarNombre, 15 | validarApellidos, 16 | validarTelefono, 17 | } from "./DatosPersonales/validaciones"; 18 | import { validarInput } from "./DatosEntrega/validaciones"; 19 | 20 | const Form = () => { 21 | const [step, setStep] = useState(0); 22 | const [pasos, setPasos] = useState({}); 23 | 24 | const onSubmit = (e, step, pasos) => { 25 | console.log(step); 26 | e.preventDefault(); 27 | let newStep = step + 1; 28 | console.log(newStep); 29 | setStep(newStep); 30 | if (newStep === 3) { 31 | console.log("Eviar datos al backend", pasos); 32 | } 33 | }; 34 | 35 | const handleChange = (element, position, currentStep, validator, pasos) => { 36 | const value = element.target.value; 37 | const valid = validator(value); 38 | const cp = { ...pasos }; 39 | cp[currentStep].inputs[position].value = value; 40 | cp[currentStep].inputs[position].valid = valid; 41 | 42 | setPasos(cp); 43 | }; 44 | 45 | const stepsFlow = { 46 | 0: { 47 | inputs: [ 48 | { 49 | label: "Correo electrónico", 50 | type: "email", 51 | value: "", 52 | valid: null, 53 | onChange: handleChange, 54 | helperText: "Ingresa un correo electrónico válido.", 55 | validator: validarEmail, 56 | }, 57 | { 58 | label: "Contraseña", 59 | type: "password", 60 | value: "", 61 | valid: null, 62 | onChange: handleChange, 63 | helperText: 64 | "Ingresa una contraseña válida, Al menos 8 caracteres y máximo 20.", 65 | validator: validarPassword, 66 | }, 67 | { 68 | label: "Cuenta de github", 69 | type: "text", 70 | value: "", 71 | valid: null, 72 | onChange: handleChange, 73 | helperText: 74 | "Ingresa una contraseña válida, Al menos 8 caracteres y máximo 20.", 75 | validator: validarPassword, 76 | }, 77 | ], 78 | buttonText: "Siguiente", 79 | onSubmit, 80 | }, 81 | 1: { 82 | inputs: [ 83 | { 84 | label: "Nombre", 85 | type: "text", 86 | value: "", 87 | valid: null, 88 | onChange: handleChange, 89 | helperText: "Ingresa al menos 2 caracteres y máximo 30 caracteres.", 90 | validator: validarNombre, 91 | }, 92 | { 93 | label: "Apellidos", 94 | type: "text", 95 | value: "", 96 | valid: null, 97 | onChange: handleChange, 98 | helperText: "Ingresa al menos 2 caracteres y máximo 50 caracteres.", 99 | validator: validarApellidos, 100 | }, 101 | { 102 | label: "Número telefonico", 103 | type: "number", 104 | value: "", 105 | valid: null, 106 | onChange: handleChange, 107 | helperText: "Ingresa al menos 8 digitos y máximo 14 digitos.", 108 | validator: validarTelefono, 109 | }, 110 | ], 111 | buttonText: "Siguiente", 112 | onSubmit, 113 | }, 114 | 2: { 115 | inputs: [ 116 | { 117 | label: "Direccion", 118 | type: "text", 119 | value: "", 120 | valid: null, 121 | onChange: handleChange, 122 | helperText: "Ingresa al menos 4 caracteres.", 123 | validator: validarInput, 124 | }, 125 | { 126 | label: "Ciudad", 127 | type: "text", 128 | value: "", 129 | valid: null, 130 | onChange: handleChange, 131 | helperText: "Ingresa al menos 4 caracteres.", 132 | validator: validarInput, 133 | }, 134 | { 135 | label: "Estado/Provincia", 136 | type: "text", 137 | value: "", 138 | valid: null, 139 | onChange: handleChange, 140 | helperText: "Ingresa al menos 4 caracteres.", 141 | validator: validarInput, 142 | }, 143 | ], 144 | buttonText: "Crear cuenta", 145 | onSubmit, 146 | }, 147 | }; 148 | 149 | useEffect(() => { 150 | setPasos(stepsFlow); 151 | }, []); 152 | 153 | // useEffect(async () => { 154 | // try { 155 | // const data = await fetch("https://jsonplaceholder.typicode.com/posts"); 156 | // const posts = await data.json(); 157 | // console.log(posts); 158 | // } catch (e) { 159 | // console.log(e); 160 | // } 161 | // }); 162 | 163 | //step = 0 --> 164 | //step = 1 --> 165 | //step = 2 --> 166 | //step = 3 --> 167 | 168 | const updateStep = (step) => { 169 | setStep(step); 170 | }; 171 | 172 | const steps = { 173 | 0: , 174 | 1: , 175 | 2: , 176 | 3: , 177 | }; 178 | 179 | return ( 180 | 187 | 188 | 189 | AluraFood 190 | 191 | 192 | {step < 3 && } 193 | {/* {steps[step]} */} 194 | {step < 3 && pasos[step] && ( 195 | 196 | )} 197 | {step === 3 && } 198 | 199 | 200 | ); 201 | }; 202 | 203 | export default Form; 204 | --------------------------------------------------------------------------------