forked from bryanjenningz/25-elm-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20-editable-todos.elm
176 lines (148 loc) · 4.95 KB
/
20-editable-todos.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
module Main exposing (..)
import Html exposing (..)
import Html.Attributes exposing (class, value, autofocus, placeholder)
import Html.Events exposing (onInput, onClick, onSubmit, onDoubleClick)
type Msg
= UpdateText String
| AddTodo
| RemoveTodo Int
| Edit Int String
| EditSave Int String
type alias TodoEdit =
{ index : Int
, text : String
}
type alias Model =
{ text : String
, todos : List String
, editing : Maybe TodoEdit
}
view : Model -> Html Msg
view model =
div [ class "col-12 col-sm-6 offset-sm-3" ]
[ form [ class "row", onSubmit AddTodo ]
[ div [ class "col-9" ]
[ input
[ onInput UpdateText
, value model.text
, autofocus True
, class "form-control"
, placeholder "Enter a todo"
]
[]
]
, div [ class "col-3" ]
[ button
[ class "btn btn-primary form-control" ]
[ text "+" ]
]
]
, div [] (List.indexedMap (viewTodo model.editing) model.todos)
]
viewTodo : Maybe TodoEdit -> Int -> String -> Html Msg
viewTodo editing index todo =
case editing of
Just todoEdit ->
if todoEdit.index == index then
viewEditTodo index todoEdit
else
viewNormalTodo index todo
Nothing ->
viewNormalTodo index todo
viewEditTodo : Int -> TodoEdit -> Html Msg
viewEditTodo index todoEdit =
div [ class "card" ]
[ div [ class "card-block" ]
[ form [ onSubmit (EditSave todoEdit.index todoEdit.text) ]
[ input
[ onInput (Edit index)
, class "form-control"
, value todoEdit.text
]
[]
]
]
]
viewNormalTodo : Int -> String -> Html Msg
viewNormalTodo index todo =
div [ class "card" ]
[ div [ class "card-block" ]
[ span
[ onDoubleClick (Edit index todo) ]
[ text todo ]
, span
[ onClick (RemoveTodo index)
, class "float-right"
]
[ text "✖" ]
]
]
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
UpdateText newText ->
( { model | text = newText }, Cmd.none )
AddTodo ->
( { model | text = "", todos = model.todos ++ [ model.text ] }
, Cmd.none
)
RemoveTodo index ->
let
beforeTodos =
List.take index model.todos
afterTodos =
List.drop (index + 1) model.todos
newTodos =
beforeTodos ++ afterTodos
in
( { model | todos = newTodos }, Cmd.none )
Edit index todoText ->
( { model | editing = Just { index = index, text = todoText } }
, Cmd.none
)
EditSave index todoText ->
let
newTodos =
List.indexedMap
(\i todo ->
if i == index then
todoText
else
todo
)
model.todos
in
( { model | editing = Nothing, todos = newTodos }
, Cmd.none
)
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
main : Program Never Model Msg
main =
program
{ init =
-- This is the short-hand version of what we had before.
-- Before we had the model initially as the value:
-- { text = "", todos = [ "Laundry", "Dishes" ], editing = Nothing }
-- We can also represent this value as this:
-- Model "" [ "Laundry", "Dishes" ] Nothing
-- Whenever we make a type alias that's a record, like Model, we
-- can use Model as a constructor function that returns a Model record.
-- Since we defined the Model type alias like this:
-- type alias Model =
-- { text : String
-- , todos : List String
-- , editing : Maybe TodoEdit
-- }
-- (Model "" [ "Laundry", "Dishes" ] Nothing) will make the first
-- argument the text property since that is first in the type alias
-- declaration. The second argument will be the todos property, and
-- the third argument will be the editing property.
( Model "" [ "Laundry", "Dishes" ] Nothing
, Cmd.none
)
, view = view
, update = update
, subscriptions = subscriptions
}