-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.elm
115 lines (84 loc) · 2.67 KB
/
Main.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
module Main exposing (..)
import Html exposing (..)
import Html.Attributes exposing (class, disabled, size, value)
import Html.Events exposing (onClick, onInput)
-- MODEL
type alias Model =
{ availableFlavors : List String
, likedFlavors : List String
, rightSelected : String
, leftSelected : String
}
initialModel : Model
initialModel =
{ availableFlavors =
[ "butter pecan"
, "chocolate"
, "chocolate chip"
, "chocolate mint"
, "cookie dough"
, "neapolitan"
, "rainbow sherbet"
, "raspberry"
, "strawberry"
, "vanilla"
]
, likedFlavors = []
, rightSelected = ""
, leftSelected = ""
}
-- MESSAGES
type Msg
= OnRightSelect String
| OnLeftSelect String
| MoveRight String
| MoveLeft String
-- VIEW
view : Model -> Html Msg
view model =
div [ class "App" ]
[ h1 [] [ text "Favorite Ice Cream Flavors" ]
, swapper model
]
swapper : Model -> Html Msg
swapper model =
div
[ class "swapper" ]
[ select_ model.availableFlavors OnRightSelect (List.length model.availableFlavors) model.rightSelected
, div [ class "buttons" ]
[ button [ disabled (String.isEmpty model.rightSelected), onClick (MoveRight model.rightSelected) ] [ text "⇨" ]
, button [ disabled (String.isEmpty model.leftSelected), onClick (MoveLeft model.leftSelected) ] [ text "⇦" ]
]
, select_ model.likedFlavors OnLeftSelect (List.length model.availableFlavors) model.leftSelected
]
select_ : List String -> (String -> msg) -> Int -> String -> Html msg
select_ items onSelect size_ value_ =
select [ onInput onSelect, size size_, value value_ ]
(List.map (\flavor -> option [] [ text flavor ]) items)
--UPDATE
update : Msg -> Model -> Model
update msg model =
case msg of
OnRightSelect str ->
{ model | rightSelected = str }
OnLeftSelect str ->
{ model | leftSelected = str }
MoveRight str ->
{ model
| likedFlavors = str :: model.likedFlavors
, availableFlavors = List.filter (\flavor -> str /= flavor) model.availableFlavors
, rightSelected = ""
}
MoveLeft str ->
{ model
| availableFlavors = str :: model.availableFlavors
, likedFlavors = List.filter (\flavor -> str /= flavor) model.likedFlavors
, leftSelected = ""
}
main : Program Never Model Msg
main =
Html.beginnerProgram
{ model = initialModel
, view = view
, update = update
}