Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test #235

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

test #235

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 79 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
"@testing-library/jest-dom": "^5.16.2",
"@testing-library/react": "^12.1.3",
"@testing-library/user-event": "^13.5.0",
"axios": "^0.26.0",
"axios": "^0.26.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-router-dom": "^6.8.1",
"react-scripts": "5.0.0"
"react-scripts": "5.0.0",
"yup": "^1.2.0"
},
"scripts": {
"start": "react-scripts start",
Expand Down
101 changes: 96 additions & 5 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,102 @@
import React from "react";
import React,{useState}from "react";
import { Route,Routes, Link } from "react-router-dom";
import axios from "axios";

import schema from './comps/formSchema'
import * as yup from 'yup'

import Form from "./comps/Form";
import Home from "./comps/Home";

const intialvalues = {
name: "",
topping1: false,
topping2: false,
topping3: false,
topping4: false,
size: "",
specialText: ""
}

const intialErrors = {
name: "",
topping1: "",
topping2: "",
topping3: "",
topping4: "",
size: "",
specialText: ""

}
const App = () => {
const [formValues,setFormvalues] = useState(intialvalues)
const [formErrors,setFormErrors] = useState(intialErrors)
const [users,setUsers] = useState([])




const handleChange = (name,value) => {
setFormvalues({...formValues, [name]: value})
formValidate(name,value)

}

const formValidate = (name,value) => {
yup.reach(schema,name)
.validate(value)
.then(() => setFormErrors({...formErrors, [name]: ""}))
.catch(err => setFormErrors({
...formErrors, [name]: err.errors[0]}))
}


const handleSubmit = () => {
axios.post("https://reqres.in/api/orders", formValues)
.then(res => {
setUsers([res.data,...users])
})
.catch(err => console.error(err))
}





return (
<>
<h1>Lambda Eats</h1>
<p>You can remove this code and create your own header</p>
</>
<><p>{formErrors.name}</p>
<h1>Bloomtech Eats</h1>
<i>Welcome to the best Pizza spot in the DMV!!!</i>
<button> Carry Out </button> <button> Delivery</button>

<nav>
<Link to="/"> Home </Link>
<Link to="/pizza"> <button id="order-pizza" > Order Now!!!</button> </Link>
<Link to="/"> Images</Link>
</nav>


<Routes>
<Route path="/" element={<Home />}/>
<Route path="/pizza" element={ <Form values={formValues}
change={handleChange}
errors={formErrors}
submit={handleSubmit} />}/>
</Routes>
{users.map(user => (
<div key={user.id}>
<p>{user.name}</p>
<p>{user.size}</p>
<p>{user.topping1}</p>
<p>{user.topping2}</p>
<p>{user.topping3}</p>
<p>{user.topping4}</p>
</div>
))}



</>
);
};
export default App;
71 changes: 71 additions & 0 deletions src/comps/Form.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import React from 'react'



function Form(props) {
const {change,submit} = props
const {name,size,topping1,topping2,topping3,topping4,specialText} = props.values

const onChange = (event => {
const {checked,value,name,type} = event.target
const valueToUse = type === 'checkbox' ? checked : value
change(name, valueToUse)
});

const onSubmit = (e) => {
e.preventDefault();
submit();
}




return (
<div>

<h1> Bloomtech Eats</h1>
<form id='pizza-form' onSubmit={onSubmit} >
<label> Name
<input
type="text"
id="name-input"
name="name"
value={name}
onChange={onChange}
/>
</label><br/>

<label> Pick your Size
<select id="size-dropdown" type="" name="size" value={size} onChange={onChange}>
<option value="0"> -- Select a size--</option>
<option value="1"> Small</option>
<option value="2"> Medium</option>
<option value="3"> Large</option>
<option value="4"> X-large</option>
</select><br/>
</label>
<h3> Add your toppings </h3>
<label> Toppings
<input type="checkbox" />
<input type="checkbox" name="topping1"checked={topping1} onChange={onChange}/>
<input type="checkbox" name="topping2" checked={topping2} onChange={onChange}/>
<input type="checkbox" name="topping3" checked={topping3} onChange={onChange}/>
<input type="checkbox" name="topping4" checked={topping4} onChange={onChange}/>
</label>




<br/>

<label id='special-text'> Special instructions <br/>
<input name="specialText" type="text" value={specialText} onChange={onChange}/>
</label><br />
<input type="submit" value="create a friend" onChange={onChange} id="order-button" />
</form>
</div>
)
}


export default Form
9 changes: 9 additions & 0 deletions src/comps/Home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react'

function Home() {
return (
<div>Home</div>
)
}

export default Home
16 changes: 16 additions & 0 deletions src/comps/formSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as yup from "yup"

const formschema = yup.object().shape({
name: yup.string().required("name must be at least 2 characters").min(2, "name must be at least 2 characters"),
size: yup.string().oneOf(["1","2","3","4"], 'you must chose your size'),
topping1: yup.boolean(),
topping2: yup.boolean(),
topping3: yup.boolean(),
topping4: yup.boolean(),
specialText: yup.string()



})

export default formschema
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import { BrowserRouter } from "react-router-dom";

ReactDOM.render(<BrowserRouter><App /></BrowserRouter>, document.getElementById("root"));

ReactDOM.render(<App />, document.getElementById("root"));