Skip to content
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

Merged
merged 12 commits into from
Dec 8, 2023
Merged

Add order confirmation chapter #9

merged 12 commits into from
Dec 8, 2023

Conversation

feihong
Copy link
Collaborator

@feihong feihong commented Dec 1, 2023

This chapter mostly introduces variant types but touches on a lot of other things like mel.raw, fun, arrays, and runtime_deps

@feihong feihong marked this pull request as ready for review December 1, 2023 03:49
Copy link
Member

@jchavarri jchavarri left a 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.

Comment on lines +3 to +6
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.
Copy link
Member

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 Show resolved Hide resolved

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.
Copy link
Member

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 😄

Copy link
Collaborator Author

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.


## `fun` sugar syntax

Incidentally, there's a sugar syntax for functions whose entire body is a switch
Copy link
Member

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:

Suggested change
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`

Copy link
Collaborator Author

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 Show resolved Hide resolved

```
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'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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'

Copy link
Collaborator Author

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 Show resolved Hide resolved
docs/order-confirmation/index.md Outdated Show resolved Hide resolved
docs/order-confirmation/index.md Show resolved Hide resolved
Comment on lines 329 to 330
[^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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lolwat

Copy link
Collaborator Author

@feihong feihong left a 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 Show resolved Hide resolved

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.
Copy link
Collaborator Author

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.


## `fun` sugar syntax

Incidentally, there's a sugar syntax for functions whose entire body is a switch
Copy link
Collaborator Author

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

<<< 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.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 895b759

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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 68a424d

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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 68a424d

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`.
Copy link
Collaborator Author

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

@feihong feihong marked this pull request as draft December 6, 2023 03:56
@feihong feihong marked this pull request as ready for review December 6, 2023 18:03
Copy link
Member

@jchavarri jchavarri left a 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)
Copy link
Member

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}
Copy link
Member

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.
:::

Copy link
Collaborator Author

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

items
|> Js.Array.reduce((acc, order) => acc +. Item.toPrice(order), 0.);

let itemRows = items |> Js.Array.map(item => <Item item />);
Copy link
Member

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:

To better see what types are at play, it might make sense to refactor
`Order.make` like so:

Copy link
Collaborator Author

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.

Comment on lines 205 to 206
In practice, it's common for Melange bindings do not match one-to-one to their
JavaScript counterparts.
Copy link
Member

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?

Suggested change
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.

Copy link
Member

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

Copy link
Collaborator Author

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


<<< Snippets.re#make

The `make` function has a single labeled argument, `~item`, of type `t`. This
Copy link
Member

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:

Suggested change
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

Copy link
Collaborator Author

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

Copy link
Collaborator Author

@feihong feihong left a 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}
Copy link
Collaborator Author

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

items
|> Js.Array.reduce((acc, order) => acc +. Item.toPrice(order), 0.);

let itemRows = items |> Js.Array.map(item => <Item item />);
Copy link
Collaborator Author

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.

Comment on lines 205 to 206
In practice, it's common for Melange bindings do not match one-to-one to their
JavaScript counterparts.
Copy link
Collaborator Author

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


<<< Snippets.re#make

The `make` function has a single labeled argument, `~item`, of type `t`. This
Copy link
Collaborator Author

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

@feihong feihong merged commit 7543861 into main Dec 8, 2023
@feihong feihong deleted the order-conf branch December 8, 2023 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants