-
Notifications
You must be signed in to change notification settings - Fork 16
/
24-filter-todos.elm
340 lines (269 loc) · 9.95 KB
/
24-filter-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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
port module Main exposing (main)
-- Importing the Random module because we're going to use it to generate
-- a random id so that we can uniquely identify each todo.
import Browser exposing (element)
import Html exposing (..)
import Html.Attributes exposing (autofocus, checked, class, placeholder, style, type_, value)
import Html.Events exposing (onClick, onDoubleClick, onInput, onSubmit)
import Random
-- Added GenerateTodoId message, which we're going to use to generate a
-- random id for a newly created todo.
-- AddTodo was changed to (AddTodo Int) because it now takes the integer
-- id that was randomly generated and uses that to add the new todo.
type Msg
= UpdateText String
| GenerateTodoId
| AddTodo Int
| RemoveTodo Int
| Edit Int String
| EditSave Int String
| ToggleTodo Int
| SetFilter Filter
type Filter
= All
| Incomplete
| Completed
-- Before TodoEdit had an index and a text field, now we're going to be
-- using the todo's id to identify which todo we're editing, so I
-- switched the field name from index to id.
type alias TodoEdit =
{ id : Int
, text : String
}
-- Added an id field, which will hold each todo's randomly generated id.
type alias Todo =
{ id : Int
, text : String
, completed : Bool
}
type alias Model =
{ text : String
, todos : List Todo
, editing : Maybe TodoEdit
, filter : Filter
}
view : Model -> Html Msg
view model =
div [ class "col-12 col-sm-6 offset-sm-3" ]
-- GenerateTodoId is now the message that gets passed into
-- the update function as the message when the form submits.
[ form [ class "row", onSubmit GenerateTodoId ]
[ 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 "+" ]
]
]
, viewFilters model.filter
, div [] <|
-- We use List.map now instead of List.indexedMap, since we
-- are using the id and not the index to edit and remove todos.
List.map
(viewTodo model.editing)
(filterTodos model.filter model.todos)
]
filterTodos : Filter -> List Todo -> List Todo
filterTodos filter todos =
case filter of
All ->
todos
Incomplete ->
List.filter (\t -> not t.completed) todos
Completed ->
List.filter (\t -> t.completed) todos
viewFilters : Filter -> Html Msg
viewFilters filter =
div []
[ viewFilter All (filter == All) "All"
, viewFilter Incomplete (filter == Incomplete) "Incomplete"
, viewFilter Completed (filter == Completed) "Completed"
]
viewFilter : Filter -> Bool -> String -> Html Msg
viewFilter filter isFilter filterText =
if isFilter then
span [ class "mr-3" ] [ text filterText ]
else
span
[ class "text-primary mr-3"
, onClick (SetFilter filter)
, style "cursor" "pointer"
]
[ text filterText ]
-- viewTodo now has one less argument because it doesn't need
-- to have the index passed in anymore since it uses the id
-- for editing and removing each todo. It also doesn't pass
-- the index into viewEditTodo and viewNormalTodo anymore.
viewTodo : Maybe TodoEdit -> Todo -> Html Msg
viewTodo editing todo =
case editing of
Just todoEdit ->
if todoEdit.id == todo.id then
viewEditTodo todoEdit
else
viewNormalTodo todo
Nothing ->
viewNormalTodo todo
-- viewEditTodo has one less argument because it doesn't need the index
-- for editing, it uses the todo's id to identify the todo that is being edited.
-- It passes in todoEdit.id into Edit and EditSave now instead of the index.
viewEditTodo : TodoEdit -> Html Msg
viewEditTodo todoEdit =
div [ class "card" ]
[ div [ class "card-block" ]
[ form [ onSubmit (EditSave todoEdit.id todoEdit.text) ]
[ input
[ onInput (Edit todoEdit.id)
, class "form-control"
, value todoEdit.text
]
[]
]
]
]
-- viewNormalTodo has one less argument because it doesn't need the index.
-- It now passes todo.id into ToggleTodo, Edit, and RemoveTodo instead of the index.
viewNormalTodo : Todo -> Html Msg
viewNormalTodo todo =
div [ class "card" ]
[ div [ class "card-block" ]
[ input
[ onClick (ToggleTodo todo.id)
, type_ "checkbox"
, checked todo.completed
, class "mr-3"
]
[]
, span
[ onDoubleClick (Edit todo.id todo.text)
, style
"text-decoration"
(if todo.completed then
"line-through"
else
"none"
)
]
[ text todo.text ]
, span
[ onClick (RemoveTodo todo.id)
, class "float-right"
]
[ text "✖" ]
]
]
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
UpdateText newText ->
( { model | text = newText }, Cmd.none )
-- When the form to add a new todo submits, it now passes in
-- GenerateTodoId, which will generate a random integer id,
-- then the result will be passed into AddTodo which now accepts
-- an integer.
-- I'm going to explain each part of this:
-- Random.generate AddTodo (Random.int Random.minInt Random.maxInt)
-- Random.int is a function that takes 2 Int arguments and returns
-- a (Generate Int) type. Random.minInt is the minimum integer value
-- that a 32-bit integer can be. Random.maxInt is the maximum integer
-- value that a 32-bit integer can be.
-- So (Random.int Random.minInt Random.maxInt) is a Generator Int type.
-- Random.generate is a function that takes a function that accepts an
-- Int and returns a Msg as the first argument and a Generator Int as
-- the second argument, then it will randomly generate an integer
-- between Random.minInt and Random.maxInt, then it will pass that
-- integer to AddTodo, which will get passed into the update function.
GenerateTodoId ->
( model
, Random.generate AddTodo (Random.int Random.minInt Random.maxInt)
)
-- So now after the random id is generated, it gets passed into AddTodo
-- which gets passed into the update function. We take the id and current
-- model.text value to create the new todo and then we append the new
-- todo to the end of model.todos.
AddTodo todoId ->
let
newTodos =
model.todos ++ [ Todo todoId model.text False ]
in
( { model | text = "", todos = newTodos }
, saveTodos newTodos
)
-- Since we get passed the id, we can just use List.filter to
-- keep all the todos that don't have the same id as the one
-- we are removing.
RemoveTodo todoId ->
let
newTodos =
List.filter (\todo -> todo.id /= todoId) model.todos
in
( { model | todos = newTodos }, saveTodos newTodos )
-- We use id to track the edited todo instead of the index.
Edit todoId todoText ->
( { model | editing = Just { id = todoId, text = todoText } }
, Cmd.none
)
-- We are now saving the todo, so if the todo's id is the id that we
-- were editing, then we change the text of that todo to the edit text.
-- If its id isn't the same as the edit todo's id, we keep it the same
-- because it wasn't the todo that we were editing.
EditSave todoId todoText ->
let
newTodos =
List.map
(\todo ->
if todo.id == todoId then
{ todo | text = todoText }
else
todo
)
model.todos
in
( { model | editing = Nothing, todos = newTodos }
, saveTodos newTodos
)
-- We map over the todos and change the completed field of
-- the todo that has the id that we chose to toggle.
ToggleTodo todoId ->
let
newTodos =
List.map
(\todo ->
if todo.id == todoId then
{ todo | completed = not todo.completed }
else
todo
)
model.todos
in
( { model | todos = newTodos }, saveTodos newTodos )
SetFilter filter ->
( { model | filter = filter }, Cmd.none )
port saveTodos : List Todo -> Cmd msg
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
init : Flags -> ( Model, Cmd Msg )
init flags =
( Model "" flags.todos Nothing All
, Cmd.none
)
type alias Flags =
{ todos : List Todo }
main : Program Flags Model Msg
main =
element
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}