forked from bryanjenningz/25-elm-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
22-localstorage-editable-todos.elm
220 lines (183 loc) · 5.83 KB
/
22-localstorage-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
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
port module Main exposing (..)
import Html exposing (..)
import Html.Attributes exposing (class, value, autofocus, placeholder, style, type_, checked)
import Html.Events exposing (onInput, onClick, onSubmit, onDoubleClick)
type Msg
= UpdateText String
| AddTodo
| RemoveTodo Int
| Edit Int String
| EditSave Int String
| ToggleTodo Int
type alias TodoEdit =
{ index : Int
, text : String
}
-- We changed the todos so that they aren't just strings, they now are records
-- with a completed property which represents whether the user has completed
-- that todo.
type alias Todo =
{ text : String
, completed : Bool
}
-- The todos property is now List Todo instead of List String.
type alias Model =
{ text : String
, todos : List Todo
, 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 -> Todo -> 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 -> Todo -> Html Msg
viewNormalTodo index todo =
div [ class "card" ]
[ div [ class "card-block" ]
-- Added a checkbox that can be clicked to toggle the todo between
-- being completed or incomplete.
[ input
[ onClick (ToggleTodo index)
, type_ "checkbox"
, checked todo.completed
, class "mr-3"
]
[]
, span
[ onDoubleClick (Edit index todo.text)
-- Added styling to indicate a todo is completed.
, style
[ ( "text-decoration"
, if todo.completed then
"line-through"
else
"none"
)
]
]
[ text todo.text ]
, 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 ->
let
newTodos =
model.todos ++ [ Todo model.text False ]
in
( { model | text = "", todos = newTodos }
, saveTodos newTodos
)
RemoveTodo index ->
let
beforeTodos =
List.take index model.todos
afterTodos =
List.drop (index + 1) model.todos
newTodos =
beforeTodos ++ afterTodos
in
( { model | todos = newTodos }, saveTodos newTodos )
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
{ todo | text = todoText }
else
todo
)
model.todos
in
( { model | editing = Nothing, todos = newTodos }
, saveTodos newTodos
)
-- Added an extra clause to handle toggling the todo.
ToggleTodo index ->
let
newTodos =
List.indexedMap
(\i todo ->
if i == index then
{ todo | completed = not todo.completed }
else
todo
)
model.todos
in
( { model | todos = newTodos }, saveTodos newTodos )
-- We changed the port type declaration to take a list of Todo records.
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
, Cmd.none
)
type alias Flags =
{ todos : List Todo }
main : Program Flags Model Msg
main =
programWithFlags
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}