From 431b350858fed7c2b03d7761f9004cb45f2b6edd Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Tue, 8 Oct 2019 21:12:17 -0700 Subject: [PATCH] Wire through (Maybe (PagePath pathKey)) as init argument. --- elm-package/src/Pages/ContentCache.elm | 15 +++++++------- elm-package/src/Pages/Platform.elm | 28 ++++++++++++++++++-------- examples/docs/src/Main.elm | 4 ++-- generator/src/elm-file-constants.js | 4 ++-- 4 files changed, 32 insertions(+), 19 deletions(-) diff --git a/elm-package/src/Pages/ContentCache.elm b/elm-package/src/Pages/ContentCache.elm index ae8f52d1..9db6a6ec 100644 --- a/elm-package/src/Pages/ContentCache.elm +++ b/elm-package/src/Pages/ContentCache.elm @@ -432,22 +432,23 @@ lookup pathKey content url = lookupMetadata : - ContentCache metadata view + pathKey + -> ContentCache metadata view -> Url - -> Maybe metadata -lookupMetadata content url = - lookup () content url + -> Maybe ( PagePath pathKey, metadata ) +lookupMetadata pathKey content url = + lookup pathKey content url |> Maybe.map (\( pagePath, entry ) -> case entry of NeedContent _ metadata -> - metadata + ( pagePath, metadata ) Unparsed _ metadata _ -> - metadata + ( pagePath, metadata ) Parsed metadata _ -> - metadata + ( pagePath, metadata ) ) diff --git a/elm-package/src/Pages/Platform.elm b/elm-package/src/Pages/Platform.elm index f67db735..032932e2 100644 --- a/elm-package/src/Pages/Platform.elm +++ b/elm-package/src/Pages/Platform.elm @@ -177,28 +177,37 @@ init : -> (Json.Encode.Value -> Cmd (Msg userMsg metadata view)) -> (metadata -> List (Head.Tag pathKey)) -> Content - -> ( userModel, Cmd userMsg ) + -> (Maybe (PagePath pathKey) -> ( userModel, Cmd userMsg )) -> Flags -> Url -> Browser.Navigation.Key -> ( ModelDetails userModel metadata view, Cmd (Msg userMsg metadata view) ) init pathKey canonicalSiteUrl document toJsPort head content initUserModel flags url key = let - ( userModel, userCmd ) = - initUserModel - contentCache = ContentCache.init document content in case contentCache of Ok okCache -> + let + ( userModel, userCmd ) = + initUserModel maybePagePath + + ( maybePagePath, maybeMetadata ) = + case ContentCache.lookupMetadata pathKey (Ok okCache) url of + Just ( pagePath, metadata ) -> + ( Just pagePath, Just metadata ) + + Nothing -> + ( Nothing, Nothing ) + in ( { key = key , url = url , userModel = userModel , contentCache = contentCache } , Cmd.batch - ([ ContentCache.lookupMetadata (Ok okCache) url + ([ maybeMetadata |> Maybe.map head |> Maybe.map (encodeHeads canonicalSiteUrl url.path) |> Maybe.map toJsPort @@ -213,6 +222,10 @@ init pathKey canonicalSiteUrl document toJsPort head content initUserModel flags ) Err _ -> + let + ( userModel, userCmd ) = + initUserModel Nothing + in ( { key = key , url = url , userModel = userModel @@ -332,7 +345,7 @@ type alias Parser metadata view = application : - { init : ( userModel, Cmd userMsg ) + { init : Maybe (PagePath pathKey) -> ( userModel, Cmd userMsg ) , update : userMsg -> userModel -> ( userModel, Cmd userMsg ) , subscriptions : userModel -> Sub userMsg , view : userModel -> List ( PagePath pathKey, metadata ) -> Page metadata view pathKey -> { title : String, body : Html userMsg } @@ -385,7 +398,7 @@ application config = cliApplication : - { init : ( userModel, Cmd userMsg ) + { init : Maybe (PagePath pathKey) -> ( userModel, Cmd userMsg ) , update : userMsg -> userModel -> ( userModel, Cmd userMsg ) , subscriptions : userModel -> Sub userMsg , view : userModel -> List ( PagePath pathKey, metadata ) -> Page metadata view pathKey -> { title : String, body : Html userMsg } @@ -444,4 +457,3 @@ encodeErrors errors = |> Json.Encode.dict (\path -> "/" ++ String.join "/" path) (\errorsForPath -> Json.Encode.string errorsForPath) - diff --git a/examples/docs/src/Main.elm b/examples/docs/src/Main.elm index 94a8bc07..1096181d 100644 --- a/examples/docs/src/Main.elm +++ b/examples/docs/src/Main.elm @@ -78,8 +78,8 @@ type alias Model = {} -init : ( Model, Cmd Msg ) -init = +init : Maybe (PagePath Pages.PathKey) -> ( Model, Cmd Msg ) +init maybePagePath = ( Model, Cmd.none ) diff --git a/generator/src/elm-file-constants.js b/generator/src/elm-file-constants.js index fe81898c..59bae28c 100644 --- a/generator/src/elm-file-constants.js +++ b/generator/src/elm-file-constants.js @@ -89,7 +89,7 @@ port toJsPort : Json.Encode.Value -> Cmd msg application : - { init : ( userModel, Cmd userMsg ) + { init : Maybe (PagePath PathKey) -> ( userModel, Cmd userMsg ) , update : userMsg -> userModel -> ( userModel, Cmd userMsg ) , subscriptions : userModel -> Sub userMsg , view : userModel -> List ( PagePath PathKey, metadata ) -> Page metadata view PathKey -> { title : String, body : Html userMsg } @@ -169,7 +169,7 @@ port toJsPort : Json.Encode.Value -> Cmd msg application : - { init : ( userModel, Cmd userMsg ) + { init : Maybe (PagePath PathKey) -> ( userModel, Cmd userMsg ) , update : userMsg -> userModel -> ( userModel, Cmd userMsg ) , subscriptions : userModel -> Sub userMsg , view : userModel -> List ( PagePath PathKey, metadata ) -> Page metadata view PathKey -> { title : String, body : Html userMsg }