Skip to main content

12.08 Order Submission And Passing Data Between Pages

BurgerBuilder.js

BurgerBuilder.js

...

const queryParams = [];
for (let i in this.state.ingredients) {
queryParams.push(
encodeURIComponent(i) +
"=" +
encodeURIComponent(this.state.ingredients[i])
);
}
queryParams.push("price=" + this.state.totalPrice);
const queryString = queryParams.join("&");

this.props.history.push({
pathname: "/checkout",
search: "?" + queryString,
});
};
render() {

...

Checkout.js

Checkout.js
import React, { Component } from "react";
import { Route } from "react-router-dom";
import CheckoutSummary from "../../components/Order/CheckoutSummary/CheckoutSummary";
import ContactData from "./ContactData/ContactData";

class Checkout extends Component {
state = {
ingredients: null,
price: 0,
};

UNSAFE_componentWillMount() {
const query = new URLSearchParams(this.props.location.search);
const ingredients = {};
let price = 0;
for (let param of query.entries()) {
if (param[0] === "price") {
price = param[1];
} else {
ingredients[param[0]] = +param[1];
}
}
this.setState({ ingredients: ingredients, totalPrice: price });
}

checkoutCancelledHandler = () => {
this.props.history.goBack();
};

checkoutContinuedHandler = () => {
this.props.history.replace("/checkout/contact-data");
};
render() {
return (
<div>
<CheckoutSummary
ingredients={this.state.ingredients}
checkoutCancelled={this.checkoutCancelledHandler}
checkoutContinued={this.checkoutContinuedHandler}
/>
<Route
path={this.props.match.path + "/contact-data"}
render={(props) => (
<ContactData
ingredients={this.state.ingredients}
price={this.state.totalPrice}
{...props}
/>
)}
/>
</div>
);
}
}
export default Checkout;

ContactData.js

caution

I got the ingredients successfully and again, preventDefault is important on the orderHandler on the event you're getting automatically to prevent the default which would be to send the request and reload the page.

ContactData.js
import React, { Component } from "react";
import axios from "../../../axios-orders";
import Button from "../../../components/UI/Button/Button";
import Spinner from "../../../components/UI/Spinner/Spinner";
import classes from "./ContactData.module.css";

class ContactData extends Component {
state = {
name: "",
email: "",
address: {
street: "",
postalCode: "",
},
loading: false,
};

orderHandler = (event) => {
event.preventDefault();
this.setState({ loading: true });
const order = {
ingredients: this.props.ingredients,
price: this.props.price,
customer: {
name: "Pash Pa",
address: {
street: "Teststreet 1",
zipCode: "34234",
country: "Russia",
},
email: "test@test.com",
},
deliveryMethod: "fastest",
};
axios
.post("/orders.json", order)
.then((response) => {
this.setState({ loading: false });
this.props.history.push("/");
})
.catch((error) => {
this.setState({ loading: false });
});
};
render() {
let form = (
<form>
<input
className={classes.Input}
type="text"
name="name"
placeholder="Your Name"
/>
<input
className={classes.Input}
type="text"
name="email"
placeholder="Your Mail"
/>
<input
className={classes.Input}
type="text"
name="street"
placeholder="Street"
/>
<input
className={classes.Input}
type="text"
name="postal"
placeholder="Postal Code"
/>
<Button btnType="Success" clicked={this.orderHandler}>
ORDER
</Button>
</form>
);
if (this.state.loading) {
form = <Spinner />;
}
return (
<div className={classes.ContactData}>
<h4>Enter your Contact Data</h4>
{form}
</div>
);
}
}
export default ContactData;