Skip to content

Commit

Permalink
Separating Navigation.Key from update function to make it testabl…
Browse files Browse the repository at this point in the history
…e. Unfortunately, `Navigation.Key` is passed in from the runtime and there’s no way to create a testing stub for it currently. See elm-explorations/test#24 for updates.
  • Loading branch information
jenmei committed Mar 8, 2019
1 parent 7f5cf41 commit 7f1ef77
Showing 1 changed file with 51 additions and 24 deletions.
75 changes: 51 additions & 24 deletions client/calliope/src/Main.elm
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand All @@ -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
)
Expand Down Expand Up @@ -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 )

Expand Down Expand Up @@ -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)


Expand All @@ -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"
Expand Down

0 comments on commit 7f1ef77

Please sign in to comment.