Wire in staticData to update with some hardcoding and Debug.todos.

This commit is contained in:
Dillon Kearns 2021-04-15 10:23:01 -07:00
parent d3a41a812e
commit 9453b5fb41
6 changed files with 132 additions and 69 deletions

View File

@ -1,4 +1,4 @@
module Template.Slide.Number_ exposing (Model, Msg, template)
module Template.Slide.Number_ exposing (Model, Msg, StaticData, template)
import Browser.Events
import Browser.Navigation
@ -51,7 +51,7 @@ template =
{ view = view
, init = \routeParams -> ( (), Cmd.none )
, update =
\sharedModel routeParams msg model ->
\sharedModel pageStaticData routeParams msg model ->
case msg of
OnKeyPress (Just direction) ->
let
@ -59,12 +59,16 @@ template =
String.toInt routeParams.number |> Maybe.withDefault 0
nextSlide =
case direction of
Right ->
currentSlide + 1
clamp
1
(pageStaticData.totalCount - 1)
(case direction of
Right ->
currentSlide + 1
Left ->
currentSlide - 1
Left ->
currentSlide - 1
)
in
( model
, sharedModel.navigationKey

View File

@ -65,7 +65,7 @@ type alias TemplateWithState routeParams templateStaticData templateModel templa
StaticPayload templateStaticData routeParams
-> List Head.Tag
, init : routeParams -> ( templateModel, Cmd templateMsg )
, update : Maybe Browser.Navigation.Key -> routeParams -> templateMsg -> templateModel -> Shared.Model -> ( templateModel, Cmd templateMsg, Maybe Shared.SharedMsg )
, update : templateStaticData -> Maybe Browser.Navigation.Key -> routeParams -> templateMsg -> templateModel -> Shared.Model -> ( templateModel, Cmd templateMsg, Maybe Shared.SharedMsg )
, subscriptions : routeParams -> PagePath -> templateModel -> Shared.Model -> Sub templateMsg
}
@ -111,7 +111,7 @@ buildNoState { view } builderState =
, staticData = record.staticData
, staticRoutes = record.staticRoutes
, init = \_ -> ( (), Cmd.none )
, update = \_ _ _ _ _ -> ( (), Cmd.none, Nothing )
, update = \_ _ _ _ _ _ -> ( (), Cmd.none, Nothing )
, subscriptions = \_ _ _ _ -> Sub.none
}
@ -124,7 +124,7 @@ buildWithLocalState :
-> StaticPayload templateStaticData routeParams
-> Document templateMsg
, init : routeParams -> ( templateModel, Cmd templateMsg )
, update : DynamicContext Shared.Model -> routeParams -> templateMsg -> templateModel -> ( templateModel, Cmd templateMsg )
, update : DynamicContext Shared.Model -> templateStaticData -> routeParams -> templateMsg -> templateModel -> ( templateModel, Cmd templateMsg )
, subscriptions : routeParams -> PagePath -> templateModel -> Sub templateMsg
}
-> Builder routeParams templateStaticData
@ -140,13 +140,14 @@ buildWithLocalState config builderState =
, staticRoutes = record.staticRoutes
, init = config.init
, update =
\navigationKey routeParams msg templateModel sharedModel ->
\staticData navigationKey routeParams msg templateModel sharedModel ->
let
( updatedModel, cmd ) =
config.update
{ navigationKey = navigationKey
, sharedModel = sharedModel
}
staticData
routeParams
msg
templateModel
@ -186,7 +187,7 @@ buildWithSharedState config builderState =
, staticRoutes = record.staticRoutes
, init = config.init
, update =
\navigationKey routeParams msg templateModel sharedModel ->
\pageStaticData navigationKey routeParams msg templateModel sharedModel ->
config.update
{ navigationKey = navigationKey
, sharedModel = sharedModel

View File

@ -85,6 +85,10 @@ type Msg
.join(" | ")}
type PageStaticData
= DataSlide__Number_ Template.Slide.Number_.StaticData
view :
{ path : PagePath
, frontmatter : Maybe Route
@ -148,6 +152,7 @@ view page =
init :
Maybe Shared.Model
-> PageStaticData
-> Maybe Browser.Navigation.Key
->
Maybe
@ -159,7 +164,7 @@ init :
, metadata : Maybe Route
}
-> ( Model, Cmd Msg )
init currentGlobalModel navigationKey maybePagePath =
init currentGlobalModel pageStaticData navigationKey maybePagePath =
let
( sharedModel, globalCmd ) =
currentGlobalModel |> Maybe.map (\\m -> ( m, Cmd.none )) |> Maybe.withDefault (Shared.template.init navigationKey maybePagePath)
@ -195,8 +200,8 @@ init currentGlobalModel navigationKey maybePagePath =
update : Maybe Browser.Navigation.Key -> Msg -> Model -> ( Model, Cmd Msg )
update navigationKey msg model =
update : PageStaticData -> Maybe Browser.Navigation.Key -> Msg -> Model -> ( Model, Cmd Msg )
update pageStaticData navigationKey msg model =
case msg of
MsgGlobal msg_ ->
let
@ -208,7 +213,7 @@ update navigationKey msg model =
)
OnPageChange record ->
(init (Just model.global) navigationKey <|
(init (Just model.global) pageStaticData navigationKey <|
Just
{ path =
{ path = record.path
@ -249,13 +254,16 @@ update navigationKey msg model =
Msg${pathNormalizedName(name)} msg_ ->
let
( updatedPageModel, pageCmd, ( newGlobalModel, newGlobalCmd ) ) =
case ( model.page, model.current |> Maybe.andThen .metadata ) of
case ( model.page, pageStaticData, model.current |> Maybe.andThen .metadata ) of
( Model${pathNormalizedName(
name
)} pageModel, Just (Route.${routeHelpers.routeVariant(
)} pageModel, Data${pathNormalizedName(
name
)} thisPageData, Just (Route.${routeHelpers.routeVariant(
name
)} routeParams) ) ->
Template.${moduleName(name)}.template.update
thisPageData
navigationKey
routeParams
msg_
@ -315,7 +323,7 @@ templateSubscriptions route path model =
Sub.none
main : Pages.Internal.Platform.Program Model Msg (Maybe Route)
main : Pages.Internal.Platform.Program Model Msg (Maybe Route) PageStaticData
main =
Pages.Internal.Platform.${
phase === "browser" ? "application" : "cliApplication"
@ -337,6 +345,7 @@ main =
, canonicalSiteUrl = "TODO"
, toJsPort = toJsPort
, fromJsPort = fromJsPort identity
, staticData = staticDataForRoute
, generateFiles =
getStaticRoutes
|> StaticHttp.andThen
@ -352,7 +361,17 @@ main =
)
}
staticDataForRoute : Maybe Route -> StaticHttp.Request PageStaticData
staticDataForRoute route =
case route of
Just (Route.Slide__Number_ routeParams) ->
Template.Slide.Number_.template.staticData routeParams
|> StaticHttp.map DataSlide__Number_
Nothing ->
StaticHttp.fail ""
getStaticRoutes : StaticHttp.Request (List (Maybe Route))
getStaticRoutes =
StaticHttp.combine
[ StaticHttp.succeed

View File

@ -3,6 +3,7 @@ module Pages.Internal.Platform exposing (Flags, Model, Msg, Program, application
import Browser
import Browser.Dom as Dom
import Browser.Navigation
import BuildError exposing (BuildError)
import Html exposing (Html)
import Html.Attributes
import Http
@ -19,13 +20,13 @@ import Task
import Url exposing (Url)
type alias Program userModel userMsg route =
Platform.Program Flags (Model userModel route) (Msg userMsg)
type alias Program userModel userMsg route pageStaticData =
Platform.Program Flags (Model userModel route pageStaticData) (Msg userMsg)
mainView :
ProgramConfig userMsg userModel route siteStaticData
-> ModelDetails userModel
ProgramConfig userMsg userModel route siteStaticData pageStaticData
-> ModelDetails userModel pageStaticData
-> { title : String, body : Html userMsg }
mainView config model =
let
@ -98,8 +99,8 @@ urlToPagePath url baseUrl =
view :
ProgramConfig userMsg userModel route siteStaticData
-> ModelDetails userModel
ProgramConfig userMsg userModel route siteStaticData pageStaticData
-> ModelDetails userModel pageStaticData
-> Browser.Document (Msg userMsg)
view config model =
let
@ -143,23 +144,12 @@ contentJsonDecoder =
init :
(Url -> route)
->
(Maybe
{ metadata : route
, path :
{ path : PagePath
, query : Maybe String
, fragment : Maybe String
}
}
-> ( userModel, Cmd userMsg )
)
ProgramConfig userMsg userModel route staticData pageStaticData
-> Flags
-> Url
-> Browser.Navigation.Key
-> ( ModelDetails userModel, Cmd (AppMsg userMsg) )
init urlToRoute initUserModel flags url key =
-> ( ModelDetails userModel pageStaticData, Cmd (AppMsg userMsg) )
init config flags url key =
let
contentCache =
ContentCache.init
@ -216,6 +206,31 @@ init urlToRoute initUserModel flags url key =
Err _ ->
DevClient False
justContentJson : RequestsAndPending
justContentJson =
case contentJson of
Nothing ->
Debug.todo "Expected content.json"
Just justValue ->
justValue.staticData
pageStaticDataResult : Result BuildError pageStaticData
pageStaticDataResult =
StaticHttpRequest.resolve ApplicationType.Browser
(config.staticData (config.urlToRoute url))
justContentJson
|> Result.mapError (StaticHttpRequest.toBuildError url.path)
pageStaticData : pageStaticData
pageStaticData =
case pageStaticDataResult of
Ok okPageStaticData ->
okPageStaticData
Err error ->
Debug.todo (BuildError.errorToString error)
( userModel, userCmd ) =
Maybe.map
(\pagePath ->
@ -224,11 +239,11 @@ init urlToRoute initUserModel flags url key =
, query = url.query
, fragment = url.fragment
}
, metadata = urlToRoute url
, metadata = config.urlToRoute url
}
)
maybePagePath
|> initUserModel
|> config.init pageStaticData (Just key)
cmd =
[ userCmd
@ -256,14 +271,18 @@ init urlToRoute initUserModel flags url key =
, userModel = userModel
, contentCache = contentCache
, phase = phase
, pageStaticData = pageStaticData
}
, cmd
)
Err _ ->
let
pageStaticData =
Debug.todo ""
( userModel, userCmd ) =
initUserModel Nothing
config.init pageStaticData (Just key) Nothing
in
( { key = key
, url = url
@ -271,6 +290,7 @@ init urlToRoute initUserModel flags url key =
, userModel = userModel
, contentCache = contentCache
, phase = DevClient False
, pageStaticData = pageStaticData
}
, Cmd.batch
[ userCmd |> Cmd.map UserMsg
@ -295,18 +315,19 @@ type AppMsg userMsg
| StartingHotReload
type Model userModel route
= Model (ModelDetails userModel)
type Model userModel route pageStaticData
= Model (ModelDetails userModel pageStaticData)
| CliModel (Pages.Internal.Platform.Cli.Model route)
type alias ModelDetails userModel =
type alias ModelDetails userModel pageStaticData =
{ key : Browser.Navigation.Key
, url : Url
, baseUrl : Url
, contentCache : ContentCache
, userModel : userModel
, phase : Phase
, pageStaticData : pageStaticData
}
@ -317,11 +338,11 @@ type Phase
update :
ProgramConfig userMsg userModel route siteStaticData
ProgramConfig userMsg userModel route siteStaticData pageStaticData
-> (Maybe Browser.Navigation.Key -> userMsg -> userModel -> ( userModel, Cmd userMsg ))
-> Msg userMsg
-> ModelDetails userModel
-> ( ModelDetails userModel, Cmd (AppMsg userMsg) )
-> ModelDetails userModel pageStaticData
-> ( ModelDetails userModel pageStaticData, Cmd (AppMsg userMsg) )
update config userUpdate msg model =
case msg of
AppMsg appMsg ->
@ -449,13 +470,13 @@ update config userUpdate msg model =
application :
ProgramConfig userMsg userModel route staticData
-> Platform.Program Flags (Model userModel route) (Msg userMsg)
ProgramConfig userMsg userModel route staticData pageStaticData
-> Platform.Program Flags (Model userModel route pageStaticData) (Msg userMsg)
application config =
Browser.application
{ init =
\flags url key ->
init config.urlToRoute (config.init (Just key)) flags url key
init config flags url key
|> Tuple.mapFirst Model
|> Tuple.mapSecond (Cmd.map AppMsg)
, view =
@ -479,7 +500,7 @@ application config =
noOpUpdate
_ ->
config.update
config.update model.pageStaticData
noOpUpdate =
\_ _ userModel ->
@ -545,8 +566,8 @@ application config =
cliApplication :
ProgramConfig userMsg userModel route staticData
-> Program userModel userMsg route
ProgramConfig userMsg userModel route staticData pageStaticData
-> Program userModel userMsg route pageStaticData
cliApplication =
Pages.Internal.Platform.Cli.cliApplication CliMsg
(\msg ->

View File

@ -7,7 +7,6 @@ module Pages.Internal.Platform.Cli exposing
, update
)
import Browser.Navigation
import BuildError exposing (BuildError)
import Codec
import Dict exposing (Dict)
@ -29,7 +28,6 @@ import Pages.Internal.Platform.ToJsPayload as ToJsPayload exposing (ToJsSuccessP
import Pages.Internal.StaticHttpBody as StaticHttpBody
import Pages.PagePath as PagePath exposing (PagePath)
import Pages.ProgramConfig exposing (ProgramConfig)
import Pages.SiteConfig exposing (SiteConfig)
import Pages.StaticHttp as StaticHttp exposing (RequestDetails)
import Pages.StaticHttpRequest as StaticHttpRequest
import SecretsDict exposing (SecretsDict)
@ -67,7 +65,7 @@ cliApplication :
-> (msg -> Maybe Msg)
-> (Model route -> model)
-> (model -> Maybe (Model route))
-> ProgramConfig userMsg userModel route siteStaticData
-> ProgramConfig userMsg userModel route siteStaticData pageStaticData
-> Platform.Program Flags model msg
cliApplication cliMsgConstructor narrowMsg toModel fromModel config =
let
@ -171,7 +169,7 @@ asJsonView x =
Json.Encode.string "REPLACE_ME_WITH_JSON_STRINGIFY"
perform : Maybe Decode.Value -> ProgramConfig userMsg userModel route siteStaticData -> (Msg -> msg) -> (Json.Encode.Value -> Cmd Never) -> Effect -> Cmd msg
perform : Maybe Decode.Value -> ProgramConfig userMsg userModel route siteStaticData pageStaticData -> (Msg -> msg) -> (Json.Encode.Value -> Cmd Never) -> Effect -> Cmd msg
perform maybeRequest config cliMsgConstructor toJsPort effect =
case effect of
Effect.NoEffect ->
@ -322,7 +320,7 @@ init :
Maybe Decode.Value
-> (Model route -> model)
-> ContentCache
-> ProgramConfig userMsg userModel route siteStaticData
-> ProgramConfig userMsg userModel route siteStaticData pageStaticData
-> Decode.Value
-> ( model, Effect )
init maybeRequestJson toModel contentCache config flags =
@ -369,7 +367,7 @@ type alias RequestPayload route =
requestPayloadDecoder :
ProgramConfig userMsg userModel route siteStaticData
ProgramConfig userMsg userModel route siteStaticData pageStaticData
-> Decode.Decoder (Maybe (RequestPayload route))
requestPayloadDecoder config =
optionalField "request"
@ -422,7 +420,7 @@ initLegacy :
-> { a | secrets : SecretsDict, mode : Mode, staticHttpCache : Dict String (Maybe String) }
-> (Model route -> model)
-> ContentCache
-> ProgramConfig userMsg userModel route siteStaticData
-> ProgramConfig userMsg userModel route siteStaticData pageStaticData
-> Decode.Value
-> ( model, Effect )
initLegacy maybeRequestJson { secrets, mode, staticHttpCache } toModel contentCache config flags =
@ -468,7 +466,7 @@ initLegacy maybeRequestJson { secrets, mode, staticHttpCache } toModel contentCa
updateAndSendPortIfDone :
ContentCache
-> ProgramConfig userMsg userModel route siteStaticData
-> ProgramConfig userMsg userModel route siteStaticData pageStaticData
-> Model route
-> (Model route -> model)
-> ( model, Effect )
@ -491,7 +489,7 @@ updateAndSendPortIfDone contentCache config model toModel =
update :
ContentCache
-> ProgramConfig userMsg userModel route siteStaticData
-> ProgramConfig userMsg userModel route siteStaticData pageStaticData
-> Msg
-> Model route
-> ( Model route, Effect )
@ -665,7 +663,7 @@ update contentCache config msg model =
nextStepToEffect :
ContentCache
-> ProgramConfig userMsg userModel route siteStaticData
-> ProgramConfig userMsg userModel route siteStaticData pageStaticData
-> Model route
-> ( StaticResponses, StaticResponses.NextStep route )
-> ( Model route, Effect )
@ -769,7 +767,7 @@ nextStepToEffect contentCache config model ( updatedStaticResponsesModel, nextSt
sendSinglePageProgress :
ToJsSuccessPayload
-> ProgramConfig userMsg userModel route siteStaticData
-> ProgramConfig userMsg userModel route siteStaticData pageStaticData
-> ContentCache
-> Model route
-> ( PagePath, route )
@ -805,9 +803,27 @@ sendSinglePageProgress toJsPayload config _ model =
currentPage =
{ path = page, frontmatter = config.urlToRoute currentUrl }
pageStaticDataResult : Result BuildError pageStaticData
pageStaticDataResult =
StaticHttpRequest.resolve ApplicationType.Browser
(config.staticData (config.urlToRoute currentUrl))
(staticData |> Dict.map (\_ v -> Just v))
|> Result.mapError (StaticHttpRequest.toBuildError currentUrl.path)
pageStaticData : pageStaticData
pageStaticData =
case pageStaticDataResult of
Ok okPageStaticData ->
okPageStaticData
Err error ->
Debug.todo (BuildError.errorToString error)
pageModel : userModel
pageModel =
config.init Nothing
config.init
pageStaticData
Nothing
(Just
{ path =
{ path = currentPage.path

View File

@ -11,9 +11,10 @@ import Pages.StaticHttp as StaticHttp
import Url exposing (Url)
type alias ProgramConfig userMsg userModel route siteStaticData =
type alias ProgramConfig userMsg userModel route siteStaticData pageStaticData =
{ init :
Maybe Browser.Navigation.Key
pageStaticData
-> Maybe Browser.Navigation.Key
->
Maybe
{ path :
@ -24,8 +25,9 @@ type alias ProgramConfig userMsg userModel route siteStaticData =
, metadata : route
}
-> ( userModel, Cmd userMsg )
, update : Maybe Browser.Navigation.Key -> userMsg -> userModel -> ( userModel, Cmd userMsg )
, update : pageStaticData -> Maybe Browser.Navigation.Key -> userMsg -> userModel -> ( userModel, Cmd userMsg )
, subscriptions : route -> PagePath -> userModel -> Sub userMsg
, staticData : route -> StaticHttp.Request pageStaticData
, view :
{ path : PagePath
, frontmatter : route