Skip to content

Commit 489c14d

Browse files
Merge pull request #35 from MI-AFP/materials-elm-11
Elm building web apps materials update.
2 parents ff855c5 + 5889869 commit 489c14d

2 files changed

Lines changed: 57 additions & 6 deletions

File tree

tutorials/11_elm-building-web-apps.md

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ type Msg
160160

161161
subscriptions : Model -> Sub Msg
162162
subscriptions model =
163-
gotUser GotUser
163+
Ports.gotUser GotUser
164164

165165
```
166166

@@ -327,6 +327,8 @@ In the previous example, we used Url as is. However, we can use [Url.Parser](htt
327327
Here's an example form the documentation converting different routes with parameters into `Route` type.
328328

329329
```elm
330+
import Url.Parser exposing ((</>), Parser, int, s, string)
331+
330332
type Route
331333
= Topic String
332334
| Blog Int
@@ -354,17 +356,67 @@ route =
354356
-- /user/ ==> Nothing
355357
```
356358

359+
## Opaque type
360+
361+
Opaque types are types that hide their internal implementation details within a module. While this statement seems benign on its surface, it’s an incredibly important concept in an ecosystem that enforces semantic versioning.
362+
363+
*Note*: Taken from [Charlie Koster, medium.com](https://ckoster22.medium.com/advanced-types-in-elm-opaque-types-ec5ec3b84ed2)
364+
365+
```elm
366+
module Email exposing (Email, decodeEmail, toString)
367+
368+
import Json.Decode as Decode
369+
370+
type Email
371+
= EmailInternal String
372+
373+
decodeEmail : Decode.Decoder Email
374+
decodeEmail =
375+
Decode.andThen validateEmail Decode.string
376+
377+
validateEmail : String -> Decode.Decoder Email
378+
validateEmail emailString =
379+
if isEmailValid emailString then
380+
Decode.succeed <| EmailInternal emailString
381+
382+
else
383+
Decode.fail "Invalid email!"
384+
385+
toString : Email -> String
386+
toString (EmailInternal email) =
387+
email
388+
389+
isEmailValid : String -> Bool
390+
391+
-- in Home page
392+
import Email
393+
394+
emailView : Email.Email -> Html msg
395+
emailView =
396+
Email.toString
397+
>> Html.text
398+
399+
emailView2 : String -> Html msg
400+
emailView2 =
401+
Html.text
402+
```
403+
404+
*Note*: From `Email` module, we expose only `Email` type without variant `EmailInternal`. The only way, how to access email value is in this module, no other module does not have access to `EmailInternal` and can use only access function `toString`.
357405

358406
## Materials
359407

360-
- [create-elm-app](https://github.com/halfzebra/create-elm-app)
361-
- [elm-shared-state](https://github.com/ohanhi/elm-shared-state)
362-
- [elm-awesome](https://github.com/sporto/awesome-elm)
408+
- [elm-spa](https://github.com/deny1994/elm-spa)
363409
- [elm-webpack-boilerplate](https://github.com/MI-AFP/elm-webpack-boilerplate)
364410
- [Examples - Web Application](https://github.com/MI-AFP/elm-examples/tree/master/webapp)
365411

366412
## Further Reading
367413

414+
- [Opaque types](https://ckoster22.medium.com/advanced-types-in-elm-opaque-types-ec5ec3b84ed2)
415+
- [Make impossible states impossible](https://www.youtube.com/watch?v=IcgmSRJHu_8&ab_channel=elm-conf)
416+
- [Elm Europe 2017 - Richard Feldman - Scaling Elm Apps](https://www.youtube.com/watch?v=DoA4Txr4GUs)
417+
- [Richard Feldman real world SPA](https://github.com/rtfeldman/elm-spa-example)
418+
- [create-elm-app](https://github.com/halfzebra/create-elm-app)
368419
- [Webpack Concepts](https://webpack.js.org/concepts)
369420
- [Web Apps · An Introduction to Elm](https://guide.elm-lang.org/webapps/)
370-
- [Elm Europe 2017 - Richard Feldman - Scaling Elm Apps](https://www.youtube.com/watch?v=DoA4Txr4GUs)
421+
- [elm-shared-state](https://github.com/ohanhi/elm-shared-state)
422+
- [elm-awesome](https://github.com/sporto/awesome-elm)

tutorials/12_elm-real-world-use-cases.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,6 @@ The other option is to [use ports](https://stackoverflow.com/a/52569683/2492795)
309309

310310
## Further Reading
311311

312-
- [Richard Feldman real world SPA](https://github.com/rtfeldman/elm-spa-example)
313312
- [SVG: Scalable Vector Graphics](https://developer.mozilla.org/en-US/docs/Web/SVG)
314313
- [Line Charts - A library for plotting line charts in SVG. Written in all Elm.](https://github.com/terezka/line-charts)
315314
- [Working with Files](https://elm-lang.org/blog/working-with-files)

0 commit comments

Comments
 (0)