-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from melange-re/order-conf-fixes
Add 'Type transformation functions' section to order confirmation chapter
- Loading branch information
Showing
11 changed files
with
270 additions
and
225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module App = { | ||
let items: Order.t = [|Sandwich, Burger, Sandwich|]; | ||
|
||
[@react.component] | ||
let make = () => | ||
<div> | ||
<h1> {React.string("Order confirmation")} </h1> | ||
<Order items /> | ||
</div>; | ||
}; | ||
|
||
let node = ReactDOM.querySelector("#root"); | ||
switch (node) { | ||
| None => | ||
Js.Console.error("Failed to start React: couldn't find the #root element") | ||
| Some(root) => ReactDOM.render(<App />, root) | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// #region type-t | ||
type t = | ||
| Sandwich | ||
| Burger; | ||
// #endregion type-t | ||
|
||
let _ = { | ||
// #region to-price | ||
let toPrice = t => | ||
switch (t) { | ||
| Sandwich => 10. | ||
| Burger => 15. | ||
}; | ||
// #endregion to-price | ||
toPrice(Sandwich) |> ignore; | ||
}; | ||
|
||
// #region to-price-fun | ||
let toPrice = | ||
fun | ||
| Sandwich => 10. | ||
| Burger => 15.; | ||
// #endregion to-price-fun | ||
|
||
// #region to-emoji | ||
let toEmoji = | ||
fun | ||
| Sandwich => {js|🥪|js} | ||
| Burger => {js|🍔|js}; | ||
// #endregion to-emoji | ||
|
||
// #region make | ||
[@react.component] | ||
let make = (~item: t) => | ||
<tr> | ||
<td> {item |> toEmoji |> React.string} </td> | ||
<td> | ||
{item | ||
|> toPrice | ||
|> Js.Float.toFixedWithPrecision(~digits=2) | ||
|> React.string} | ||
</td> | ||
</tr>; | ||
// #endregion make | ||
|
||
module AddHotdog = { | ||
// #region hotdog | ||
type t = | ||
| Sandwich | ||
| Burger | ||
| Hotdog; | ||
|
||
let toPrice = | ||
fun | ||
| Sandwich => 10. | ||
| Burger => 15. | ||
| Hotdog => 5.; | ||
|
||
let toEmoji = | ||
fun | ||
| Sandwich => {js|🥪|js} | ||
| Burger => {js|🍔|js} | ||
| Hotdog => {js|🌭|js}; | ||
// #endregion hotdog | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// #region order | ||
type t = array(Item.t); | ||
|
||
[@react.component] | ||
let make = (~items: t) => { | ||
let total = | ||
items |> Js.Array.reduce((acc, order) => acc +. Item.toPrice(order), 0.); | ||
|
||
<table> | ||
<tbody> | ||
{items |> Js.Array.map(item => <Item item />) |> React.array} | ||
<tr> | ||
<td> {React.string("Total")} </td> | ||
<td> | ||
{total |> Js.Float.toFixedWithPrecision(~digits=2) |> React.string} | ||
</td> | ||
</tr> | ||
</tbody> | ||
</table>; | ||
}; | ||
// #endregion order | ||
|
||
let _ = { | ||
let items = [||]; | ||
// #region mapi | ||
items | ||
|> Js.Array.mapi((item, index) => | ||
<Item key={"item-" ++ string_of_int(index)} item /> | ||
) | ||
|> React.array | ||
// #endregion mapi | ||
|> ignore; | ||
}; | ||
|
||
let _ = { | ||
let items = [||]; | ||
// #region order-make-item-rows | ||
let total = | ||
items |> Js.Array.reduce((acc, order) => acc +. Item.toPrice(order), 0.); | ||
|
||
let itemRows: array(React.element) = | ||
items |> Js.Array.map(item => <Item item />); | ||
|
||
<table> | ||
<tbody> | ||
{itemRows |> React.array} | ||
<tr> | ||
<td> {React.string("Total")} </td> | ||
<td> | ||
{total |> Js.Float.toFixedWithPrecision(~digits=2) |> React.string} | ||
</td> | ||
</tr> | ||
</tbody> | ||
</table>; | ||
// #endregion order-make-item-rows | ||
}; |
Oops, something went wrong.