-
Notifications
You must be signed in to change notification settings - Fork 3
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
Add order confirmation chapter #9
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
phew! Amazing work, this chapter is quite extensive! I really like it.
I left a lot of minor suggestions, the most relevant one is prob the JSX and React.element
relationship and the key
attrs.
The famed restauranteur Madame Jellobutter has opened a hot new pop-up | ||
restaurant called Emoji Cafe, and you've been commissioned to build the order | ||
confirmation widget on its website. Feeling adventurous, you decide to build it | ||
using Melange. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love this intro 👩🍳
docs/order-confirmation/index.md
Outdated
|
||
Your OCaml code wouldn't compile if you just added a new constructor to | ||
`Item.t` without also updating `Item.toPrice`. Adding or removing constructors | ||
from a variant usually forces you to change relevant parts of your code. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"usually"? Sounds like the compiler is not precise sometimes 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got rid of "usually" in 2d781b2. Also in the commit: added a new section about wildcard in switch expression to explain the case when adding or removing constructors wouldn't force you to change your code.
docs/order-confirmation/index.md
Outdated
|
||
## `fun` sugar syntax | ||
|
||
Incidentally, there's a sugar syntax for functions whose entire body is a switch |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why "Incidentally"? Maybe can be removed? I am also not sure about using "syntax sugar", I remember when I started using BuckleScript and Reason that this term would be thrown around, and I had no clue what it meant for a while 😅 i think it's a term mostly people familiar with parsers and programming langs might be familiar with, but maybe can be written in a different way:
Incidentally, there's a sugar syntax for functions whose entire body is a switch | |
There's an alternate, shorter way to write functions whose entire body is a `switch` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stopped using the term syntax sugar in 895b759
docs/order-confirmation/index.md
Outdated
|
||
``` | ||
ERROR in ./_build/default/src/order-confirmation/output/src/order-confirmation/Item.js 5:0-6:1 | ||
Module not found: Error: Can't resolve './item.css' in '/home/fhsu/work/melange-for-react-devs/_build/default/src/order-confirmation/output/src/order-confirmation' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Module not found: Error: Can't resolve './item.css' in '/home/fhsu/work/melange-for-react-devs/_build/default/src/order-confirmation/output/src/order-confirmation' | |
Module not found: Error: Can't resolve './item.css' in '~/melange-for-react-devs/_build/default/src/order-confirmation/output/src/order-confirmation' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part was removed from this PR, will reappear in next PR
docs/order-confirmation/index.md
Outdated
[^4]: Madame Jellobutter was passing by and just happened to catch a glimpse of | ||
the unstyled component over your shoulder and puked in her mouth a little. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lolwat
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the latest edits I made, I increased the size of the chapter and now it's almost two times as long as some other chapters.
I'm going to move this PR back to draft and cut out the styling with CSS parts to put them in the next chapter.
Update: Cut out the styling bits in 3a7d0a0
docs/order-confirmation/index.md
Outdated
|
||
Your OCaml code wouldn't compile if you just added a new constructor to | ||
`Item.t` without also updating `Item.toPrice`. Adding or removing constructors | ||
from a variant usually forces you to change relevant parts of your code. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got rid of "usually" in 2d781b2. Also in the commit: added a new section about wildcard in switch expression to explain the case when adding or removing constructors wouldn't force you to change your code.
docs/order-confirmation/index.md
Outdated
|
||
## `fun` sugar syntax | ||
|
||
Incidentally, there's a sugar syntax for functions whose entire body is a switch |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stopped using the term syntax sugar in 895b759
docs/order-confirmation/index.md
Outdated
<<< Snippets.re#to-emoji | ||
|
||
Using the `fun` syntax is completely equivalent to using a switch expression, | ||
so it's up to your personal taste whether you want to use this sugar syntax. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 895b759
docs/order-confirmation/index.md
Outdated
This means the `Order` component has a single prop named `items`. | ||
- We sum up the prices of all items using | ||
[Js.Array.reduce](https://melange.re/v2.1.0/api/re/melange/Js/Array/index.html#val-reduce). | ||
This is identical to JavaScript's [Array.reduce |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 68a424d
docs/order-confirmation/index.md
Outdated
except that the Melange version requires the initial value to be passed in. | ||
- For each order, we render an `Item` component via | ||
[Js.Array.map](https://melange.re/v2.1.0/api/re/melange/Js/Array/index.html#val-map), | ||
which is equivalent to the [Array.map |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 68a424d
docs/order-confirmation/index.md
Outdated
method](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map). | ||
- The `React.array` function has the type signature `array(React.element) => | ||
React.element`, meaning it converts an array of `React.element`s to | ||
`React.element`. All expressions in JSX must have the type `React.element`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to tackle this by adding an entire section for this instead of relegating it to a single bullet point: c85c07a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! 🚀 👩🍳
Just a thought, prob too late already, but did you consider using lists instead of arrays across the board? Would that make anything easier? As we don't need random access, and probably new items in the order are just appended, wonder if it'd have been a better data structure 🤔
<td> | ||
{item | ||
|> toPrice | ||
|> Js.Float.toFixedWithPrecision(~digits=2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Heads up: these functions are being modified with Melange v3: melange-re/melange#673
No action needed, just once v3 is released we'll have to remember to update the book so that all snippets compile.
// #region to-emoji | ||
let toEmoji = | ||
fun | ||
| Sandwich => {js|🥪|js} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just realized we could mention that {js||js}
is Melange-specific, and it's not available in native OCaml.
Maybe we could come up with some kind of tips, I noticed vitepress supports them:
::: tip
This is a tip.
:::
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a tip in the chapter where we first covered {js||js}
strings: d4aa3b7
docs/order-confirmation/Snippets.re
Outdated
items | ||
|> Js.Array.reduce((acc, order) => acc +. Item.toPrice(order), 0.); | ||
|
||
let itemRows = items |> Js.Array.map(item => <Item item />); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to add the explicit annotation here? As I read the text that uses this code, I noticed it's talking about the types themselves:
melange-for-react-devs/docs/order-confirmation/index.md
Lines 167 to 168 in ed2005d
To better see what types are at play, it might make sense to refactor | |
`Order.make` like so: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added type annotation in 6634e59 and also added an explanation that React.array
doesn't change the underlying JS object.
docs/order-confirmation/index.md
Outdated
In practice, it's common for Melange bindings do not match one-to-one to their | ||
JavaScript counterparts. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sentence is strangely constructed, maybe this?
In practice, it's common for Melange bindings do not match one-to-one to their | |
JavaScript counterparts. | |
In practice, it's common for Melange bindings to not match one-to-one their | |
JavaScript counterparts. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The sentence is also a bit misleading. The JavaScript API includes the index as well. Maybe something like:
It's common for Melange bindings to represent JavaScript APIs with optional arguments using multiple OCaml externals.
Can link to this section: https://melange.re/v2.1.0/communicate-with-javascript/#approach-1-multiple-external-functions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the link and tried to make the statement more specific in 132bda1
docs/order-confirmation/index.md
Outdated
|
||
<<< Snippets.re#make | ||
|
||
The `make` function has a single labeled argument, `~item`, of type `t`. This |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this make sense? A bit redundant, but might help clarifying:
The `make` function has a single labeled argument, `~item`, of type `t`. This | |
The `make` function has a single labeled argument, `~item`, of type `Item.t`. This |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed from t to Item.t in 1176139
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a thought, prob too late already, but did you consider using lists instead of arrays across the board?
Yes, in a future chapter, I'll migrate from arrays to lists. I figured I would start with arrays since that's what most frontend devs are more familiar with.
// #region to-emoji | ||
let toEmoji = | ||
fun | ||
| Sandwich => {js|🥪|js} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a tip in the chapter where we first covered {js||js}
strings: d4aa3b7
docs/order-confirmation/Snippets.re
Outdated
items | ||
|> Js.Array.reduce((acc, order) => acc +. Item.toPrice(order), 0.); | ||
|
||
let itemRows = items |> Js.Array.map(item => <Item item />); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added type annotation in 6634e59 and also added an explanation that React.array
doesn't change the underlying JS object.
docs/order-confirmation/index.md
Outdated
In practice, it's common for Melange bindings do not match one-to-one to their | ||
JavaScript counterparts. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the link and tried to make the statement more specific in 132bda1
docs/order-confirmation/index.md
Outdated
|
||
<<< Snippets.re#make | ||
|
||
The `make` function has a single labeled argument, `~item`, of type `t`. This |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed from t to Item.t in 1176139
This chapter mostly introduces variant types but touches on a lot of other things like
mel.raw
,fun
, arrays, andruntime_deps