Wire in static payload to init and update.

This commit is contained in:
Dillon Kearns 2021-04-22 11:31:06 -07:00
parent 31a659e2d4
commit d08d0e7ff0
5 changed files with 46 additions and 36 deletions

View File

@ -19,7 +19,7 @@ type Msg
= Msg
template : TemplateWithState {} StaticData Model Msg
template : TemplateWithState RouteParams StaticData Model Msg
template =
Template.withStaticData
{ head = head
@ -46,7 +46,9 @@ type alias StaticData =
List ( PagePath, Article.ArticleMetadata )
init : {} -> ( Model, Cmd Msg )
init :
StaticPayload StaticData RouteParams
-> ( Model, Cmd Msg )
init _ =
( Model, Cmd.none )
@ -57,12 +59,11 @@ type alias RouteParams =
update :
DynamicContext Shared.Model
-> StaticData
-> RouteParams
-> StaticPayload StaticData RouteParams
-> Msg
-> Model
-> ( Model, Cmd Msg )
update dynamic sharedModel routeParams msg model =
update dynamic static msg model =
( model, Cmd.none )

View File

@ -48,7 +48,9 @@ template =
}
init : {} -> ( Model, Cmd Msg )
init :
StaticPayload StaticData RouteParams
-> ( Model, Cmd Msg )
init _ =
( {}, Cmd.none )
@ -59,7 +61,7 @@ type alias RouteParams =
update :
DynamicContext Shared.Model
-> RouteParams
-> StaticPayload StaticData RouteParams
-> Msg
-> Model
-> ( Model, Cmd Msg, Maybe Shared.SharedMsg )

View File

@ -49,19 +49,19 @@ template =
}
|> Template.buildWithLocalState
{ view = view
, init = \routeParams -> ( (), Cmd.none )
, init = \staticPayload -> ( (), Cmd.none )
, update =
\sharedModel pageStaticData routeParams msg model ->
\sharedModel static msg model ->
case msg of
OnKeyPress (Just direction) ->
let
currentSlide =
String.toInt routeParams.number |> Maybe.withDefault 0
String.toInt static.routeParams.number |> Maybe.withDefault 0
nextSlide =
clamp
1
pageStaticData.totalCount
static.static.totalCount
(case direction of
Right ->
currentSlide + 1

View File

@ -65,8 +65,8 @@ type alias TemplateWithState routeParams templateStaticData templateModel templa
, head :
StaticPayload templateStaticData routeParams
-> List Head.Tag
, init : routeParams -> ( templateModel, Cmd templateMsg )
, update : templateStaticData -> Maybe Browser.Navigation.Key -> routeParams -> templateMsg -> templateModel -> Shared.Model -> ( templateModel, Cmd templateMsg, Maybe Shared.SharedMsg )
, init : StaticPayload templateStaticData routeParams -> ( templateModel, Cmd templateMsg )
, update : StaticPayload templateStaticData routeParams -> Maybe Browser.Navigation.Key -> templateMsg -> templateModel -> Shared.Model -> ( templateModel, Cmd templateMsg, Maybe Shared.SharedMsg )
, subscriptions : routeParams -> PagePath -> templateModel -> Shared.Model -> Sub templateMsg
}
@ -112,7 +112,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,8 +124,8 @@ buildWithLocalState :
-> Shared.Model
-> StaticPayload templateStaticData routeParams
-> Document templateMsg
, init : routeParams -> ( templateModel, Cmd templateMsg )
, update : DynamicContext Shared.Model -> templateStaticData -> routeParams -> templateMsg -> templateModel -> ( templateModel, Cmd templateMsg )
, init : StaticPayload templateStaticData routeParams -> ( templateModel, Cmd templateMsg )
, update : DynamicContext Shared.Model -> StaticPayload templateStaticData routeParams -> templateMsg -> templateModel -> ( templateModel, Cmd templateMsg )
, subscriptions : routeParams -> PagePath -> templateModel -> Sub templateMsg
}
-> Builder routeParams templateStaticData
@ -141,15 +141,14 @@ buildWithLocalState config builderState =
, staticRoutes = record.staticRoutes
, init = config.init
, update =
\staticData navigationKey routeParams msg templateModel sharedModel ->
\staticPayload navigationKey msg templateModel sharedModel ->
let
( updatedModel, cmd ) =
config.update
{ navigationKey = navigationKey
, sharedModel = sharedModel
}
staticData
routeParams
staticPayload
msg
templateModel
in
@ -173,8 +172,8 @@ buildWithSharedState :
-> Shared.Model
-> StaticPayload templateStaticData routeParams
-> Document templateMsg
, init : routeParams -> ( templateModel, Cmd templateMsg )
, update : DynamicContext Shared.Model -> routeParams -> templateMsg -> templateModel -> ( templateModel, Cmd templateMsg, Maybe Shared.SharedMsg )
, init : StaticPayload templateStaticData routeParams -> ( templateModel, Cmd templateMsg )
, update : DynamicContext Shared.Model -> StaticPayload templateStaticData routeParams -> templateMsg -> templateModel -> ( templateModel, Cmd templateMsg, Maybe Shared.SharedMsg )
, subscriptions : routeParams -> PagePath -> templateModel -> Shared.Model -> Sub templateMsg
}
-> Builder routeParams templateStaticData
@ -188,12 +187,12 @@ buildWithSharedState config builderState =
, staticRoutes = record.staticRoutes
, init = config.init
, update =
\pageStaticData navigationKey routeParams msg templateModel sharedModel ->
\staticPayload navigationKey msg templateModel sharedModel ->
config.update
{ navigationKey = navigationKey
, sharedModel = sharedModel
}
routeParams
staticPayload
msg
templateModel
, subscriptions = config.subscriptions

View File

@ -196,23 +196,28 @@ init currentGlobalModel sharedStaticData pageStaticData navigationKey maybePageP
currentGlobalModel |> Maybe.map (\\m -> ( m, Cmd.none )) |> Maybe.withDefault (Shared.template.init navigationKey maybePagePath)
( templateModel, templateCmd ) =
case maybePagePath |> Maybe.andThen .metadata of
Nothing ->
( NotFound, Cmd.none )
case ( ( Maybe.map2 Tuple.pair (maybePagePath |> Maybe.andThen .metadata) (maybePagePath |> Maybe.map .path) ), pageStaticData ) of
${templates
.map(
(name) => `Just (Route.${routeHelpers.routeVariant(
(name) => `( Just ( (Route.${routeHelpers.routeVariant(
name
)} routeParams) ->
Template.${moduleName(name)}.template.init routeParams
)} routeParams), justPath ), Data${pathNormalizedName(
name
)} thisPageData ) ->
Template.${moduleName(name)}.template.init
{ static = thisPageData
, sharedStatic = sharedStaticData
, routeParams = routeParams
, path = justPath.path
}
|> Tuple.mapBoth Model${pathNormalizedName(
name
)} (Cmd.map Msg${pathNormalizedName(name)})
`
)
.join("\n ")}
_ ->
( NotFound, Cmd.none )
in
( { global = sharedModel
, page = templateModel
@ -280,18 +285,21 @@ update sharedStaticData pageStaticData navigationKey msg model =
Msg${pathNormalizedName(name)} msg_ ->
let
( updatedPageModel, pageCmd, ( newGlobalModel, newGlobalCmd ) ) =
case ( model.page, pageStaticData, model.current |> Maybe.andThen .metadata ) of
case ( model.page, pageStaticData, Maybe.map2 Tuple.pair (model.current |> Maybe.andThen .metadata) (model.current |> Maybe.map .path) ) of
( Model${pathNormalizedName(
name
)} pageModel, Data${pathNormalizedName(
name
)} thisPageData, Just (Route.${routeHelpers.routeVariant(
)} thisPageData, Just ( (Route.${routeHelpers.routeVariant(
name
)} routeParams) ) ->
)} routeParams), justPage ) ) ->
Template.${moduleName(name)}.template.update
thisPageData
{ static = thisPageData
, sharedStatic = sharedStaticData
, routeParams = routeParams
, path = justPage.path
}
navigationKey
routeParams
msg_
pageModel
model.global