<commit_before>module Main exposing (..)

import Html exposing (..)
import Html.App as App
import Html.Attributes exposing (..)


-- We need to import the `on` event from Html.Events, and we'll need keyCode
-- later so let's add it as well.

import Html.Events exposing (on, keyCode, onInput, onCheck)


-- We need to use a Json Decoder to extract our keyCode from the `on` event.
-- For the most part we'll gloss over this for now - there are not many events
-- where you actually need to use this early on.

import Json.Decode as Json


-- We define a function called is13 that takes an int and returns a result.
-- The types are 'error type' and 'success type' but, again, we'll talk more
-- about these types in the future.


is13 : Int -> Result String ()
is13 code =
    -- If it's 13, then we return `Ok ()` where that second thing is called 'the
    -- unit type' but you can think of it as 'this stands in for a value we don't
    -- care about so much'.  Sometimes your OK needs a type, but here as long as
    -- we're returning an Ok result, things will go well.
    -- If it's not a 13, we return an `Err` with a string explaining the error.
    -- We won't handle this error, but it's nice to see that you can type your
    -- errors as well.
    if code == 13 then
        Ok ()
    else
        Err "not the right key code"


handleKeyPress : Json.Decoder Msg
handleKeyPress =
    Json.map (always Add) (Json.customDecoder keyCode is13)



-- We have a todo


type alias Todo =
    { title : String
    , completed : Bool
    , editing : Bool
    , identifier : Int
    }



-- We have the filter state for the application


type FilterState
    = All
    | Active
    | Completed



-- We have the entire application state's model


type alias Model =
    { todos : List Todo
    , todo : Todo
    , filter : FilterState
    , nextIdentifier : Int
    }



-- We have the messages that can occur


type Msg
    = Add
    | ToggleCompletion Todo Bool
    | Delete Todo
    | UpdateField String
    | Filter FilterState


newTodo : Todo
newTodo =
    { title = ""
    , completed = False
    , editing = False
    , identifier = 0
    }


initialModel =
    { todos =
        [ { title = "The first todo"
          , completed = False
          , editing = False
          , identifier = 1
          }
        ]
    , todo = { newTodo | identifier = 2 }
    , filter = All
    , nextIdentifier = 3
    }


update : Msg -> Model -> Model
update msg model =
    case msg of
        Add ->
            { model
                | todos = model.todo :: model.todos
                , todo = { newTodo | identifier = model.nextIdentifier }
                , nextIdentifier = model.nextIdentifier + 1
            }

        ToggleCompletion todo checked ->
            let
                updateTodo thisTodo =
                    if thisTodo.identifier == todo.identifier then
                        { todo | completed = checked }
                    else
                        thisTodo
            in
                { model | todos = List.map updateTodo model.todos }

        Delete todo ->
            model

        Filter filterState ->
            model

        UpdateField str ->
            let
                todo =
                    model.todo

                updatedTodo =
                    { todo | title = str }
            in
                { model | todo = updatedTodo }


todoView : Todo -> Html Msg
todoView todo =
    -- We will give the li the class "completed" if the todo is completed
    li [ classList [ ( "completed", todo.completed ) ] ]
        [ div [ class "view" ]
            -- We will check the checkbox if the todo is completed
            [ input
                [ class "toggle"
                , type' "checkbox"
                , checked todo.completed
                , onCheck (\checked -> ToggleCompletion todo checked)
                ]
                []
              -- We will use the todo's title as the label text
            , label [] [ text todo.title ]
            , button [ class "destroy" ] []
            ]
        ]


view : Model -> Html Msg
view model =
    div []
        [ node "style" [ type' "text/css" ] [ text styles ]
        , section [ class "todoapp" ]
            [ header [ class "header" ]
                [ h1 [] [ text "todos" ]
                , input
                    [ class "new-todo"
                    , placeholder "What needs to be done?"
                    , autofocus True
                    , value model.todo.title
                    , on "keypress" handleKeyPress
                    , onInput UpdateField
                    ]
                    []
                ]
            , section [ class "main" ]
                [ ul [ class "todo-list" ]
                    (List.map todoView model.todos)
                ]
            ]
        ]


main =
    App.beginnerProgram
        { model = initialModel
        , update = update
        , view = view
        }


styles : String
styles =
    """

  html,<commit_msg>Added filtering<commit_after>module Main exposing (..)

import Html exposing (..)
import Html.App as App
import Html.Attributes exposing (..)


-- We need to import the `on` event from Html.Events, and we'll need keyCode
-- later so let's add it as well.

import Html.Events exposing (..)


--import Html.Events exposing (on, keyCode, onInput, onCheck)
-- We need to use a Json Decoder to extract our keyCode from the `on` event.
-- For the most part we'll gloss over this for now - there are not many events
-- where you actually need to use this early on.

import Json.Decode as Json


-- We define a function called is13 that takes an int and returns a result.
-- The types are 'error type' and 'success type' but, again, we'll talk more
-- about these types in the future.


is13 : Int -> Result String ()
is13 code =
    -- If it's 13, then we return `Ok ()` where that second thing is called 'the
    -- unit type' but you can think of it as 'this stands in for a value we don't
    -- care about so much'.  Sometimes your OK needs a type, but here as long as
    -- we're returning an Ok result, things will go well.
    -- If it's not a 13, we return an `Err` with a string explaining the error.
    -- We won't handle this error, but it's nice to see that you can type your
    -- errors as well.
    if code == 13 then
        Ok ()
    else
        Err "not the right key code"


handleKeyPress : Json.Decoder Msg
handleKeyPress =
    Json.map (always Add) (Json.customDecoder keyCode is13)



-- We have a todo


type alias Todo =
    { title : String
    , completed : Bool
    , editing : Bool
    , identifier : Int
    }



-- We have the filter state for the application


type FilterState
    = All
    | Active
    | Completed



-- We have the entire application state's model


type alias Model =
    { todos : List Todo
    , todo : Todo
    , filter : FilterState
    , nextIdentifier : Int
    }



-- We have the messages that can occur


type Msg
    = Add
    | ToggleCompletion Todo Bool
    | Delete Todo
    | UpdateField String
    | Filter FilterState


newTodo : Todo
newTodo =
    { title = ""
    , completed = False
    , editing = False
    , identifier = 0
    }


initialModel =
    { todos =
        [ { title = "The first todo"
          , completed = False
          , editing = False
          , identifier = 1
          }
        ]
    , todo = { newTodo | identifier = 2 }
    , filter = All
    , nextIdentifier = 3
    }


update : Msg -> Model -> Model
update msg model =
    case msg of
        Add ->
            { model
                | todos = model.todo :: model.todos
                , todo = { newTodo | identifier = model.nextIdentifier }
                , nextIdentifier = model.nextIdentifier + 1
            }

        ToggleCompletion todo checked ->
            let
                updateTodo thisTodo =
                    if thisTodo.identifier == todo.identifier then
                        { todo | completed = checked }
                    else
                        thisTodo
            in
                { model | todos = List.map updateTodo model.todos }

        Delete todo ->
            model

        Filter filterState ->
            { model | filter = filterState }

        UpdateField str ->
            let
                todo =
                    model.todo

                updatedTodo =
                    { todo | title = str }
            in
                { model | todo = updatedTodo }


filteredTodos : Model -> List Todo
filteredTodos model =
    let
        matchesFilter =
            case model.filter of
                All ->
                    (\_ -> True)

                Active ->
                    (\todo -> todo.completed == False)

                Completed ->
                    (\todo -> todo.completed == True)
    in
        List.filter matchesFilter model.todos


todoView : Todo -> Html Msg
todoView todo =
    -- We will give the li the class "completed" if the todo is completed
    li [ classList [ ( "completed", todo.completed ) ] ]
        [ div [ class "view" ]
            -- We will check the checkbox if the todo is completed
            [ input
                [ class "toggle"
                , type' "checkbox"
                , checked todo.completed
                , onCheck (\checked -> ToggleCompletion todo checked)
                ]
                []
              -- We will use the todo's title as the label text
            , label [] [ text todo.title ]
            , button [ class "destroy" ] []
            ]
        ]


filterItemView : Model -> FilterState -> Html Msg
filterItemView model filterState =
    li []
        [ a
            [ classList [ ( "selected", (model.filter == filterState) ) ]
            , href "#"
            , onClick (Filter filterState)
            ]
            [ text (toString filterState) ]
        ]


view : Model -> Html Msg
view model =
    div []
        [ node "style" [ type' "text/css" ] [ text styles ]
        , section [ class "todoapp" ]
            [ header [ class "header" ]
                [ h1 [] [ text "todos" ]
                , input
                    [ class "new-todo"
                    , placeholder "What needs to be done?"
                    , autofocus True
                    , value model.todo.title
                    , on "keypress" handleKeyPress
                    , onInput UpdateField
                    ]
                    []
                ]
            , section [ class "main" ]
                [ ul [ class "todo-list" ]
                    (List.map todoView (filteredTodos model))
                ]
            ]
        , footer [ class "footer" ]
            [ span [ class "todo-count" ]
                [ strong []
                    [ text
                        (toString
                            (List.length
                                (List.filter (\todo -> todo.completed == False) model.todos)
                            )
                        )
                    , text " items left"
                    ]
                ]
            , ul [ class "filters" ]
                [ filterItemView model All
                , filterItemView model Active
                , filterItemView model Completed
                ]
            , button [ class "clear-completed" ] [ text "Clear completed" ]
            ]
        ]


main =
    App.beginnerProgram
        { model = initialModel
        , update = update
        , view = view
        }


styles : String
styles =
    """

  html,
{'fraction_non_alphanumeric': 0.08282235080749632, 'fraction_numerical': 0.0034545297521374903, 'mean_word_length': 1.3608562691131498, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3990153', 'n_tokens_mistral': 3201, 'n_tokens_neox': 2959, 'n_words': 1118}