-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.elm
100 lines (83 loc) · 2.56 KB
/
app.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
import Html exposing (Html, button, div, text, input, label)
import Html.App as HtmlApp
import Html.Attributes exposing(class, style, placeholder, type', id, autofocus, value, for)
import Html.Events exposing (onClick, onInput, on, keyCode, targetChecked, onCheck)
import Debug exposing (log)
import String
import Json.Decode as Json
main =
HtmlApp.beginnerProgram { model = model, view = view, update = update }
type alias Task = {
name: String
, task_id: Int
, complete: Bool
}
type alias Model = {
tasks: List Task
, text: String
, count: Int
}
type Msg
= TextContent String
| PressToCreate Int
| CompleteOrIncomplete Bool Int
| Create
| DeleteAll
model : Model
model =
{text = "", tasks = [], count= 1}
onKeyUp : (Int -> msg) -> Html.Attribute msg
onKeyUp tagger =
on "keyup" (Json.map tagger keyCode)
update: Msg -> Model -> Model
update action model =
case action |> log "action" of
TextContent inputText ->
{model | text = inputText}
PressToCreate code->
if code == 13 then
createRecord model
else
model
CompleteOrIncomplete checked task_id ->
{model | tasks = List.map (\task -> update_task task task_id checked) model.tasks}
Create ->
createRecord model
DeleteAll ->
{model | tasks = [], count = 1}
update_task: Task -> Int -> Bool -> Task
update_task task task_id checked =
if task.task_id == task_id then
{task | complete = checked}
else
task
createRecord: Model -> Model
createRecord model =
if String.isEmpty model.text then
model
else
{model | count = model.count + 1, text = "", tasks = model.tasks ++ [{name = model.text, task_id = model.count, complete = False}]}
view : Model -> Html Msg
view model =
div []
[
input [type' "text", placeholder "Add a task",
onInput TextContent, autofocus True,
value model.text,
onKeyUp PressToCreate
] []
, div [id "tasks-holder"]
(List.map
taskHtml (model.tasks))
, div [class "button-group"] [
button [ type' "button", class "success button", onClick Create ] [ text "Add Task" ]
, button [ type' "button", class "alert button", onClick DeleteAll ] [ text "Clear All" ]
]]
taskHtml: Task -> Html Msg
taskHtml task =
div [class "row"] [
div [class "small-12 columns"] [
input [type' "checkbox", id ("checkbox_" ++ toString task.task_id), onCheck (\bool -> CompleteOrIncomplete bool task.task_id)][],
label [for ("checkbox_" ++ toString task.task_id), class (if task.complete then "strikethrough" else "")] [text task.name]
]
]