pick a lane, you swervin

This commit is contained in:
Ryan Haskell-Glatz 2019-10-16 21:24:54 -05:00
parent c74cda90fc
commit 3796bbff10
11 changed files with 211 additions and 149 deletions

View File

@ -9,10 +9,11 @@ view : Html msg
view = view =
header [ Attr.class "navbar" ] header [ Attr.class "navbar" ]
(List.map viewLink (List.map viewLink
[ ( "Homepage", Route.Homepage ) [ ( "Homepage", Route.Homepage () )
, ( "Counter", Route.Counter ) , ( "Counter", Route.Counter () )
, ( "Random", Route.Random ) , ( "Random", Route.Random () )
, ( "My user", Route.Users_Slug "ryan" ) , ( "My user", Route.Users_Slug "ryan" )
, ( "My user's post", Route.Users_Slug_Posts_Slug { user = "ryan", post = 123 } )
] ]
) )

View File

@ -7,7 +7,8 @@ import Pages.Counter as Counter
import Pages.Homepage as Homepage import Pages.Homepage as Homepage
import Pages.NotFound as NotFound import Pages.NotFound as NotFound
import Pages.Random as Random import Pages.Random as Random
import Pages.Users.Slug_ as Users_Slug import Pages.Users.Slug as Users_Slug
import Pages.Users.Slug.Posts.Slug as Users_Slug_Posts_Slug
type Model type Model
@ -16,6 +17,7 @@ type Model
| RandomModel Random.Model | RandomModel Random.Model
| NotFoundModel NotFound.Model | NotFoundModel NotFound.Model
| Users_SlugModel Users_Slug.Model | Users_SlugModel Users_Slug.Model
| Users_Slug_Posts_SlugModel Users_Slug_Posts_Slug.Model
type Msg type Msg
@ -24,9 +26,10 @@ type Msg
| RandomMsg Random.Msg | RandomMsg Random.Msg
| NotFoundMsg NotFound.Msg | NotFoundMsg NotFound.Msg
| Users_SlugMsg Users_Slug.Msg | Users_SlugMsg Users_Slug.Msg
| Users_Slug_Posts_SlugMsg Users_Slug_Posts_Slug.Msg
homepage : Application.Recipe Homepage.Model Homepage.Msg Model Msg homepage : Application.Recipe Homepage.Params Homepage.Model Homepage.Msg Model Msg
homepage = homepage =
Homepage.page Homepage.page
{ toModel = HomepageModel { toModel = HomepageModel
@ -34,7 +37,7 @@ homepage =
} }
counter : Application.Recipe Counter.Model Counter.Msg Model Msg counter : Application.Recipe Counter.Params Counter.Model Counter.Msg Model Msg
counter = counter =
Counter.page Counter.page
{ toModel = CounterModel { toModel = CounterModel
@ -42,7 +45,7 @@ counter =
} }
random : Application.Recipe Random.Model Random.Msg Model Msg random : Application.Recipe Random.Params Random.Model Random.Msg Model Msg
random = random =
Random.page Random.page
{ toModel = RandomModel { toModel = RandomModel
@ -50,7 +53,7 @@ random =
} }
notFound : Application.Recipe NotFound.Model NotFound.Msg Model Msg notFound : Application.Recipe NotFound.Params NotFound.Model NotFound.Msg Model Msg
notFound = notFound =
NotFound.page NotFound.page
{ toModel = NotFoundModel { toModel = NotFoundModel
@ -58,7 +61,7 @@ notFound =
} }
users_slug : Application.RecipeWithParams Users_Slug.Model Users_Slug.Msg Model Msg String users_slug : Application.Recipe Users_Slug.Params Users_Slug.Model Users_Slug.Msg Model Msg
users_slug = users_slug =
Users_Slug.page Users_Slug.page
{ toModel = Users_SlugModel { toModel = Users_SlugModel
@ -66,23 +69,34 @@ users_slug =
} }
users_slug_posts_slug : Application.Recipe Users_Slug_Posts_Slug.Params Users_Slug_Posts_Slug.Model Users_Slug_Posts_Slug.Msg Model Msg
users_slug_posts_slug =
Users_Slug_Posts_Slug.page
{ toModel = Users_Slug_Posts_SlugModel
, toMsg = Users_Slug_Posts_SlugMsg
}
init : Route -> ( Model, Cmd Msg ) init : Route -> ( Model, Cmd Msg )
init route = init route =
case route of case route of
Route.Homepage -> Route.Homepage params ->
homepage.init homepage.init params
Route.Counter -> Route.Counter params ->
counter.init counter.init params
Route.Random -> Route.Random params ->
random.init random.init params
Route.NotFound -> Route.NotFound params ->
notFound.init notFound.init params
Route.Users_Slug slug -> Route.Users_Slug params ->
users_slug.init slug users_slug.init params
Route.Users_Slug_Posts_Slug params ->
users_slug_posts_slug.init params
update : Msg -> Model -> ( Model, Cmd Msg ) update : Msg -> Model -> ( Model, Cmd Msg )
@ -118,6 +132,12 @@ update appMsg appModel =
( Users_SlugMsg _, _ ) -> ( Users_SlugMsg _, _ ) ->
Application.keep appModel Application.keep appModel
( Users_Slug_Posts_SlugMsg msg, Users_Slug_Posts_SlugModel model ) ->
users_slug_posts_slug.update msg model
( Users_Slug_Posts_SlugMsg _, _ ) ->
Application.keep appModel
bundle : Model -> { view : Html Msg, subscriptions : Sub Msg } bundle : Model -> { view : Html Msg, subscriptions : Sub Msg }
bundle appModel = bundle appModel =
@ -136,3 +156,6 @@ bundle appModel =
Users_SlugModel model -> Users_SlugModel model ->
users_slug.bundle model users_slug.bundle model
Users_Slug_Posts_SlugModel model ->
users_slug_posts_slug.bundle model

View File

@ -1,44 +1,64 @@
module Generated.Route exposing (Route(..), fromUrl, toPath) module Generated.Route exposing (Route(..), fromUrl, toPath)
import Url exposing (Url) import Url exposing (Url)
import Url.Parser as Parser exposing ((</>)) import Url.Parser as Parser exposing ((</>), Parser)
type Route type Route
= Homepage = Homepage ()
| Counter | Counter ()
| Random | Random ()
| Users_Slug String | Users_Slug String
| NotFound | Users_Slug_Posts_Slug UserPostInfo
| NotFound ()
type alias UserPostInfo =
{ user : String
, post : Int
}
fromUrl : Url -> Route fromUrl : Url -> Route
fromUrl = fromUrl =
Parser.parse Parser.parse
(Parser.oneOf (Parser.oneOf routes)
[ Parser.map Homepage Parser.top >> Maybe.withDefault (NotFound ())
, Parser.map Counter (Parser.s "counter")
, Parser.map Random (Parser.s "random")
, Parser.map Users_Slug (Parser.s "users" </> Parser.string) routes : List (Parser (Route -> Route) Route)
routes =
[ Parser.top
|> Parser.map (Homepage ())
, Parser.s "counter"
|> Parser.map (Counter ())
, Parser.s "random"
|> Parser.map (Random ())
, (Parser.s "users" </> Parser.string)
|> Parser.map Users_Slug
, (Parser.s "users" </> Parser.string </> Parser.s "posts" </> Parser.int)
|> Parser.map UserPostInfo
|> Parser.map Users_Slug_Posts_Slug
] ]
)
>> Maybe.withDefault NotFound
toPath : Route -> String toPath : Route -> String
toPath route = toPath route =
case route of case route of
Homepage -> Homepage _ ->
"/" "/"
Counter -> Counter _ ->
"/counter" "/counter"
Random -> Random _ ->
"/random" "/random"
NotFound -> NotFound _ ->
"/not-found" "/not-found"
Users_Slug slug -> Users_Slug slug ->
"/users/" ++ slug "/users/" ++ slug
Users_Slug_Posts_Slug { user, post } ->
"/users/" ++ user ++ "/posts/" ++ String.fromInt post

View File

@ -1,4 +1,9 @@
module Pages.Counter exposing (Model, Msg, page) module Pages.Counter exposing
( Model
, Msg
, Params
, page
)
import Application import Application
import Html exposing (..) import Html exposing (..)
@ -15,10 +20,14 @@ type Msg
| Decrement | Decrement
page : Application.Page Model Msg model msg type alias Params =
()
page : Application.Page Params Model Msg model msg
page = page =
Application.sandbox Application.sandbox
{ init = init { init = always init
, update = update , update = update
, view = view , view = view
} }

View File

@ -1,6 +1,7 @@
module Pages.Homepage exposing module Pages.Homepage exposing
( Model ( Model
, Msg , Msg
, Params
, page , page
) )
@ -16,7 +17,11 @@ type alias Msg =
Never Never
page : Application.Page Model Msg model msg type alias Params =
()
page : Application.Page Params Model Msg model msg
page = page =
Application.static Application.static
{ view = view { view = view

View File

@ -1,4 +1,9 @@
module Pages.NotFound exposing (Model, Msg, page) module Pages.NotFound exposing
( Model
, Msg
, Params
, page
)
import Application import Application
import Html exposing (..) import Html exposing (..)
@ -12,7 +17,11 @@ type alias Msg =
Never Never
page : Application.Page Model Msg model msg type alias Params =
()
page : Application.Page Params Model Msg model msg
page = page =
Application.static Application.static
{ view = view { view = view

View File

@ -1,4 +1,9 @@
module Pages.Random exposing (Model, Msg, page) module Pages.Random exposing
( Model
, Msg
, Params
, page
)
import Application as Application import Application as Application
import Html exposing (..) import Html exposing (..)
@ -18,10 +23,14 @@ type Msg
| CatResponded (Result Http.Error String) | CatResponded (Result Http.Error String)
page : Application.Page Model Msg model msg type alias Params =
()
page : Application.Page Params Model Msg model msg
page = page =
Application.element Application.element
{ init = init { init = always init
, update = update , update = update
, view = view , view = view
, subscriptions = subscriptions , subscriptions = subscriptions

View File

@ -1,4 +1,4 @@
module Pages.Users.Slug_ exposing (Model, Msg, page) module Pages.Users.Slug exposing (Model, Msg, Params, page)
import Application import Application
import Html exposing (..) import Html exposing (..)
@ -13,9 +13,13 @@ type Msg
= Msg = Msg
page : Application.PageWithParams Model Msg a b String type alias Params =
String
page : Application.Page Params Model Msg a b
page = page =
Application.elementWithParams Application.element
{ init = init { init = init
, update = update , update = update
, view = view , view = view
@ -23,7 +27,7 @@ page =
} }
init : String -> ( Model, Cmd Msg ) init : Params -> ( Model, Cmd Msg )
init slug = init slug =
( Model slug, Cmd.none ) ( Model slug, Cmd.none )

View File

@ -0,0 +1,52 @@
module Pages.Users.Slug.Posts.Slug exposing (Model, Msg, Params, page)
import Application
import Html exposing (..)
type alias Model =
{ user : String
, post : Int
}
type Msg
= Msg
type alias Params =
{ user : String
, post : Int
}
page : Application.Page Params Model Msg a b
page =
Application.element
{ init = init
, update = update
, view = view
, subscriptions = subscriptions
}
init : Params -> ( Model, Cmd Msg )
init params =
( params, 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 ("Post " ++ String.fromInt model.post ++ " for user: " ++ model.user) ]

View File

@ -1,12 +1,10 @@
module Application exposing module Application exposing
( Application, create ( Application, create
, Page, Recipe , Page, Recipe
, PageWithParams, RecipeWithParams
, Bundle, keep , Bundle, keep
, Static, static , Static, static
, Sandbox, sandbox , Sandbox, sandbox
, Element, element , Element, element
, ElementWithParams, elementWithParams
) )
{-| {-|
@ -14,7 +12,6 @@ module Application exposing
@docs Application, create @docs Application, create
@docs Page, Recipe @docs Page, Recipe
@docs PageWithParams, RecipeWithParams
@docs Bundle, keep @docs Bundle, keep
@docs Static, static @docs Static, static
@ -23,6 +20,8 @@ module Application exposing
@docs Element, element @docs Element, element
@docs PageWithParams, RecipeWithParams
@docs ElementWithParams, elementWithParams @docs ElementWithParams, elementWithParams
-} -}
@ -195,20 +194,12 @@ view config model =
-- PAGE API -- PAGE API
type alias Page pageModel pageMsg model msg = type alias Page params pageModel pageMsg model msg =
Page.Page pageModel pageMsg model msg Page.Page params pageModel pageMsg model msg
type alias Recipe pageModel pageMsg model msg = type alias Recipe params pageModel pageMsg model msg =
Page.Recipe pageModel pageMsg model msg Page.Recipe params pageModel pageMsg model msg
type alias PageWithParams pageModel pageMsg model msg arg =
Page.PageWithParams pageModel pageMsg model msg arg
type alias RecipeWithParams pageModel pageMsg model msg arg =
Page.RecipeWithParams pageModel pageMsg model msg arg
type alias Bundle msg = type alias Bundle msg =
@ -226,39 +217,28 @@ type alias Static =
static : static :
Static Static
-> Page () Never model msg -> Page params () Never model msg
static = static =
Page.static Page.static
type alias Sandbox pageModel pageMsg = type alias Sandbox pageModel pageMsg params =
Page.Sandbox pageModel pageMsg Page.Sandbox pageModel pageMsg params
sandbox : sandbox :
Sandbox pageModel pageMsg Sandbox pageModel pageMsg params
-> Page pageModel pageMsg model msg -> Page params pageModel pageMsg model msg
sandbox = sandbox =
Page.sandbox Page.sandbox
type alias Element pageModel pageMsg = type alias Element pageModel pageMsg params =
Page.Element pageModel pageMsg Page.Element pageModel pageMsg params
element : element :
Element pageModel pageMsg Element pageModel pageMsg params
-> Page pageModel pageMsg model msg -> Page params pageModel pageMsg model msg
element = element =
Page.element Page.element
type alias ElementWithParams pageModel pageMsg arg =
Page.ElementWithParams pageModel pageMsg arg
elementWithParams :
ElementWithParams pageModel pageMsg arg
-> PageWithParams pageModel pageMsg model msg arg
elementWithParams =
Page.elementWithParams

View File

@ -1,19 +1,15 @@
module Internals.Page exposing module Internals.Page exposing
( Page, Recipe ( Page, Recipe
, PageWithParams, RecipeWithParams
, Bundle , Bundle
, Static, static , Static, static
, Sandbox, sandbox , Sandbox, sandbox
, Element, element , Element, element
, ElementWithParams, elementWithParams
) )
{-| {-|
@docs Page, Recipe @docs Page, Recipe
@docs PageWithParams, RecipeWithParams
@docs Bundle @docs Bundle
@docs Static, static @docs Static, static
@ -22,22 +18,20 @@ module Internals.Page exposing
@docs Element, element @docs Element, element
@docs ElementWithParams, elementWithParams
-} -}
import Html exposing (Html) import Html exposing (Html)
type alias Page pageModel pageMsg model msg = type alias Page params pageModel pageMsg model msg =
{ toModel : pageModel -> model { toModel : pageModel -> model
, toMsg : pageMsg -> msg , toMsg : pageMsg -> msg
} }
-> Recipe pageModel pageMsg model msg -> Recipe params pageModel pageMsg model msg
type alias Recipe pageModel pageMsg model msg = type alias Recipe params pageModel pageMsg model msg =
{ init : ( model, Cmd msg ) { init : params -> ( model, Cmd msg )
, update : pageMsg -> pageModel -> ( model, Cmd msg ) , update : pageMsg -> pageModel -> ( model, Cmd msg )
, bundle : pageModel -> Bundle msg , bundle : pageModel -> Bundle msg
} }
@ -60,9 +54,9 @@ type alias Static =
static : static :
Static Static
-> Page () Never model msg -> Page params () Never model msg
static page { toModel, toMsg } = static page { toModel, toMsg } =
{ init = ( toModel (), Cmd.none ) { init = \_ -> ( toModel (), Cmd.none )
, update = \_ model -> ( toModel model, Cmd.none ) , update = \_ model -> ( toModel model, Cmd.none )
, bundle = , bundle =
\_ -> \_ ->
@ -76,18 +70,18 @@ static page { toModel, toMsg } =
-- SANDBOX -- SANDBOX
type alias Sandbox pageModel pageMsg = type alias Sandbox pageModel pageMsg params =
{ init : pageModel { init : params -> pageModel
, update : pageMsg -> pageModel -> pageModel , update : pageMsg -> pageModel -> pageModel
, view : pageModel -> Html pageMsg , view : pageModel -> Html pageMsg
} }
sandbox : sandbox :
Sandbox pageModel pageMsg Sandbox pageModel pageMsg params
-> Page pageModel pageMsg model msg -> Page params pageModel pageMsg model msg
sandbox page { toModel, toMsg } = sandbox page { toModel, toMsg } =
{ init = ( toModel page.init, Cmd.none ) { init = \params -> ( toModel (page.init params), Cmd.none )
, update = , update =
\msg model -> \msg model ->
( page.update msg model |> toModel ( page.update msg model |> toModel
@ -105,8 +99,8 @@ sandbox page { toModel, toMsg } =
-- ELEMENT -- ELEMENT
type alias Element pageModel pageMsg = type alias Element pageModel pageMsg params =
{ init : ( pageModel, Cmd pageMsg ) { init : params -> ( pageModel, Cmd pageMsg )
, update : pageMsg -> pageModel -> ( pageModel, Cmd pageMsg ) , update : pageMsg -> pageModel -> ( pageModel, Cmd pageMsg )
, view : pageModel -> Html pageMsg , view : pageModel -> Html pageMsg
, subscriptions : pageModel -> Sub pageMsg , subscriptions : pageModel -> Sub pageMsg
@ -114,53 +108,9 @@ type alias Element pageModel pageMsg =
element : element :
Element pageModel pageMsg Element pageModel pageMsg params
-> Page pageModel pageMsg model msg -> Page params pageModel pageMsg model msg
element page { toModel, toMsg } = element page { toModel, toMsg } =
{ init =
page.init |> Tuple.mapBoth toModel (Cmd.map toMsg)
, update =
\msg model ->
page.update msg model
|> Tuple.mapBoth toModel (Cmd.map toMsg)
, bundle =
\model ->
{ view = page.view model |> Html.map toMsg
, subscriptions = Sub.none
}
}
-- ELEMENT WITH PARAMS
type alias ElementWithParams pageModel pageMsg arg =
{ init : arg -> ( pageModel, Cmd pageMsg )
, update : pageMsg -> pageModel -> ( pageModel, Cmd pageMsg )
, view : pageModel -> Html pageMsg
, subscriptions : pageModel -> Sub pageMsg
}
type alias RecipeWithParams pageModel pageMsg model msg arg =
{ init : arg -> ( model, Cmd msg )
, update : pageMsg -> pageModel -> ( model, Cmd msg )
, bundle : pageModel -> Bundle msg
}
type alias PageWithParams pageModel pageMsg model msg arg =
{ toModel : pageModel -> model
, toMsg : pageMsg -> msg
}
-> RecipeWithParams pageModel pageMsg model msg arg
elementWithParams :
ElementWithParams pageModel pageMsg arg
-> PageWithParams pageModel pageMsg model msg arg
elementWithParams page { toModel, toMsg } =
{ init = { init =
page.init >> Tuple.mapBoth toModel (Cmd.map toMsg) page.init >> Tuple.mapBoth toModel (Cmd.map toMsg)
, update = , update =