From 7f1ef778b95c36f06abec4cb60e5bb7e5778b1f0 Mon Sep 17 00:00:00 2001 From: Jen-Mei Wu Date: Thu, 7 Mar 2019 23:07:03 -0800 Subject: [PATCH] =?UTF-8?q?Separating=20`Navigation.Key`=20from=20`update`?= =?UTF-8?q?=20function=20to=20make=20it=20testable.=20Unfortunately,=20`Na?= =?UTF-8?q?vigation.Key`=20is=20passed=20in=20from=20the=20runtime=20and?= =?UTF-8?q?=20there=E2=80=99s=20no=20way=20to=20create=20a=20testing=20stu?= =?UTF-8?q?b=20for=20it=20currently.=20See=20https://github.com/elm-explor?= =?UTF-8?q?ations/test/issues/24=20for=20updates.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/calliope/src/Main.elm | 75 ++++++++++++++++++++++++------------ 1 file changed, 51 insertions(+), 24 deletions(-) diff --git a/client/calliope/src/Main.elm b/client/calliope/src/Main.elm index c65f2cb..c7fec3a 100644 --- a/client/calliope/src/Main.elm +++ b/client/calliope/src/Main.elm @@ -31,9 +31,9 @@ import Url.Builder main = Browser.application { init = init - , update = update + , update = updateWithKey , subscriptions = subscriptions - , view = view + , view = viewWithKey , onUrlRequest = onUrlRequest , onUrlChange = onUrlChange } @@ -54,15 +54,15 @@ reactor = url = Url.Builder.absolute [] [] - reactorInit : () -> Url.Url -> Navigation.Key -> ( Model, Cmd Msg ) + reactorInit : () -> Url.Url -> Navigation.Key -> ( ModelWithKey, Cmd Msg ) reactorInit _ = init 800 in Browser.application { init = reactorInit - , update = update + , update = updateWithKey , subscriptions = subscriptions - , view = view + , view = viewWithKey , onUrlRequest = onUrlRequest , onUrlChange = onUrlChange } @@ -91,9 +91,14 @@ type alias RawSearchForm = } -type alias Model = +type alias ModelWithKey = { key : Navigation.Key - , url : Url.Url + , model : Model + } + + +type alias Model = + { url : Url.Url , gmailUrl : String , searchForm : SearchForm , rawSearchForm : RawSearchForm @@ -152,7 +157,7 @@ type alias SearchResults = } -init : Int -> Url.Url -> Navigation.Key -> ( Model, Cmd Msg ) +init : Int -> Url.Url -> Navigation.Key -> ( ModelWithKey, Cmd Msg ) init width url key = let defaultSearchForm = @@ -177,15 +182,17 @@ init width url key = SearchResults "" [] [] in ( { key = key - , url = url - , gmailUrl = "https://mail.google.com/mail/" - , searchForm = defaultSearchForm - , rawSearchForm = defaultRawSearchForm - , searchResults = emptySearchResults - , expandedMessageId = "" - , searchStatus = Empty - , showAdvancedSearch = False - , windowWidth = width + , model = + { url = url + , gmailUrl = "https://mail.google.com/mail/" + , searchForm = defaultSearchForm + , rawSearchForm = defaultRawSearchForm + , searchResults = emptySearchResults + , expandedMessageId = "" + , searchStatus = Empty + , showAdvancedSearch = False + , windowWidth = width + } } , Cmd.none ) @@ -226,21 +233,36 @@ type Msg | UrlChanged Url.Url -update : Msg -> Model -> ( Model, Cmd Msg ) -update msg model = +updateWithKey : Msg -> ModelWithKey -> ( ModelWithKey, Cmd Msg ) +updateWithKey msg modelWithKey = case msg of UrlChangeRequested urlRequest -> case urlRequest of Internal url -> - ( model - , Navigation.pushUrl model.key (Url.toString url) + ( modelWithKey + , Navigation.pushUrl modelWithKey.key (Url.toString url) ) External url -> - ( model + ( modelWithKey , Navigation.load url ) + _ -> + let + ( newModel, newMsg ) = + update msg modelWithKey.model + in + ( { modelWithKey | model = newModel }, newMsg ) + + +update : Msg -> Model -> ( Model, Cmd Msg ) +update msg model = + case msg of + UrlChangeRequested _ -> + -- Should never happen + ( model, Cmd.none ) + UrlChanged url -> ( { model | url = url }, Cmd.none ) @@ -409,8 +431,8 @@ updateSearchForm msg model = -- SUBSCRIPTIONS -subscriptions : Model -> Sub Msg -subscriptions model = +subscriptions : ModelWithKey -> Sub Msg +subscriptions _ = Browser.Events.onResize (\x y -> Resize x y) @@ -419,6 +441,11 @@ subscriptions model = -- VIEW +viewWithKey : ModelWithKey -> Document Msg +viewWithKey modelWithKey = + view modelWithKey.model + + view : Model -> Document Msg view model = { title = "Calliope – therapy for email monsters"