From 706ead2dc17ada0dabb53d7203048fe68fe6933c Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Mon, 4 Apr 2022 16:22:45 -0700 Subject: [PATCH] Update some test examples. --- examples/base-path/app/Effect.elm | 56 ++++++++++++++++++++-- examples/base-path/app/ErrorPage.elm | 46 +++++++++++++++--- examples/base-path/package-lock.json | 8 ++-- examples/escaping/app/Effect.elm | 45 +++++++++++++++-- examples/escaping/app/ErrorPage.elm | 46 +++++++++++++++--- examples/escaping/dist/escaping/index.html | 6 +-- examples/escaping/package-lock.json | 8 ++-- examples/routing/app/Effect.elm | 44 +++++++++++++++-- examples/routing/app/ErrorPage.elm | 46 +++++++++++++++--- examples/routing/package-lock.json | 8 ++-- 10 files changed, 268 insertions(+), 45 deletions(-) diff --git a/examples/base-path/app/Effect.elm b/examples/base-path/app/Effect.elm index 00a8a270..348d99c3 100644 --- a/examples/base-path/app/Effect.elm +++ b/examples/base-path/app/Effect.elm @@ -1,12 +1,27 @@ module Effect exposing (Effect(..), batch, fromCmd, map, none, perform) import Browser.Navigation +import Http +import Json.Decode as Decode +import Url exposing (Url) type Effect msg = None | Cmd (Cmd msg) | Batch (List (Effect msg)) + | GetStargazers (Result Http.Error Int -> msg) + | FetchPageData + { body : Maybe { contentType : String, body : String } + , path : Maybe String + , toMsg : Result Http.Error Url -> msg + } + + +type alias RequestInfo = + { contentType : String + , body : String + } none : Effect msg @@ -36,9 +51,30 @@ map fn effect = Batch list -> Batch (List.map (map fn) list) + GetStargazers toMsg -> + GetStargazers (toMsg >> fn) -perform : (pageMsg -> msg) -> Browser.Navigation.Key -> Effect pageMsg -> Cmd msg -perform fromPageMsg key effect = + FetchPageData fetchInfo -> + FetchPageData + { body = fetchInfo.body + , path = fetchInfo.path + , toMsg = fetchInfo.toMsg >> fn + } + + +perform : + { fetchRouteData : + { body : Maybe { contentType : String, body : String } + , path : Maybe String + , toMsg : Result Http.Error Url -> pageMsg + } + -> Cmd msg + } + -> (pageMsg -> msg) + -> Browser.Navigation.Key + -> Effect pageMsg + -> Cmd msg +perform info fromPageMsg key effect = case effect of None -> Cmd.none @@ -47,4 +83,18 @@ perform fromPageMsg key effect = Cmd.map fromPageMsg cmd Batch list -> - Cmd.batch (List.map (perform fromPageMsg key) list) + Cmd.batch (List.map (perform info fromPageMsg key) list) + + GetStargazers toMsg -> + Http.get + { url = + "https://api.github.com/repos/dillonkearns/elm-pages" + , expect = Http.expectJson (toMsg >> fromPageMsg) (Decode.field "stargazers_count" Decode.int) + } + + FetchPageData fetchInfo -> + info.fetchRouteData + { body = fetchInfo.body + , path = fetchInfo.path + , toMsg = fetchInfo.toMsg + } diff --git a/examples/base-path/app/ErrorPage.elm b/examples/base-path/app/ErrorPage.elm index 33fd455c..e6f3f0af 100644 --- a/examples/base-path/app/ErrorPage.elm +++ b/examples/base-path/app/ErrorPage.elm @@ -1,6 +1,37 @@ -module ErrorPage exposing (ErrorPage(..), internalError, notFound, statusCode, view) +module ErrorPage exposing (ErrorPage(..), Model, Msg, head, init, internalError, notFound, statusCode, update, view) -import Html exposing (Html) +import Effect exposing (Effect) +import Head +import Html.Styled as Html exposing (Html) +import View exposing (View) + + +type Msg + = Increment + + +type alias Model = + { count : Int + } + + +init : ErrorPage -> ( Model, Effect Msg ) +init errorPage = + ( { count = 0 } + , Effect.none + ) + + +update : ErrorPage -> Msg -> Model -> ( Model, Effect Msg ) +update errorPage msg model = + case msg of + Increment -> + ( { model | count = model.count + 1 }, Effect.none ) + + +head : ErrorPage -> List Head.Tag +head errorPage = + [] type ErrorPage @@ -18,13 +49,14 @@ internalError = InternalError -view : ErrorPage -> { body : Html msg, title : String } -view error = +view : ErrorPage -> Model -> View Msg +view error model = { body = - Html.div [] - [ Html.text "Hi! This is a NotFound error" + [ Html.div [] + [ Html.p [] [ Html.text "Page not found. Maybe try another URL?" ] ] - , title = "Error" + ] + , title = "This is a NotFound Error" } diff --git a/examples/base-path/package-lock.json b/examples/base-path/package-lock.json index 832cbdc9..7ce70895 100644 --- a/examples/base-path/package-lock.json +++ b/examples/base-path/package-lock.json @@ -41,8 +41,8 @@ "node-fetch": "^2.6.7", "object-hash": "^2.2.0", "serve-static": "^1.14.1", - "terser": "^5.12.0", - "vite": "^2.8.6", + "terser": "^5.12.1", + "vite": "^2.9.1", "which": "^2.0.2" }, "bin": { @@ -1585,9 +1585,9 @@ "node-fetch": "^2.6.7", "object-hash": "^2.2.0", "serve-static": "^1.14.1", - "terser": "^5.12.0", + "terser": "^5.12.1", "typescript": "4.3.5", - "vite": "^2.8.6", + "vite": "^2.9.1", "which": "^2.0.2" }, "dependencies": { diff --git a/examples/escaping/app/Effect.elm b/examples/escaping/app/Effect.elm index 00a8a270..26e577aa 100644 --- a/examples/escaping/app/Effect.elm +++ b/examples/escaping/app/Effect.elm @@ -1,12 +1,26 @@ module Effect exposing (Effect(..), batch, fromCmd, map, none, perform) import Browser.Navigation +import Http +import Json.Decode as Decode +import Url exposing (Url) type Effect msg = None | Cmd (Cmd msg) | Batch (List (Effect msg)) + | FetchPageData + { body : Maybe { contentType : String, body : String } + , path : Maybe String + , toMsg : Result Http.Error Url -> msg + } + + +type alias RequestInfo = + { contentType : String + , body : String + } none : Effect msg @@ -36,9 +50,27 @@ map fn effect = Batch list -> Batch (List.map (map fn) list) + FetchPageData fetchInfo -> + FetchPageData + { body = fetchInfo.body + , path = fetchInfo.path + , toMsg = fetchInfo.toMsg >> fn + } -perform : (pageMsg -> msg) -> Browser.Navigation.Key -> Effect pageMsg -> Cmd msg -perform fromPageMsg key effect = + +perform : + { fetchRouteData : + { body : Maybe { contentType : String, body : String } + , path : Maybe String + , toMsg : Result Http.Error Url -> pageMsg + } + -> Cmd msg + } + -> (pageMsg -> msg) + -> Browser.Navigation.Key + -> Effect pageMsg + -> Cmd msg +perform info fromPageMsg key effect = case effect of None -> Cmd.none @@ -47,4 +79,11 @@ perform fromPageMsg key effect = Cmd.map fromPageMsg cmd Batch list -> - Cmd.batch (List.map (perform fromPageMsg key) list) + Cmd.batch (List.map (perform info fromPageMsg key) list) + + FetchPageData fetchInfo -> + info.fetchRouteData + { body = fetchInfo.body + , path = fetchInfo.path + , toMsg = fetchInfo.toMsg + } diff --git a/examples/escaping/app/ErrorPage.elm b/examples/escaping/app/ErrorPage.elm index 33fd455c..e6f3f0af 100644 --- a/examples/escaping/app/ErrorPage.elm +++ b/examples/escaping/app/ErrorPage.elm @@ -1,6 +1,37 @@ -module ErrorPage exposing (ErrorPage(..), internalError, notFound, statusCode, view) +module ErrorPage exposing (ErrorPage(..), Model, Msg, head, init, internalError, notFound, statusCode, update, view) -import Html exposing (Html) +import Effect exposing (Effect) +import Head +import Html.Styled as Html exposing (Html) +import View exposing (View) + + +type Msg + = Increment + + +type alias Model = + { count : Int + } + + +init : ErrorPage -> ( Model, Effect Msg ) +init errorPage = + ( { count = 0 } + , Effect.none + ) + + +update : ErrorPage -> Msg -> Model -> ( Model, Effect Msg ) +update errorPage msg model = + case msg of + Increment -> + ( { model | count = model.count + 1 }, Effect.none ) + + +head : ErrorPage -> List Head.Tag +head errorPage = + [] type ErrorPage @@ -18,13 +49,14 @@ internalError = InternalError -view : ErrorPage -> { body : Html msg, title : String } -view error = +view : ErrorPage -> Model -> View Msg +view error model = { body = - Html.div [] - [ Html.text "Hi! This is a NotFound error" + [ Html.div [] + [ Html.p [] [ Html.text "Page not found. Maybe try another URL?" ] ] - , title = "Error" + ] + , title = "This is a NotFound Error" } diff --git a/examples/escaping/dist/escaping/index.html b/examples/escaping/dist/escaping/index.html index 8a1de0ee..5788a0bf 100644 --- a/examples/escaping/dist/escaping/index.html +++ b/examples/escaping/dist/escaping/index.html @@ -1,7 +1,7 @@ - + @@ -17,8 +17,8 @@ content="black-translucent" /> - - + + diff --git a/examples/escaping/package-lock.json b/examples/escaping/package-lock.json index 9b77cea8..cb3b130f 100644 --- a/examples/escaping/package-lock.json +++ b/examples/escaping/package-lock.json @@ -40,8 +40,8 @@ "node-fetch": "^2.6.7", "object-hash": "^2.2.0", "serve-static": "^1.14.1", - "terser": "^5.12.0", - "vite": "^2.8.6", + "terser": "^5.12.1", + "vite": "^2.9.1", "which": "^2.0.2" }, "bin": { @@ -1526,9 +1526,9 @@ "node-fetch": "^2.6.7", "object-hash": "^2.2.0", "serve-static": "^1.14.1", - "terser": "^5.12.0", + "terser": "^5.12.1", "typescript": "4.3.5", - "vite": "^2.8.6", + "vite": "^2.9.1", "which": "^2.0.2" }, "dependencies": { diff --git a/examples/routing/app/Effect.elm b/examples/routing/app/Effect.elm index 00a8a270..8e40d37e 100644 --- a/examples/routing/app/Effect.elm +++ b/examples/routing/app/Effect.elm @@ -1,12 +1,25 @@ module Effect exposing (Effect(..), batch, fromCmd, map, none, perform) import Browser.Navigation +import Http +import Url exposing (Url) type Effect msg = None | Cmd (Cmd msg) | Batch (List (Effect msg)) + | FetchPageData + { body : Maybe { contentType : String, body : String } + , path : Maybe String + , toMsg : Result Http.Error Url -> msg + } + + +type alias RequestInfo = + { contentType : String + , body : String + } none : Effect msg @@ -36,9 +49,27 @@ map fn effect = Batch list -> Batch (List.map (map fn) list) + FetchPageData fetchInfo -> + FetchPageData + { body = fetchInfo.body + , path = fetchInfo.path + , toMsg = fetchInfo.toMsg >> fn + } -perform : (pageMsg -> msg) -> Browser.Navigation.Key -> Effect pageMsg -> Cmd msg -perform fromPageMsg key effect = + +perform : + { fetchRouteData : + { body : Maybe { contentType : String, body : String } + , path : Maybe String + , toMsg : Result Http.Error Url -> pageMsg + } + -> Cmd msg + } + -> (pageMsg -> msg) + -> Browser.Navigation.Key + -> Effect pageMsg + -> Cmd msg +perform info fromPageMsg key effect = case effect of None -> Cmd.none @@ -47,4 +78,11 @@ perform fromPageMsg key effect = Cmd.map fromPageMsg cmd Batch list -> - Cmd.batch (List.map (perform fromPageMsg key) list) + Cmd.batch (List.map (perform info fromPageMsg key) list) + + FetchPageData fetchInfo -> + info.fetchRouteData + { body = fetchInfo.body + , path = fetchInfo.path + , toMsg = fetchInfo.toMsg + } diff --git a/examples/routing/app/ErrorPage.elm b/examples/routing/app/ErrorPage.elm index 33fd455c..e6f3f0af 100644 --- a/examples/routing/app/ErrorPage.elm +++ b/examples/routing/app/ErrorPage.elm @@ -1,6 +1,37 @@ -module ErrorPage exposing (ErrorPage(..), internalError, notFound, statusCode, view) +module ErrorPage exposing (ErrorPage(..), Model, Msg, head, init, internalError, notFound, statusCode, update, view) -import Html exposing (Html) +import Effect exposing (Effect) +import Head +import Html.Styled as Html exposing (Html) +import View exposing (View) + + +type Msg + = Increment + + +type alias Model = + { count : Int + } + + +init : ErrorPage -> ( Model, Effect Msg ) +init errorPage = + ( { count = 0 } + , Effect.none + ) + + +update : ErrorPage -> Msg -> Model -> ( Model, Effect Msg ) +update errorPage msg model = + case msg of + Increment -> + ( { model | count = model.count + 1 }, Effect.none ) + + +head : ErrorPage -> List Head.Tag +head errorPage = + [] type ErrorPage @@ -18,13 +49,14 @@ internalError = InternalError -view : ErrorPage -> { body : Html msg, title : String } -view error = +view : ErrorPage -> Model -> View Msg +view error model = { body = - Html.div [] - [ Html.text "Hi! This is a NotFound error" + [ Html.div [] + [ Html.p [] [ Html.text "Page not found. Maybe try another URL?" ] ] - , title = "Error" + ] + , title = "This is a NotFound Error" } diff --git a/examples/routing/package-lock.json b/examples/routing/package-lock.json index 9b77cea8..cb3b130f 100644 --- a/examples/routing/package-lock.json +++ b/examples/routing/package-lock.json @@ -40,8 +40,8 @@ "node-fetch": "^2.6.7", "object-hash": "^2.2.0", "serve-static": "^1.14.1", - "terser": "^5.12.0", - "vite": "^2.8.6", + "terser": "^5.12.1", + "vite": "^2.9.1", "which": "^2.0.2" }, "bin": { @@ -1526,9 +1526,9 @@ "node-fetch": "^2.6.7", "object-hash": "^2.2.0", "serve-static": "^1.14.1", - "terser": "^5.12.0", + "terser": "^5.12.1", "typescript": "4.3.5", - "vite": "^2.8.6", + "vite": "^2.9.1", "which": "^2.0.2" }, "dependencies": {