Skip to content

Instantly share code, notes, and snippets.

@0gust1
Last active January 21, 2016 01:00
Show Gist options
  • Save 0gust1/28d55e3f6d07d34ed47f to your computer and use it in GitHub Desktop.
Save 0gust1/28d55e3f6d07d34ed47f to your computer and use it in GitHub Desktop.
module MyTodoApp where
import Html exposing (..)
import Html.Events exposing (..)
import Html.Attributes exposing (..)
import Signal exposing (..)
import StartApp.Simple as StartApp
type alias Model =
{ title: String, tasks: List Task, newTaskTitle: String, newTaskContent: String, uid : Int}
type alias Task =
{ title: String, content: String, isDone: Bool , id: Int}
initialModel : Model
initialModel =
{ title="Ma liste de tâches"
,tasks = [
{title = "Nourrir le singe", content = "Les bananes sont à la cave", isDone= True, id=1},
{title = "Décongeler la belle-mère", content = "Micro-onde, réglage 5", isDone= False, id=2}
]
,uid = 2
,newTaskTitle=""
,newTaskContent=""
}
type Action =
Add
| Delete Int
| MarkDone Int Bool
| UpdateNewTaskTitle String
| UpdateNewTaskContent String
{-
createNewTask : String -> String -> Bool -> Int -> Task
createNewTask ttle content compStatus current_index
{title = ttle, content = content, isDone = compStatus, id = (current_index +1)}
-}
update : Action -> Model -> Model
update action updateList =
case action of
Add ->
{updateList | tasks = (updateList.tasks ++ [{title= updateList.newTaskTitle, content= updateList.newTaskContent, isDone = False, id= updateList.uid + 1}])}
Delete t_id ->
{updateList | tasks = List.filter (\t -> t.id /= t_id) updateList.tasks}
MarkDone id completionStatus ->
let markTask t = if t.id == id then { t | isDone = completionStatus } else t
in
{updateList | tasks = List.map markTask updateList.tasks}
UpdateNewTaskTitle title ->
{updateList | newTaskTitle = title}
UpdateNewTaskContent content ->
{updateList | newTaskContent = content}
view : Address Action -> Model -> Html
view address model =
div []
[ --button [ onClick address (Mark "•") ] [ text "Mark" ],
h2 [] [ text(model.title ++ " – "), text (model.tasks |> List.length |> toString), text " tâches" ],
ul [] (List.map (\el -> taskItem el address) model.tasks),
Html.form [{-onSubmit address (Add targetValue.title targetValue.content), action "javascript:void(0);"-} id "addTaskForm"][
label[for "ajout-titre"][
text "Titre",
input[type' "text", on "input" targetValue (Signal.message address << UpdateNewTaskTitle), name "title", id "ajout-titre", placeholder "titre"][]
],
label[for "ajout-contenu"][
text "Contenu",
input[type' "text", on "input" targetValue (Signal.message address << UpdateNewTaskContent), name "content", id "ajout-contenu", placeholder "contenu"][]
],
button [type' "button", onClick address (Add)][text "➕"]
]
]
taskItem taskElt address =
li[ class (if taskElt.isDone then "status-done" else "status-todo")] [
p [][text taskElt.title],
p [][text taskElt.content],
p [][text (taskElt.isDone |> toString) ],
button [type' "button"
,onClick address (MarkDone taskElt.id True)][text "✅"],
button [type' "button"
,onClick address (Delete taskElt.id)][text "❌"]
]
main : Signal Html
main =
StartApp.start
{ model = initialModel,
view = view,
update = update }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment