diff --git a/part10/ElmHub.elm b/part10/ElmHub.elm index fae2f95..6b0bf84 100644 --- a/part10/ElmHub.elm +++ b/part10/ElmHub.elm @@ -1,4 +1,4 @@ -module ElmHub exposing (..) +port module ElmHub exposing (..) import Html exposing (..) import Html.Attributes exposing (class, target, href, defaultValue, type', checked, placeholder, value) @@ -60,6 +60,16 @@ initialModel = } +init : ( Model, Cmd Msg ) +init = + ( initialModel, githubSearch (getQueryString initialModel) ) + + +subscriptions : Model -> Sub Msg +subscriptions _ = + githubResponse decodeResponse + + type Msg = Search | Options OptionsMsg @@ -70,14 +80,14 @@ type Msg | DoNothing -update : (String -> Cmd Msg) -> Msg -> Model -> ( Model, Cmd Msg ) -update searchFeed msg model = +update : Msg -> Model -> ( Model, Cmd Msg ) +update msg model = case msg of Options optionsMsg -> ( { model | options = updateOptions optionsMsg model.options }, Cmd.none ) Search -> - ( model, searchFeed (getQueryString model) ) + ( model, githubSearch (getQueryString model) ) SetQuery query -> ( { model | query = query }, Cmd.none ) @@ -213,6 +223,22 @@ onChange toMsg = on "change" (Json.Decode.map toMsg Html.Events.targetValue) +decodeResponse : Json.Decode.Value -> Msg +decodeResponse json = + case Json.Decode.decodeValue responseDecoder json of + Err err -> + HandleSearchError (Just err) + + Ok results -> + HandleSearchResponse results + + +port githubSearch : String -> Cmd msg + + +port githubResponse : (Json.Decode.Value -> msg) -> Sub msg + + {-| NOTE: The following is not part of the exercise, but is food for thought if you have extra time. diff --git a/part10/Main.elm b/part10/Main.elm index 10bb678..32ce3e1 100644 --- a/part10/Main.elm +++ b/part10/Main.elm @@ -1,31 +1,14 @@ -port module Main exposing (..) +module Main exposing (main) -import ElmHub exposing (..) +import ElmHub import Html.App as Html -import Json.Decode main : Program Never main = Html.program - { view = view - , update = update githubSearch - , init = ( initialModel, githubSearch (getQueryString initialModel) ) - , subscriptions = \_ -> githubResponse decodeResponse + { view = ElmHub.view + , update = ElmHub.update + , init = ElmHub.init + , subscriptions = ElmHub.subscriptions } - - -decodeResponse : Json.Decode.Value -> Msg -decodeResponse json = - case Json.Decode.decodeValue responseDecoder json of - Err err -> - HandleSearchError (Just err) - - Ok results -> - HandleSearchResponse results - - -port githubSearch : String -> Cmd msg - - -port githubResponse : (Json.Decode.Value -> msg) -> Sub msg diff --git a/part9/ElmHub.elm b/part9/ElmHub.elm index 2adf79e..32de441 100644 --- a/part9/ElmHub.elm +++ b/part9/ElmHub.elm @@ -53,6 +53,16 @@ initialModel = } +init : ( Model, Cmd Msg ) +init = + ( initialModel, githubSearch (getQueryString initialModel.query) ) + + +subscriptions : Model -> Sub Msg +subscriptions _ = + githubResponse decodeResponse + + view : Model -> Html Msg view model = div [ class "content" ] diff --git a/part9/Main.elm b/part9/Main.elm index 6e47a29..32ce3e1 100644 --- a/part9/Main.elm +++ b/part9/Main.elm @@ -1,14 +1,14 @@ -module Main exposing (..) +module Main exposing (main) -import ElmHub exposing (..) +import ElmHub import Html.App as Html main : Program Never main = Html.program - { view = view - , update = update - , init = ( initialModel, githubSearch (getQueryString initialModel.query) ) - , subscriptions = \_ -> githubResponse decodeResponse + { view = ElmHub.view + , update = ElmHub.update + , init = ElmHub.init + , subscriptions = ElmHub.subscriptions }