From 07baa8d18dfc21c73d39dedab9b99e6e6228eb91 Mon Sep 17 00:00:00 2001 From: Ryan Haskell-Glatz Date: Thu, 7 Nov 2019 13:00:23 -0800 Subject: [PATCH] =?UTF-8?q?recursive=20dynamic=20routes=20work,=20with=20a?= =?UTF-8?q?=20function=20that=20doesnt=20exist=20=F0=9F=98=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- elm-stuff/.elm-spa/Generated/Docs/Pages.elm | 12 +-- .../Generated/Docs/{Flags.elm => Params.elm} | 7 +- elm-stuff/.elm-spa/Generated/Docs/Route.elm | 37 +++++++ elm-stuff/.elm-spa/Generated/Docs/Routes.elm | 30 ------ .../Generated/Guide/Dynamic/Flags.elm | 5 - .../Generated/Guide/Dynamic/Pages.elm | 32 ++++-- .../Generated/Guide/Dynamic/Params.elm | 14 +++ .../Generated/Guide/Dynamic/Route.elm | 37 +++++++ .../Generated/Guide/Dynamic/Routes.elm | 25 ----- elm-stuff/.elm-spa/Generated/Guide/Pages.elm | 22 ++-- .../Generated/Guide/{Flags.elm => Params.elm} | 8 +- elm-stuff/.elm-spa/Generated/Guide/Route.elm | 50 +++++++++ elm-stuff/.elm-spa/Generated/Guide/Routes.elm | 41 ------- elm-stuff/.elm-spa/Generated/Pages.elm | 36 +++---- .../Generated/{Flags.elm => Params.elm} | 12 +-- elm-stuff/.elm-spa/Generated/Route.elm | 63 +++++++++++ elm-stuff/.elm-spa/Generated/Routes.elm | 57 ---------- elm-stuff/.elm-spa/README.md | 4 +- example/Global.elm | 5 +- example/Layouts/Guide.elm | 10 +- example/Main.elm | 8 +- example/Pages/Docs.elm | 4 +- example/Pages/Docs/Dynamic.elm | 10 +- example/Pages/Docs/Static.elm | 4 +- example/Pages/Guide.elm | 4 +- example/Pages/Guide/Dynamic/Intro.elm | 27 +++-- example/Pages/Guide/Dynamic/Other.elm | 53 ++++++++++ example/Pages/Guide/Elm.elm | 4 +- example/Pages/Guide/ElmSpa.elm | 4 +- example/Pages/Guide/Programming.elm | 4 +- example/Pages/NotFound.elm | 6 +- example/Pages/SignIn.elm | 6 +- example/Pages/Top.elm | 8 +- src/App.elm | 7 +- src/App/Route.elm | 100 ------------------ src/App/Router.elm | 83 +++++++++++++++ 36 files changed, 480 insertions(+), 359 deletions(-) rename elm-stuff/.elm-spa/Generated/Docs/{Flags.elm => Params.elm} (51%) create mode 100644 elm-stuff/.elm-spa/Generated/Docs/Route.elm delete mode 100644 elm-stuff/.elm-spa/Generated/Docs/Routes.elm delete mode 100644 elm-stuff/.elm-spa/Generated/Guide/Dynamic/Flags.elm create mode 100644 elm-stuff/.elm-spa/Generated/Guide/Dynamic/Params.elm create mode 100644 elm-stuff/.elm-spa/Generated/Guide/Dynamic/Route.elm delete mode 100644 elm-stuff/.elm-spa/Generated/Guide/Dynamic/Routes.elm rename elm-stuff/.elm-spa/Generated/Guide/{Flags.elm => Params.elm} (65%) create mode 100644 elm-stuff/.elm-spa/Generated/Guide/Route.elm delete mode 100644 elm-stuff/.elm-spa/Generated/Guide/Routes.elm rename elm-stuff/.elm-spa/Generated/{Flags.elm => Params.elm} (71%) create mode 100644 elm-stuff/.elm-spa/Generated/Route.elm delete mode 100644 elm-stuff/.elm-spa/Generated/Routes.elm create mode 100644 example/Pages/Guide/Dynamic/Other.elm delete mode 100644 src/App/Route.elm create mode 100644 src/App/Router.elm diff --git a/elm-stuff/.elm-spa/Generated/Docs/Pages.elm b/elm-stuff/.elm-spa/Generated/Docs/Pages.elm index d81cc52..b3b8355 100644 --- a/elm-stuff/.elm-spa/Generated/Docs/Pages.elm +++ b/elm-stuff/.elm-spa/Generated/Docs/Pages.elm @@ -5,8 +5,8 @@ module Generated.Docs.Pages exposing ) import App.Page -import Generated.Docs.Flags as Flags -import Generated.Docs.Routes as Routes exposing (Route(..)) +import Generated.Docs.Params as Params +import Generated.Docs.Route as Route exposing (Route(..)) import Layouts.Docs as Layout import Pages.Docs.Dynamic import Pages.Docs.Static @@ -44,8 +44,8 @@ type alias Recipe flags model msg appMsg = type alias Recipes msg = - { dynamic : Recipe Flags.Dynamic Pages.Docs.Dynamic.Model Pages.Docs.Dynamic.Msg msg - , static : Recipe Flags.Static Pages.Docs.Static.Model Pages.Docs.Static.Msg msg + { dynamic : Recipe Params.Dynamic Pages.Docs.Dynamic.Model Pages.Docs.Dynamic.Msg msg + , static : Recipe Params.Static Pages.Docs.Static.Model Pages.Docs.Static.Msg msg } @@ -73,10 +73,10 @@ recipes = init : Route -> Page.Init Model Msg init route = case route of - Routes.Dynamic flags -> + Route.Dynamic _ flags -> recipes.dynamic.init flags - Routes.Static flags -> + Route.Static flags -> recipes.static.init flags diff --git a/elm-stuff/.elm-spa/Generated/Docs/Flags.elm b/elm-stuff/.elm-spa/Generated/Docs/Params.elm similarity index 51% rename from elm-stuff/.elm-spa/Generated/Docs/Flags.elm rename to elm-stuff/.elm-spa/Generated/Docs/Params.elm index e1ff002..bb3a7a2 100644 --- a/elm-stuff/.elm-spa/Generated/Docs/Flags.elm +++ b/elm-stuff/.elm-spa/Generated/Docs/Params.elm @@ -1,12 +1,13 @@ -module Generated.Docs.Flags exposing +module Generated.Docs.Params exposing ( Dynamic , Static ) type alias Static = - () + {} type alias Dynamic = - String + { param1 : String + } diff --git a/elm-stuff/.elm-spa/Generated/Docs/Route.elm b/elm-stuff/.elm-spa/Generated/Docs/Route.elm new file mode 100644 index 0000000..4e7b02a --- /dev/null +++ b/elm-stuff/.elm-spa/Generated/Docs/Route.elm @@ -0,0 +1,37 @@ +module Generated.Docs.Route exposing + ( Route(..) + , routes + , toPath + ) + +import App.Router +import Generated.Docs.Params as Params +import Url.Parser as Parser exposing ((), Parser) + + +type Route + = Static Params.Static + | Dynamic String Params.Dynamic + + +toPath : Route -> String +toPath route = + case route of + Static _ -> + "/static" + + Dynamic value _ -> + "/" ++ value + + +routes : + {} + -> List (Parser (Route -> a) a) +routes params = + let + router = + App.Router.create params + in + [ router.path Static "static" + , router.dynamic Dynamic (\param1 -> { param1 = param1 }) + ] diff --git a/elm-stuff/.elm-spa/Generated/Docs/Routes.elm b/elm-stuff/.elm-spa/Generated/Docs/Routes.elm deleted file mode 100644 index c1b9dd5..0000000 --- a/elm-stuff/.elm-spa/Generated/Docs/Routes.elm +++ /dev/null @@ -1,30 +0,0 @@ -module Generated.Docs.Routes exposing - ( Route(..) - , routes - , toPath - ) - -import App.Route as Route -import Generated.Docs.Flags as Flags - - -type Route - = Static Flags.Static - | Dynamic Flags.Dynamic - - -routes : List (Route.Route Route a) -routes = - [ Route.path "static" Static - , Route.dynamic Dynamic - ] - - -toPath : Route -> String -toPath route = - case route of - Static _ -> - "/static" - - Dynamic string -> - "/" ++ string diff --git a/elm-stuff/.elm-spa/Generated/Guide/Dynamic/Flags.elm b/elm-stuff/.elm-spa/Generated/Guide/Dynamic/Flags.elm deleted file mode 100644 index fd106a7..0000000 --- a/elm-stuff/.elm-spa/Generated/Guide/Dynamic/Flags.elm +++ /dev/null @@ -1,5 +0,0 @@ -module Generated.Guide.Dynamic.Flags exposing (Intro) - - -type alias Intro = - () diff --git a/elm-stuff/.elm-spa/Generated/Guide/Dynamic/Pages.elm b/elm-stuff/.elm-spa/Generated/Guide/Dynamic/Pages.elm index 37b5798..ebe54e5 100644 --- a/elm-stuff/.elm-spa/Generated/Guide/Dynamic/Pages.elm +++ b/elm-stuff/.elm-spa/Generated/Guide/Dynamic/Pages.elm @@ -5,19 +5,22 @@ module Generated.Guide.Dynamic.Pages exposing ) import App.Page -import Generated.Guide.Dynamic.Flags as Flags -import Generated.Guide.Dynamic.Routes as Routes exposing (Route(..)) +import Generated.Guide.Dynamic.Params as Params +import Generated.Guide.Dynamic.Route as Route exposing (Route) import Layouts.Guide.Dynamic as Layout import Pages.Guide.Dynamic.Intro +import Pages.Guide.Dynamic.Other import Utils.Page as Page exposing (Page) type Model = IntroModel Pages.Guide.Dynamic.Intro.Model + | OtherModel Pages.Guide.Dynamic.Other.Model type Msg = IntroMsg Pages.Guide.Dynamic.Intro.Msg + | OtherMsg Pages.Guide.Dynamic.Other.Msg page : Page Route Model Msg layoutModel layoutMsg appMsg @@ -41,7 +44,8 @@ type alias Recipe flags model msg appMsg = type alias Recipes msg = - { intro : Recipe Flags.Intro Pages.Guide.Dynamic.Intro.Model Pages.Guide.Dynamic.Intro.Msg msg + { intro : Recipe Params.Intro Pages.Guide.Dynamic.Intro.Model Pages.Guide.Dynamic.Intro.Msg msg + , other : Recipe Params.Other Pages.Guide.Dynamic.Other.Model Pages.Guide.Dynamic.Other.Msg msg } @@ -53,6 +57,12 @@ recipes = , toModel = IntroModel , toMsg = IntroMsg } + , other = + Page.recipe + { page = Pages.Guide.Dynamic.Other.page + , toModel = OtherModel + , toMsg = OtherMsg + } } @@ -63,9 +73,12 @@ recipes = init : Route -> Page.Init Model Msg init route = case route of - Routes.Intro flags -> + Route.Intro flags -> recipes.intro.init flags + Route.Other flags -> + recipes.other.init flags + -- UPDATE @@ -77,10 +90,14 @@ update bigMsg bigModel = ( IntroMsg msg, IntroModel model ) -> recipes.intro.update msg model + ( OtherMsg msg, OtherModel model ) -> + recipes.other.update msg model + + _ -> + App.Page.keep bigModel + --- _ -> --- App.Page.keep bigModel -- BUNDLE @@ -89,3 +106,6 @@ bundle bigModel = case bigModel of IntroModel model -> recipes.intro.bundle model + + OtherModel model -> + recipes.other.bundle model diff --git a/elm-stuff/.elm-spa/Generated/Guide/Dynamic/Params.elm b/elm-stuff/.elm-spa/Generated/Guide/Dynamic/Params.elm new file mode 100644 index 0000000..f489643 --- /dev/null +++ b/elm-stuff/.elm-spa/Generated/Guide/Dynamic/Params.elm @@ -0,0 +1,14 @@ +module Generated.Guide.Dynamic.Params exposing + ( Intro + , Other + ) + + +type alias Intro = + { param1 : String + } + + +type alias Other = + { param1 : String + } diff --git a/elm-stuff/.elm-spa/Generated/Guide/Dynamic/Route.elm b/elm-stuff/.elm-spa/Generated/Guide/Dynamic/Route.elm new file mode 100644 index 0000000..7fd75fe --- /dev/null +++ b/elm-stuff/.elm-spa/Generated/Guide/Dynamic/Route.elm @@ -0,0 +1,37 @@ +module Generated.Guide.Dynamic.Route exposing + ( Route(..) + , routes + , toPath + ) + +import App.Router +import Generated.Guide.Dynamic.Params as Params +import Url.Parser as Parser exposing ((), Parser) + + +type Route + = Intro Params.Intro + | Other Params.Other + + +toPath : Route -> String +toPath route = + case route of + Intro _ -> + "/intro" + + Other _ -> + "/other" + + +routes : + { param1 : String } + -> List (Parser (Route -> a) a) +routes params = + let + router = + App.Router.create params + in + [ router.path Intro "intro" + , router.path Other "other" + ] diff --git a/elm-stuff/.elm-spa/Generated/Guide/Dynamic/Routes.elm b/elm-stuff/.elm-spa/Generated/Guide/Dynamic/Routes.elm deleted file mode 100644 index 3ee82e4..0000000 --- a/elm-stuff/.elm-spa/Generated/Guide/Dynamic/Routes.elm +++ /dev/null @@ -1,25 +0,0 @@ -module Generated.Guide.Dynamic.Routes exposing - ( Route(..) - , routes - , toPath - ) - -import App.Route as Route -import Generated.Guide.Dynamic.Flags as Flags - - -type Route - = Intro Flags.Intro - - -routes : List (Route.Route Route a) -routes = - [ Route.path "intro" Intro - ] - - -toPath : Route -> String -toPath route = - case route of - Intro _ -> - "/intro" diff --git a/elm-stuff/.elm-spa/Generated/Guide/Pages.elm b/elm-stuff/.elm-spa/Generated/Guide/Pages.elm index a22c044..03daa93 100644 --- a/elm-stuff/.elm-spa/Generated/Guide/Pages.elm +++ b/elm-stuff/.elm-spa/Generated/Guide/Pages.elm @@ -6,9 +6,9 @@ module Generated.Guide.Pages exposing import App.Page import Generated.Guide.Dynamic.Pages -import Generated.Guide.Dynamic.Routes -import Generated.Guide.Flags as Flags -import Generated.Guide.Routes as Routes exposing (Route(..)) +import Generated.Guide.Dynamic.Route +import Generated.Guide.Params as Params +import Generated.Guide.Route as Route exposing (Route) import Layouts.Guide as Layout import Pages.Guide.Elm import Pages.Guide.ElmSpa @@ -51,10 +51,10 @@ type alias Recipe flags model msg appMsg = type alias Recipes msg = - { elm : Recipe Flags.Elm Pages.Guide.Elm.Model Pages.Guide.Elm.Msg msg - , elmApp : Recipe Flags.ElmSpa Pages.Guide.ElmSpa.Model Pages.Guide.ElmSpa.Msg msg - , programming : Recipe Flags.Programming Pages.Guide.Programming.Model Pages.Guide.Programming.Msg msg - , dynamic_folder : Recipe Generated.Guide.Dynamic.Routes.Route Generated.Guide.Dynamic.Pages.Model Generated.Guide.Dynamic.Pages.Msg msg + { elm : Recipe Params.Elm Pages.Guide.Elm.Model Pages.Guide.Elm.Msg msg + , elmApp : Recipe Params.ElmSpa Pages.Guide.ElmSpa.Model Pages.Guide.ElmSpa.Msg msg + , programming : Recipe Params.Programming Pages.Guide.Programming.Model Pages.Guide.Programming.Msg msg + , dynamic_folder : Recipe Generated.Guide.Dynamic.Route.Route Generated.Guide.Dynamic.Pages.Model Generated.Guide.Dynamic.Pages.Msg msg } @@ -94,16 +94,16 @@ recipes = init : Route -> Page.Init Model Msg init route = case route of - Routes.Elm flags -> + Route.Elm flags -> recipes.elm.init flags - Routes.ElmSpa flags -> + Route.ElmSpa flags -> recipes.elmApp.init flags - Routes.Programming flags -> + Route.Programming flags -> recipes.programming.init flags - Routes.Dynamic_Folder flags route_ -> + Route.Dynamic_Folder flags route_ -> recipes.dynamic_folder.init route_ diff --git a/elm-stuff/.elm-spa/Generated/Guide/Flags.elm b/elm-stuff/.elm-spa/Generated/Guide/Params.elm similarity index 65% rename from elm-stuff/.elm-spa/Generated/Guide/Flags.elm rename to elm-stuff/.elm-spa/Generated/Guide/Params.elm index b4876e9..7431fbe 100644 --- a/elm-stuff/.elm-spa/Generated/Guide/Flags.elm +++ b/elm-stuff/.elm-spa/Generated/Guide/Params.elm @@ -1,4 +1,4 @@ -module Generated.Guide.Flags exposing +module Generated.Guide.Params exposing ( Elm , ElmSpa , Programming @@ -6,12 +6,12 @@ module Generated.Guide.Flags exposing type alias Elm = - () + {} type alias ElmSpa = - () + {} type alias Programming = - () + {} diff --git a/elm-stuff/.elm-spa/Generated/Guide/Route.elm b/elm-stuff/.elm-spa/Generated/Guide/Route.elm new file mode 100644 index 0000000..ad923c9 --- /dev/null +++ b/elm-stuff/.elm-spa/Generated/Guide/Route.elm @@ -0,0 +1,50 @@ +module Generated.Guide.Route exposing + ( Route(..) + , routes + , toPath + ) + +import App.Router +import Generated.Guide.Dynamic.Route as Dynamic_Routes +import Generated.Guide.Params as Params +import Url.Parser as Parser exposing ((), Parser) + + +type Route + = Elm Params.Elm + | ElmSpa Params.ElmSpa + | Programming Params.Programming + | Dynamic_Folder String Dynamic_Routes.Route + + +toPath : Route -> String +toPath route = + case route of + Elm _ -> + "/elm" + + ElmSpa _ -> + "/elm-spa" + + Programming _ -> + "/programming" + + Dynamic_Folder value subRoute -> + "/" ++ value ++ Dynamic_Routes.toPath subRoute + + +routes : + {} + -> List (Parser (Route -> a) a) +routes params = + let + router = + App.Router.create params + in + [ router.path Elm "elm" + , router.path ElmSpa "elm-spa" + , router.path Programming "programming" + , router.dynamicFolder Dynamic_Folder + (\param1 -> { param1 = param1 }) + Dynamic_Routes.routes + ] diff --git a/elm-stuff/.elm-spa/Generated/Guide/Routes.elm b/elm-stuff/.elm-spa/Generated/Guide/Routes.elm deleted file mode 100644 index 7b9b1d6..0000000 --- a/elm-stuff/.elm-spa/Generated/Guide/Routes.elm +++ /dev/null @@ -1,41 +0,0 @@ -module Generated.Guide.Routes exposing - ( Route(..) - , routes - , toPath - ) - -import App.Route as Route -import Generated.Guide.Dynamic.Routes -import Generated.Guide.Flags as Flags - - -type Route - = Elm Flags.Elm - | ElmSpa Flags.ElmSpa - | Programming Flags.Programming - | Dynamic_Folder String Generated.Guide.Dynamic.Routes.Route - - -routes : List (Route.Route Route a) -routes = - [ Route.path "elm" Elm - , Route.path "elm-spa" ElmSpa - , Route.path "programming" Programming - , Route.dynamicFolder Dynamic_Folder Generated.Guide.Dynamic.Routes.routes - ] - - -toPath : Route -> String -toPath route = - case route of - Elm _ -> - "/" - - ElmSpa _ -> - "/elm-spa" - - Programming _ -> - "/programming" - - Dynamic_Folder string route_ -> - "/" ++ string ++ Generated.Guide.Dynamic.Routes.toPath route_ diff --git a/elm-stuff/.elm-spa/Generated/Pages.elm b/elm-stuff/.elm-spa/Generated/Pages.elm index 678320f..9e079ba 100644 --- a/elm-stuff/.elm-spa/Generated/Pages.elm +++ b/elm-stuff/.elm-spa/Generated/Pages.elm @@ -6,11 +6,11 @@ module Generated.Pages exposing import App.Page import Generated.Docs.Pages -import Generated.Docs.Routes -import Generated.Flags as Flags +import Generated.Docs.Route import Generated.Guide.Pages -import Generated.Guide.Routes -import Generated.Routes as Routes exposing (Route(..)) +import Generated.Guide.Route +import Generated.Params as Params +import Generated.Route as Route exposing (Route(..)) import Layout as Layout import Pages.Docs import Pages.Guide @@ -61,13 +61,13 @@ type alias Recipe flags model msg appMsg = type alias Recipes msg = - { top : Recipe Flags.Top Pages.Top.Model Pages.Top.Msg msg - , docs : Recipe Flags.Docs Pages.Docs.Model Pages.Docs.Msg msg - , notFound : Recipe Flags.NotFound Pages.NotFound.Model Pages.NotFound.Msg msg - , signIn : Recipe Flags.SignIn Pages.SignIn.Model Pages.SignIn.Msg msg - , guide : Recipe Flags.Guide Pages.Guide.Model Pages.Guide.Msg msg - , guide_folder : Recipe Generated.Guide.Routes.Route Generated.Guide.Pages.Model Generated.Guide.Pages.Msg msg - , docs_folder : Recipe Generated.Docs.Routes.Route Generated.Docs.Pages.Model Generated.Docs.Pages.Msg msg + { top : Recipe Params.Top Pages.Top.Model Pages.Top.Msg msg + , docs : Recipe Params.Docs Pages.Docs.Model Pages.Docs.Msg msg + , notFound : Recipe Params.NotFound Pages.NotFound.Model Pages.NotFound.Msg msg + , signIn : Recipe Params.SignIn Pages.SignIn.Model Pages.SignIn.Msg msg + , guide : Recipe Params.Guide Pages.Guide.Model Pages.Guide.Msg msg + , guide_folder : Recipe Generated.Guide.Route.Route Generated.Guide.Pages.Model Generated.Guide.Pages.Msg msg + , docs_folder : Recipe Generated.Docs.Route.Route Generated.Docs.Pages.Model Generated.Docs.Pages.Msg msg } @@ -125,25 +125,25 @@ recipes = init : Route -> Page.Init Model Msg init route_ = case route_ of - Routes.Top flags -> + Route.Top flags -> recipes.top.init flags - Routes.Docs flags -> + Route.Docs flags -> recipes.docs.init flags - Routes.NotFound flags -> + Route.NotFound flags -> recipes.notFound.init flags - Routes.SignIn flags -> + Route.SignIn flags -> recipes.signIn.init flags - Routes.Guide flags -> + Route.Guide flags -> recipes.guide.init flags - Routes.Guide_Folder route -> + Route.Guide_Folder route -> recipes.guide_folder.init route - Routes.Docs_Folder route -> + Route.Docs_Folder route -> recipes.docs_folder.init route diff --git a/elm-stuff/.elm-spa/Generated/Flags.elm b/elm-stuff/.elm-spa/Generated/Params.elm similarity index 71% rename from elm-stuff/.elm-spa/Generated/Flags.elm rename to elm-stuff/.elm-spa/Generated/Params.elm index 80f794c..7cbf831 100644 --- a/elm-stuff/.elm-spa/Generated/Flags.elm +++ b/elm-stuff/.elm-spa/Generated/Params.elm @@ -1,4 +1,4 @@ -module Generated.Flags exposing +module Generated.Params exposing ( Docs , Guide , NotFound @@ -8,20 +8,20 @@ module Generated.Flags exposing type alias Top = - () + {} type alias Docs = - () + {} type alias NotFound = - () + {} type alias SignIn = - () + {} type alias Guide = - () + {} diff --git a/elm-stuff/.elm-spa/Generated/Route.elm b/elm-stuff/.elm-spa/Generated/Route.elm new file mode 100644 index 0000000..c5b9ba1 --- /dev/null +++ b/elm-stuff/.elm-spa/Generated/Route.elm @@ -0,0 +1,63 @@ +module Generated.Route exposing + ( Route(..) + , routes + , toPath + ) + +import App.Router +import Generated.Docs.Route +import Generated.Guide.Dynamic.Route +import Generated.Guide.Route +import Generated.Params +import Url.Parser as Parser exposing ((), Parser) + + +type Route + = Top Generated.Params.Top + | Docs Generated.Params.Docs + | NotFound Generated.Params.NotFound + | SignIn Generated.Params.SignIn + | Guide Generated.Params.Guide + | Guide_Folder Generated.Guide.Route.Route + | Docs_Folder Generated.Docs.Route.Route + + +routes : List (Parser (Route -> a) a) +routes = + let + router = + App.Router.create {} + in + [ router.top Top + , router.path Docs "docs" + , router.path Guide "guide" + , router.path SignIn "sign-in" + , router.path NotFound "not-found" + , router.folder Guide_Folder "guide" Generated.Guide.Route.routes + , router.folder Docs_Folder "docs" Generated.Docs.Route.routes + ] + + +toPath : Route -> String +toPath route = + case route of + Top _ -> + "/" + + Docs _ -> + "/docs" + + NotFound _ -> + "/not-found" + + SignIn _ -> + "/sign-in" + + Guide _ -> + "/guide" + + Guide_Folder subRoute -> + "/guide" ++ Generated.Guide.Route.toPath subRoute + + Docs_Folder subRoute -> + "/docs" ++ Generated.Docs.Route.toPath subRoute diff --git a/elm-stuff/.elm-spa/Generated/Routes.elm b/elm-stuff/.elm-spa/Generated/Routes.elm deleted file mode 100644 index d6e63d8..0000000 --- a/elm-stuff/.elm-spa/Generated/Routes.elm +++ /dev/null @@ -1,57 +0,0 @@ -module Generated.Routes exposing - ( Route(..) - , routes - , toPath - ) - -import App.Route as Route -import Generated.Docs.Routes -import Generated.Flags as Flags -import Generated.Guide.Routes - - -type Route - = Top Flags.Top - | Docs Flags.Docs - | NotFound Flags.NotFound - | SignIn Flags.SignIn - | Guide Flags.Guide - | Guide_Folder Generated.Guide.Routes.Route - | Docs_Folder Generated.Docs.Routes.Route - - -routes : List (Route.Route Route a) -routes = - [ Route.top Top - , Route.path "docs" Docs - , Route.path "not-found" NotFound - , Route.path "sign-in" SignIn - , Route.path "guide" Guide - , Route.folder "guide" Guide_Folder Generated.Guide.Routes.routes - , Route.folder "docs" Docs_Folder Generated.Docs.Routes.routes - ] - - -toPath : Route -> String -toPath route = - case route of - Top _ -> - "/" - - Docs _ -> - "/docs" - - NotFound _ -> - "/not-found" - - SignIn _ -> - "/sign-in" - - Guide _ -> - "/guide" - - Guide_Folder route_ -> - "/guide" ++ Generated.Guide.Routes.toPath route_ - - Docs_Folder route_ -> - "/docs" ++ Generated.Docs.Routes.toPath route_ diff --git a/elm-stuff/.elm-spa/README.md b/elm-stuff/.elm-spa/README.md index 0253509..5d49a3a 100644 --- a/elm-stuff/.elm-spa/README.md +++ b/elm-stuff/.elm-spa/README.md @@ -2,5 +2,5 @@ > this is where all the generated code goes! Normally, this whole directory should be `.gitignore`d, -but I'm keeping it around so I can manually practice -what `elm-spa build` should be building. +but I'm keeping it around so I can better understand +what `elm-spa build` should be generating! diff --git a/example/Global.elm b/example/Global.elm index 619a815..ad7b4e8 100644 --- a/example/Global.elm +++ b/example/Global.elm @@ -7,7 +7,7 @@ module Global exposing , update ) -import Generated.Routes as Routes exposing (Route) +import Generated.Route as Route exposing (Route) type alias Flags = @@ -43,7 +43,8 @@ update commands msg model = SignIn user -> ( { model | user = Just user } , Cmd.none - , commands.navigate (Routes.Top ()) + , Cmd.none + -- , commands.navigate (Route.Top {}) ) SignOut -> diff --git a/example/Layouts/Guide.elm b/example/Layouts/Guide.elm index bba52fb..33480c9 100644 --- a/example/Layouts/Guide.elm +++ b/example/Layouts/Guide.elm @@ -1,5 +1,6 @@ module Layouts.Guide exposing (view) +import Components.Styles as Styles import Element exposing (..) import Global @@ -13,4 +14,11 @@ type alias Context msg = view : Context msg -> Element msg view { page } = - page + column + [ width fill + , spacing -128 + ] + [ page + , el [ centerX ] <| + link Styles.link { label = text "back to guide", url = "/guide" } + ] diff --git a/example/Main.elm b/example/Main.elm index 9d896a7..ce5a9c4 100644 --- a/example/Main.elm +++ b/example/Main.elm @@ -3,7 +3,7 @@ module Main exposing (main) import App import Element import Generated.Pages as Pages -import Generated.Routes as Routes +import Generated.Route as Route import Global import Pages.NotFound @@ -16,9 +16,9 @@ main = , map = Element.map } , routing = - { routes = Routes.routes - , toPath = Routes.toPath - , notFound = Routes.NotFound () + { routes = Route.routes + , toPath = Route.toPath + , notFound = Route.NotFound {} } , global = { init = Global.init diff --git a/example/Pages/Docs.elm b/example/Pages/Docs.elm index 511ff0e..102423e 100644 --- a/example/Pages/Docs.elm +++ b/example/Pages/Docs.elm @@ -3,7 +3,7 @@ module Pages.Docs exposing (Model, Msg, page) import App.Page import Components.Hero import Element exposing (..) -import Generated.Flags as Flags +import Generated.Params as Params import Utils.Page exposing (Page) @@ -15,7 +15,7 @@ type alias Msg = Never -page : Page Flags.Docs Model Msg model msg appMsg +page : Page Params.Docs Model Msg model msg appMsg page = App.Page.static { title = always "Docs" diff --git a/example/Pages/Docs/Dynamic.elm b/example/Pages/Docs/Dynamic.elm index 719f21c..b32932b 100644 --- a/example/Pages/Docs/Dynamic.elm +++ b/example/Pages/Docs/Dynamic.elm @@ -3,7 +3,7 @@ module Pages.Docs.Dynamic exposing (Model, Msg, page) import App.Page import Components.Hero import Element exposing (..) -import Generated.Docs.Flags as Flags +import Generated.Docs.Params as Params import Global import Utils.Page exposing (Page) @@ -17,7 +17,7 @@ type alias Msg = Never -page : Page Flags.Dynamic Model Msg model msg appMsg +page : Page Params.Dynamic Model Msg model msg appMsg page = App.Page.sandbox { title = always "Dynamic" @@ -31,9 +31,9 @@ page = -- INIT -init : Flags.Dynamic -> Model -init slug = - { slug = slug +init : Params.Dynamic -> Model +init { param1 } = + { slug = param1 } diff --git a/example/Pages/Docs/Static.elm b/example/Pages/Docs/Static.elm index 8f15585..edeeae1 100644 --- a/example/Pages/Docs/Static.elm +++ b/example/Pages/Docs/Static.elm @@ -3,7 +3,7 @@ module Pages.Docs.Static exposing (Model, Msg, page) import App.Page import Components.Hero import Element exposing (..) -import Generated.Docs.Flags as Flags +import Generated.Docs.Params as Params import Utils.Page exposing (Page) @@ -15,7 +15,7 @@ type alias Msg = Never -page : Page Flags.Static Model Msg model msg appMsg +page : Page Params.Static Model Msg model msg appMsg page = App.Page.static { title = always "Static" diff --git a/example/Pages/Guide.elm b/example/Pages/Guide.elm index de419f0..837a864 100644 --- a/example/Pages/Guide.elm +++ b/example/Pages/Guide.elm @@ -3,7 +3,7 @@ module Pages.Guide exposing (Model, Msg, page) import App.Page import Components.Hero import Element exposing (..) -import Generated.Flags as Flags +import Generated.Params as Params import Utils.Page exposing (Page) @@ -15,7 +15,7 @@ type alias Msg = Never -page : Page Flags.Guide Model Msg model msg appMsg +page : Page Params.Guide Model Msg model msg appMsg page = App.Page.static { title = always "Guide" diff --git a/example/Pages/Guide/Dynamic/Intro.elm b/example/Pages/Guide/Dynamic/Intro.elm index 677fb97..2eb383c 100644 --- a/example/Pages/Guide/Dynamic/Intro.elm +++ b/example/Pages/Guide/Dynamic/Intro.elm @@ -3,37 +3,50 @@ module Pages.Guide.Dynamic.Intro exposing (Model, Msg, page) import App.Page import Components.Hero import Element exposing (..) -import Generated.Guide.Dynamic.Flags as Flags +import Generated.Guide.Dynamic.Params as Params import Utils.Page exposing (Page) type alias Model = - () + { slug : String + } type alias Msg = Never -page : Page Flags.Intro Model Msg model msg appMsg +page : Page Params.Intro Model Msg model msg appMsg page = - App.Page.static + App.Page.sandbox { title = always "Guide.Dynamic.Intro" + , init = always init + , update = always update , view = always view } +init : Params.Intro -> Model +init { param1 } = + { slug = param1 + } + + +update msg model = + model + + -- VIEW -view : Element Msg -view = +view : Model -> Element Msg +view model = column [ width fill ] [ Components.Hero.view - { title = "intro" + { title = "intro to " ++ model.slug , subtitle = text "\"you're gonna be great.\"" , buttons = [] } diff --git a/example/Pages/Guide/Dynamic/Other.elm b/example/Pages/Guide/Dynamic/Other.elm new file mode 100644 index 0000000..95d2d9f --- /dev/null +++ b/example/Pages/Guide/Dynamic/Other.elm @@ -0,0 +1,53 @@ +module Pages.Guide.Dynamic.Other exposing (Model, Msg, page) + +import App.Page +import Components.Hero +import Element exposing (..) +import Generated.Guide.Dynamic.Params as Params +import Utils.Page exposing (Page) + + +type alias Model = + { slug : String + } + + +type alias Msg = + Never + + +page : Page Params.Other Model Msg model msg appMsg +page = + App.Page.sandbox + { title = always "Guide.Dynamic.Other" + , init = always init + , update = always update + , view = always view + } + + +init : Params.Other -> Model +init { param1 } = + { slug = param1 + } + + +update msg model = + model + + + +-- VIEW + + +view : Model -> Element Msg +view model = + column + [ width fill + ] + [ Components.Hero.view + { title = "other " ++ model.slug + , subtitle = text "\"you're gonna be great.\"" + , buttons = [] + } + ] diff --git a/example/Pages/Guide/Elm.elm b/example/Pages/Guide/Elm.elm index e2c567e..f1146a7 100644 --- a/example/Pages/Guide/Elm.elm +++ b/example/Pages/Guide/Elm.elm @@ -3,7 +3,7 @@ module Pages.Guide.Elm exposing (Model, Msg, page) import App.Page import Components.Hero import Element exposing (..) -import Generated.Guide.Flags as Flags +import Generated.Guide.Params as Params import Utils.Page exposing (Page) @@ -15,7 +15,7 @@ type alias Msg = Never -page : Page Flags.Elm Model Msg model msg appMsg +page : Page Params.Elm Model Msg model msg appMsg page = App.Page.static { title = always "Guide.Elm" diff --git a/example/Pages/Guide/ElmSpa.elm b/example/Pages/Guide/ElmSpa.elm index 26b4a9a..570118c 100644 --- a/example/Pages/Guide/ElmSpa.elm +++ b/example/Pages/Guide/ElmSpa.elm @@ -3,7 +3,7 @@ module Pages.Guide.ElmSpa exposing (Model, Msg, page) import App.Page import Components.Hero import Element exposing (..) -import Generated.Guide.Flags as Flags +import Generated.Guide.Params as Params import Utils.Page exposing (Page) @@ -15,7 +15,7 @@ type alias Msg = Never -page : Page Flags.ElmSpa Model Msg model msg appMsg +page : Page Params.ElmSpa Model Msg model msg appMsg page = App.Page.static { title = always "Guide.ElmSpa" diff --git a/example/Pages/Guide/Programming.elm b/example/Pages/Guide/Programming.elm index d528c0b..26811ad 100644 --- a/example/Pages/Guide/Programming.elm +++ b/example/Pages/Guide/Programming.elm @@ -3,7 +3,7 @@ module Pages.Guide.Programming exposing (Model, Msg, page) import App.Page import Components.Hero import Element exposing (..) -import Generated.Guide.Flags as Flags +import Generated.Guide.Params as Params import Utils.Page exposing (Page) @@ -15,7 +15,7 @@ type alias Msg = Never -page : Page Flags.Programming Model Msg model msg appMsg +page : Page Params.Programming Model Msg model msg appMsg page = App.Page.static { title = always "Guide.Programming" diff --git a/example/Pages/NotFound.elm b/example/Pages/NotFound.elm index 01b6d3d..acce333 100644 --- a/example/Pages/NotFound.elm +++ b/example/Pages/NotFound.elm @@ -3,7 +3,7 @@ module Pages.NotFound exposing (Model, Msg, page) import App.Page import Components.Hero import Element exposing (..) -import Generated.Flags as Flags +import Generated.Params as Params import Utils.Page exposing (Page) @@ -15,7 +15,7 @@ type alias Msg = Never -page : Page Flags.NotFound Model Msg model msg appMsg +page : Page Params.NotFound Model Msg model msg appMsg page = App.Page.static { title = always "NotFound" @@ -31,7 +31,7 @@ view : Element Msg view = column [ width fill ] [ Components.Hero.view - { title = "that's not a place." + { title = "page not found" , subtitle = text "but i'm not even mad about it." , buttons = [ { label = text "back home", action = Components.Hero.Link "/" } diff --git a/example/Pages/SignIn.elm b/example/Pages/SignIn.elm index 4c19f04..0116fbf 100644 --- a/example/Pages/SignIn.elm +++ b/example/Pages/SignIn.elm @@ -7,7 +7,7 @@ import Element exposing (..) import Element.Border as Border import Element.Font as Font import Element.Input as Input -import Generated.Flags as Flags +import Generated.Params as Params import Global import Html exposing (Html) import Html.Attributes as Attr @@ -31,7 +31,7 @@ type Field | Password -page : Page Flags.SignIn Model Msg model msg appMsg +page : Page Params.SignIn Model Msg model msg appMsg page = App.Page.component { title = always "sign in | elm-spa" @@ -46,7 +46,7 @@ page = -- INIT -init : Flags.SignIn -> ( Model, Cmd Msg, Cmd Global.Msg ) +init : Params.SignIn -> ( Model, Cmd Msg, Cmd Global.Msg ) init flags = ( { username = "" , password = "" diff --git a/example/Pages/Top.elm b/example/Pages/Top.elm index 96ad9c8..3db85c2 100644 --- a/example/Pages/Top.elm +++ b/example/Pages/Top.elm @@ -5,7 +5,7 @@ import Components.Hero import Components.Section import Components.Styles as Styles import Element exposing (..) -import Generated.Flags as Flags +import Generated.Params as Params import Html.Attributes as Attr import Ports import Utils.Page exposing (Page) @@ -15,7 +15,7 @@ type alias Model = () -page : Page Flags.Top Model Msg model msg appMsg +page : Page Params.Top Model Msg model msg appMsg page = App.Page.element { title = always "elm-spa" @@ -30,8 +30,8 @@ page = -- INIT -init : Flags.Top -> ( Model, Cmd Msg ) -init flags = +init : Params.Top -> ( Model, Cmd Msg ) +init params = ( () , Cmd.none ) diff --git a/src/App.elm b/src/App.elm index 6c4f573..72f949f 100644 --- a/src/App.elm +++ b/src/App.elm @@ -57,14 +57,13 @@ Let `App.create` know about this by passing in your own `Options` like these: -} -import App.Route as Route exposing (Route) import Browser import Browser.Navigation as Nav import Html exposing (Html) import Internals.Page as Page import Internals.Utils as Utils import Url exposing (Url) -import Url.Parser as Parser +import Url.Parser as Parser exposing (Parser) @@ -109,7 +108,7 @@ create : , map : (layoutMsg -> Msg globalMsg layoutMsg) -> uiLayoutMsg -> uiMsg } , routing : - { routes : List (Route route route) + { routes : List (Parser (route -> route) route) , toPath : route -> String , notFound : route } @@ -186,7 +185,7 @@ create config = type alias Routes route a = - List (Route.Route route a) + List (Parser (route -> a) a) fromUrl : { a | routes : Routes route route, notFound : route } -> Url -> route diff --git a/src/App/Route.elm b/src/App/Route.elm deleted file mode 100644 index d06c824..0000000 --- a/src/App/Route.elm +++ /dev/null @@ -1,100 +0,0 @@ -module App.Route exposing - ( Route - , top - , path - , folder - , dynamic, dynamicFolder - ) - -{-| - - -# Routing - -The [cli tool](https://github.com/ryannhg/elm-spa/tree/master/cli) is able to generate routes based on the folder -structure and files in the `src/Pages` folder. - -If you're choosing to type out the routes manually, these are just -convenience functions for creating `Url.Parser` types for your application. - -@docs Route - -@docs top - -@docs path - -@docs slug - -@docs folder - --} - -import Url exposing (Url) -import Url.Parser as Parser exposing ((), Parser) - - -{-| Literally just a Url.Parser under the hood. --} -type alias Route route a = - Parser (route -> a) a - - -{-| A route for a path like. These are generated by other file names. - - Route.path "about-us" AboutUs - --} -path : String -> (() -> route) -> Route route a -path path_ toRoute = - Parser.map toRoute (Parser.s path_ |> Parser.map ()) - - -{-| A top level route. -Usually generated by an `Top.elm` file. - - Route.top Top - --} -top : (() -> route) -> Route route a -top toRoute = - Parser.map toRoute (Parser.top |> Parser.map ()) - - -{-| A dynamic route, that passes the `String` value. -Usually generated by an `Dynamic.elm` file. - - Route.dynamic Dynamic - --} -dynamic : (String -> route) -> Route route a -dynamic toRoute = - Parser.map toRoute Parser.string - - -{-| A route for nested routes, generated by a folder. - - Route.folder "settings" Settings_ Generated.Settings.Route.routes - --} -folder : - String - -> (subRoute -> route) - -> List (Route subRoute route) - -> Route route a -folder path_ toRoute children = - Parser.map toRoute - (Parser.s path_ Parser.oneOf children) - - -{-| A route for nested dynamic routes, generated by a folder. - - Route.dynamicFolder Dynamic_ Dynamic_.routes - --} -dynamicFolder : - (String -> subRoute -> route) - -> List (Route subRoute route) - -> Route route a -dynamicFolder toRoute children = - Parser.map toRoute - (Parser.string Parser.oneOf children) diff --git a/src/App/Router.elm b/src/App/Router.elm new file mode 100644 index 0000000..797f832 --- /dev/null +++ b/src/App/Router.elm @@ -0,0 +1,83 @@ +module App.Router exposing (Router, create) + +import Url.Parser as Parser exposing ((), Parser) + + +router = + { top = + \r -> Parser.map (r {}) <| Parser.top + , path = + \r p -> Parser.map (r {}) <| Parser.s p + , folder = + \r p children -> Parser.map r <| Parser.s p Parser.oneOf children + , dynamicFolder = + \folder toParams routes_ -> + Parser.string + |> andThen + (\value -> + Parser.oneOf (routes_ (toParams value)) + |> Parser.map (folder value) + ) + } + + +andThen : + (a -> Parser (b -> c) c) + -> Parser (a -> b) b + -> Parser (b -> c) c +andThen = + Debug.todo "Parser.andThen" + + +type alias Router params subParams route subRoute a = + { top : + (params -> route) + -> Parser (route -> a) a + , path : + (params -> route) + -> String + -> Parser (route -> a) a + , dynamic : + (String -> subParams -> route) + -> (String -> subParams) + -> Parser (route -> a) a + , folder : + (subRoute -> route) + -> String + -> (params -> List (Parser (subRoute -> route) route)) + -> Parser (route -> a) a + , dynamicFolder : + (String -> subRoute -> route) + -> (String -> subParams) + -> (subParams -> List (Parser (subRoute -> route) route)) + -> Parser (route -> a) a + } + + +create : params -> Router params subParams route subRoute a +create params = + { top = + \toRoute -> + Parser.map (toRoute params) + Parser.top + , path = + \toRoute path -> + Parser.map (toRoute params) + (Parser.s path) + , dynamic = + \toRoute toParams -> + Parser.map (\value -> toRoute value (toParams value)) + Parser.string + , folder = + \toRoute path children -> + Parser.map toRoute + (Parser.s path Parser.oneOf (children params)) + , dynamicFolder = + \toRoute toParams routes_ -> + Parser.string + |> andThen + (\value -> + Parser.oneOf (routes_ (toParams value)) + |> Parser.map (toRoute value) + ) + }