prepare to make init work with route props

This commit is contained in:
Ryan Haskell-Glatz 2019-10-16 18:42:14 -05:00
parent 6ddddf3187
commit d72b74d20e
4 changed files with 74 additions and 1 deletions

View File

@ -12,6 +12,7 @@ view =
[ ( "Homepage", Route.Homepage )
, ( "Counter", Route.Counter )
, ( "Random", Route.Random )
, ( "My user", Route.Users_Slug "ryan" )
]
)

View File

@ -7,6 +7,7 @@ import Pages.Counter as Counter
import Pages.Homepage as Homepage
import Pages.NotFound as NotFound
import Pages.Random as Random
import Pages.Users.Slug as Users_Slug
type Model
@ -14,6 +15,7 @@ type Model
| CounterModel Counter.Model
| RandomModel Random.Model
| NotFoundModel NotFound.Model
| Users_SlugModel Users_Slug.Model
type Msg
@ -21,6 +23,7 @@ type Msg
| CounterMsg Counter.Msg
| RandomMsg Random.Msg
| NotFoundMsg NotFound.Msg
| Users_SlugMsg Users_Slug.Msg
homepage : Application.Recipe Homepage.Model Homepage.Msg Model Msg
@ -55,6 +58,14 @@ notFound =
}
users_slug : Application.Recipe Users_Slug.Model Users_Slug.Msg Model Msg
users_slug =
Users_Slug.page
{ toModel = Users_SlugModel
, toMsg = Users_SlugMsg
}
init : Route -> ( Model, Cmd Msg )
init route =
case route of
@ -70,6 +81,9 @@ init route =
Route.NotFound ->
notFound.init
Route.Users_Slug _ ->
users_slug.init
update : Msg -> Model -> ( Model, Cmd Msg )
update appMsg appModel =
@ -98,6 +112,12 @@ update appMsg appModel =
( NotFoundMsg _, _ ) ->
Application.keep appModel
( Users_SlugMsg msg, Users_SlugModel model ) ->
users_slug.update msg model
( Users_SlugMsg _, _ ) ->
Application.keep appModel
bundle : Model -> { view : Html Msg, subscriptions : Sub Msg }
bundle appModel =
@ -113,3 +133,6 @@ bundle appModel =
NotFoundModel model ->
notFound.bundle model
Users_SlugModel model ->
users_slug.bundle model

View File

@ -1,13 +1,14 @@
module Generated.Route exposing (Route(..), fromUrl, toPath)
import Url exposing (Url)
import Url.Parser as Parser
import Url.Parser as Parser exposing ((</>))
type Route
= Homepage
| Counter
| Random
| Users_Slug String
| NotFound
@ -18,6 +19,7 @@ fromUrl =
[ Parser.map Homepage Parser.top
, Parser.map Counter (Parser.s "counter")
, Parser.map Random (Parser.s "random")
, Parser.map Users_Slug (Parser.s "users" </> Parser.string)
]
)
>> Maybe.withDefault NotFound
@ -37,3 +39,6 @@ toPath route =
NotFound ->
"/not-found"
Users_Slug slug ->
"/users/" ++ slug

View File

@ -0,0 +1,44 @@
module Pages.Users.Slug exposing (Model, Msg, page)
import Application
import Html exposing (..)
type Model
= Model
type Msg
= Msg
page : Application.Page Model Msg a b
page =
Application.element
{ init = init
, update = update
, view = view
, subscriptions = subscriptions
}
init : ( Model, Cmd Msg )
init =
( Model, Cmd.none )
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Msg ->
( model, Cmd.none )
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
view : Model -> Html Msg
view model =
h1 [] [ text "New Element" ]