Skip to content

Commit 2a83869

Browse files
committed
Some helpful examples and fixes
1 parent 14f5eb0 commit 2a83869

File tree

5 files changed

+109
-10
lines changed

5 files changed

+109
-10
lines changed

how-to-elm/README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,18 @@ These can be a blessing and a curse, as some things might be better with a dedic
1010

1111
## Rules
1212

13-
> I need to start setting rules for myself
13+
> I need to start setting rules for myself ...
14+
> How do I keep my programs simple? What's in? What's out?
1415
1516
1. What format am I using? (custom headings or `elm-format`)
1617
2. What indentation style am I preferring? (`2` or `4` spaces)
1718
3. What learning is "in" and what is "out"?[^1]
1819

1920

21+
## Improvements
22+
23+
1. When using `port`s, it can be handy to have a cursory knowledge of javascript.
24+
- In general I use as little javascript as possible. Set it and forget it.
25+
26+
2027
[^1]: I'm using Elm as my go-to language and for that reason learning the details in a little more depth than, say, Python. However, my purpose is prototyping and I still want to keep my learning light. Some implementations (SPA, packages) get complicated quite quickly as an app scales.

how-to-elm/src/Form/Email.elm

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module Form.Email exposing (validateEmail)
2+
3+
{-| ----------------------------------------------------------------------------
4+
Email (with Regex)
5+
============================================================================
6+
Some helpful links:
7+
@ http://html-to-elm.com
8+
@ hecrj/html-parser (package)
9+
@ elm-explorations/markdown (Github)
10+
@ pablohirafuji/elm-markdown (package)
11+
12+
Wishlist
13+
--------
14+
1. Add some color (red or green)
15+
2. Present user with error message ("username or password incorrect")
16+
- Be careful of oversharing which risks a brute force hack!
17+
- @ https://ux.stackexchange.com/questions/111830
18+
3. Add log files? (backend, probably)
19+
-}
20+
21+
import Regex
22+
23+
24+
type alias Email
25+
= Maybe String
26+
27+
validateEmail : String -> Email
28+
validateEmail userinput =
29+
let
30+
pattern = Regex.regex "\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}\\b"
31+
isEmailStr = Regex.contains pattern userinput
32+
33+
in
34+
if isEmailStr then
35+
Email userInput
36+
else
37+
Nothing

how-to-elm/src/Form/Simple.elm

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
module Form.Simple exposing (..)
22

3-
import Browser
4-
import Html exposing (..)
5-
import Html.Attributes exposing (..)
6-
import Html.Events exposing (onInput)
7-
import String exposing (any)
8-
import Char exposing (isDigit, isLower, isUpper)
9-
import Debug exposing (log)
10-
113
{-| A simple form
124
135
Always start with the `Model` — what shape will our data take?
@@ -36,6 +28,14 @@ import Debug exposing (log)
3628
deciding you need something more complex!
3729
-}
3830

31+
import Browser
32+
import Html exposing (..)
33+
import Html.Attributes exposing (..)
34+
import Html.Events exposing (onInput)
35+
import String exposing (any)
36+
import Char exposing (isDigit, isLower, isUpper)
37+
import Debug exposing (log)
38+
3939

4040
-- Main ------------------------------------------------------------------------
4141

how-to-elm/src/Ports/SimpleLogout.elm

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
module Ports.SimpleLogout exposing (..)
2+
3+
{-| ----------------------------------------------------------------------------
4+
From the example in Slack
5+
============================================================================
6+
Here we have everything contained in the one file, unlike our `LocalStorage.elm`
7+
example. We start with a flag that may be `Nothing` and we've got two functions
8+
that set login/logout. Unlike `LocalStorage.elm`, our update function is simple
9+
and doesn't attempt to `Cmd.batch` our commands. It's one `Cmd` to login, and
10+
one to logout.
11+
12+
Pushing our changes The JS side would look like:
13+
14+
```js
15+
const localStorageAuthKey = "my-app-auth";
16+
const auth = localStorage.getItem(localStorageAuthKey);
17+
const app = Elm.Main.init(someNode, {auth});
18+
19+
app.ports.persistLogin.subscribe((auth) => localStorage.setItem(localStorageAuthKey,auth));
20+
app.ports.persistLogout.subscribe(() => localStorage.removeItem(localStorageAuthKey));
21+
```
22+
23+
You might also like to check if the function exists as Ryan Haskell suggests.
24+
If it doesn't exist, you can use a default value or do nothing ...
25+
@ https://www.youtube.com/watch?v=YfS5BJ4IXcQ
26+
27+
```js
28+
if (app.ports?.persistLogin?.subscribe) {
29+
app.ports.persistLogin.subscribe((auth) => localStorage.setItem
30+
}
31+
```
32+
-}
33+
34+
port persistLogin : String -> Cmd msg
35+
port persistLogout : () -> Cmd msg
36+
37+
type alias Flags =
38+
{ auth : Maybe String }
39+
40+
type alias Model =
41+
{ auth : Maybe String }
42+
43+
type Msg
44+
= LogIn String
45+
| LogOut
46+
47+
init : Flags -> (Model, Cmd Msg)
48+
init flags =
49+
( { auth = flags.auth }
50+
, Cmd.none
51+
)
52+
53+
update msg model =
54+
case msg of
55+
LogIn auth -> ({ model | auth = Just auth }, persistLogin auth)
56+
LogOut -> ({ model | auth = Nothing }, persistLogout ())

programming-elm/src/DevDebugDeploy/Debugging.elm

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ module DevDebugDeploy.Debugging exposing (..)
1919
import Html exposing (Html, text)
2020
import Json.Decode as Json
2121
import Json.Decode.Pipeline exposing (required)
22-
import WebSockets.RealTime exposing (Msg)
2322

2423

2524
-- Model -----------------------------------------------------------------------

0 commit comments

Comments
 (0)