Skip to content

Commit

Permalink
postOrder
Browse files Browse the repository at this point in the history
  • Loading branch information
lyandrasaito committed Oct 10, 2021
1 parent 7f2d1d1 commit 6aea165
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/components/cart/cart.js
Expand Up @@ -8,7 +8,7 @@ export default function Cart({ className, key, id, name, price, flavor, compleme
<div className={className} key={key} id={id} price={price} qtd={qtd}>
<div>
<h1 className="" > {id} {name}</h1>
<img src={img} className="productsImage"></img>
<img src={img} className="productsImage" alt="banana"></img>
</div>
<div className="">
Valor unitário: R${price},00 |
Expand Down
2 changes: 1 addition & 1 deletion src/components/menu/menu.js
Expand Up @@ -7,7 +7,7 @@ export default function Menu({ onClick, className, key, id, name, price, flavor,
<>
<div className={className} key={key} id={id} price={price}>
<h1 className="" > {id} {name}</h1>
<img src={img} className="productsImage"></img>
<img src={img} className="productsImage" alt="banana"></img>

<h1 className="" > R${price},00</h1>
<h1 className=""> {flavor}</h1>
Expand Down
45 changes: 28 additions & 17 deletions src/pages/hall/hall.js
Expand Up @@ -27,16 +27,13 @@ function Hall() {
).then((json) => {
setProducts(json)
})
},[token]);
});

useEffect(() => {
console.log(table, order,client);
},[table, order, client])

// const validationOrder = () => {
// let error = {}
// error.isFormValid = true

// if (!client) {
// error.client = 'Preencha o nomedo cliente corretamente'
// error.isFormValid = false
Expand All @@ -50,29 +47,43 @@ function Hall() {
// accumulator = Number(qtd * price + accumulator)
// return accumulator
// }, 0)

// return totalPrice;
// }


const handleSubmit = (e) => {
e.preventDefault();
// const valid = validationOrder();
// if(valid.isFormValid){
const sendOrder = ({

const pedido = ({
"client": client,
"table": table,
"products": order.map((item) => ([{
"id":Number(item.id),
"qdt":Number(item.qtd)
}]))

"products":
order.map((item) => (
{
id: Number(item.id),
qtd: Number(item.qtd),
}))

})
postOrder(sendOrder)
// }
console.log(sendOrder);

postOrder(pedido);

// fetch('https://lab-api-bq.herokuapp.com/orders', {
// method: 'POST',
// headers: {
// "Content-Type": "application/json",
// 'Authorization': `${token}`,
// },
// body: JSON.stringify(pedido)
// })
// // }
// console.log(pedido);
}


const onChangeClient = (e) => {
const name = e.target.value;
setClient(name);
Expand Down Expand Up @@ -159,7 +170,7 @@ function Hall() {
<div className="hall">
<section className="">
<h1>Carrinho</h1>

<Input
className=""
placeholder="Nome do cliente: "
Expand Down
2 changes: 2 additions & 0 deletions src/pages/login/useForm.js
Expand Up @@ -41,6 +41,8 @@ const useForm = () => {
} else {
localStorage.setItem('token', response.token);
localStorage.setItem('id', response.id);

console.log(response.token)

if (response.role === "hall") {
history.push('/hall')
Expand Down
27 changes: 20 additions & 7 deletions src/services/postAPI.js
@@ -1,13 +1,26 @@
export const postOrder = async (values) => {
// export const postOrder = async (values) => {
// const token = localStorage.getItem('token');
// return await fetch('https://lab-api-bq.herokuapp.com/orders', {
// method: "POST",
// headers: {
// 'Accept': 'application/json',
// "Content-Type": "application/json",
// 'Authorization': `${token}`,
// },
// body: JSON.stringify(values)
// }).then(res => res.json())
// .catch(e => console.log(e))
// };

export const postOrder = async (pedido) => {
const token = localStorage.getItem('token');
return await fetch('https://lab-api-bq.herokuapp.com/orders', {
method: "POST",
method: 'POST',
headers: {
'Accept': 'application/json',
"Content-Type": "application/json",
'Authorization': `${token}`,
},
body: JSON.stringify(values)
}).then(res => res.json())
.catch(e => console.log(e))
};
body: JSON.stringify(pedido)
})
}

0 comments on commit 6aea165

Please sign in to comment.