Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Html exposing (beginnerProgram, div, button, text,input)
- import Html.Events exposing (onClick, onInput)
- import Html.Attributes exposing (..)
- main : Program Never Model Msg
- main =
- beginnerProgram { model = model, update = update, view = view}
- type alias Model =
- { input : String
- , tasks : List String
- , done : Int
- , dayGoal : Int --default is 3
- , taskHistory : List String
- }
- model : Model
- model =
- Model "" [] 0 3 []
- type Msg =
- Add
- | Del
- | DelCheckbox String
- | Input String
- | SetGoal
- filterFirst : String -> List String -> List String
- filterFirst str list = if str == ""
- then list
- else if (List.length list) == 0
- then []
- else if (List.take 1 list) == [str]
- then (List.drop 1 list)
- else List.append (List.take 1 list) (filterFirst str (List.drop 1 list))
- toAdd : List String -> String -> Int
- toAdd list str = if List.member str list
- then 1
- else 0
- getFirst : String -> List String -> String
- getFirst str list = if List.member str list
- then str
- else ""
- appendHistory : String -> List String -> List String
- appendHistory str list = if str == ""
- then list
- else
- (List.append list [str])
- --List.filter (\x-> (x/=model.input || model.input == "")
- update : Msg -> Model -> Model
- update msg model=
- case msg of
- Del ->
- (Model model.input (filterFirst model.input model.tasks)
- (model.done+(toAdd model.tasks model.input)) (model.dayGoal) (appendHistory model.input model.taskHistory)) --because otherwise 1 delete with no args deletes every task
- Add ->
- (Model model.input (List.append model.tasks [model.input]) model.done (model.dayGoal) model.taskHistory)
- Input str ->
- (Model str model.tasks model.done (model.dayGoal) model.taskHistory)
- DelCheckbox str ->
- (Model model.input (filterFirst str model.tasks)
- (model.done+(toAdd model.tasks model.input)) (model.dayGoal) (appendHistory str model.taskHistory))
- SetGoal ->
- (Model "" model.tasks model.done (Result.withDefault 0 (String.toInt model.input)) model.taskHistory)
- checkIfdayGoal : Model -> Bool
- checkIfdayGoal model = if model.done >= model.dayGoal
- then True
- else
- False
- getCongratulations : Model -> String
- getCongratulations model =
- if model.dayGoal == 0
- then "Man, be ambitious!"
- else if (checkIfdayGoal model)
- then "Congratulations, goal achieved!"
- else
- "Keep it up, all you need is " ++ (toString (model.dayGoal - model.done))
- view : Model -> Html.Html Msg
- view model =
- div []
- [
- div [doneStyle] [text ("Done today: " ++ toString model.done)]
- , div [doneStyle] [text (getCongratulations model)]
- , div [alignCenter] [
- input [type_ "text", placeholder "Add new task",onInput Input] []
- , button [onClick Add] [text "Add"]
- , button [onClick Del] [text "Delete"]]
- , div [alignCenter] [input [type_ "text", placeholder "Set your day goal!",onInput Input] [] , button [onClick SetGoal] [text "Set Goal"]]
- , div [checkboxStyle] (List.map viewCheckbox model.tasks)
- , div [alignCenter] (List.map viewCheckbox model.taskHistory)
- ]
- doneStyle : Html.Attribute msg
- doneStyle =
- style
- [
- ("padding","10px 0")
- ,("text-align","center")
- ]
- checkboxStyle : Html.Attribute msg
- checkboxStyle =
- style
- [
- ("padding","20px 0")
- ,("text-align","center")
- ]
- alignCenter : Html.Attribute msg
- alignCenter = style [("text-align","center")]
- viewMessage : String -> Html.Html msg
- viewMessage msg =
- div [] [ text msg ]
- viewCheckbox : String -> Html.Html Msg
- viewCheckbox msg =
- div [] [ input [ Html.Attributes.type_ "checkbox", onClick (DelCheckbox msg)] []
- , text msg
- ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement