Skip to main content

12.12 Outputting Orders

Order.js

Order.js
import React from "react";
import classes from "./Order.module.css";

const order = (props) => {
const ingredients = [];

for (let ingredientName in props.ingredients) {
ingredients.push({
name: ingredientName,
amount: props.ingredients[ingredientName],
});
}

const ingredientOutput = ingredients.map((ig) => {
return (
<span
key={ig.name}
style={{
textTransform: "capitalize",
display: "inline-block",
margin: "0 8px",
border: "1px solid #ccc",
padding: "5px",
}}
>
{ig.name}: ({ig.amount})
</span>
);
});

return (
<div className={classes.Order}>
<p>Ingredients: </p>
{ingredientOutput}
<p>
Price:<strong>USD {Number.parseFloat(props.price).toFixed(2)}</strong>
</p>
</div>
);
};

export default order;

Orders.js

Orders.js
import React, { Component } from "react";
import axios from "../../axios-orders";
import Order from "../../components/Order/Order";
import withErrorHandler from "../../hoc/withErrorHandler/withErrorHandler";

class Orders extends Component {
state = {
orders: [],
loading: true,
};
componentDidMount() {
axios
.get("/orders.json")
.then((res) => {
// console.log("res.data:", res.data);
const fetchedOrders = [];
for (let key in res.data) {
fetchedOrders.push({
...res.data[key],
id: key,
});
}
this.setState({ loading: false, orders: fetchedOrders });
})
.catch((err) => {
this.setState({ loading: false });
});
}
render() {
return (
<div>
{this.state.orders.map((order) => (
<Order
key={order.id}
ingredients={order.ingredients}
price={order.price}
/>
))}
</div>
);
}
}
export default withErrorHandler(Orders, axios);