Update blog example.

This commit is contained in:
Dillon Kearns 2023-02-04 12:38:42 -08:00
parent 285f3022b2
commit 4482820641
4 changed files with 47 additions and 51 deletions

View File

@ -6,13 +6,12 @@ import FatalError exposing (FatalError)
import Head
import Head.Seo as Seo
import Html
import Html.Styled.Attributes as Attr
import Json.Decode as Decode
import Json.Encode as Encode
import Pages.Msg
import Pages.PageUrl exposing (PageUrl)
import Pages.Url
import Path
import Post exposing (Post)
import Route
import RouteBuilder exposing (StatefulRoute, StatelessRoute, StaticPayload)
import Shared
@ -45,7 +44,7 @@ route =
type alias Data =
{ posts : List String
{ posts : List Post
}
@ -55,16 +54,11 @@ data =
|> BackendTask.andMap
(BackendTask.Custom.run "posts"
Encode.null
(Decode.list postDecoder)
(Decode.list Post.decoder)
|> BackendTask.allowFatal
)
postDecoder : Decode.Decoder String
postDecoder =
Decode.field "title" Decode.string
head :
StaticPayload Data ActionData RouteParams
-> List Head.Tag
@ -94,7 +88,17 @@ view maybeUrl sharedModel app =
{ title = "Index page"
, body =
[ app.data.posts
|> String.join ", "
|> Html.text
|> List.map postView
|> Html.ul []
]
}
postView : Post -> Html.Html msg
postView post =
Html.li []
[ Route.Posts__Slug___Edit { slug = post.slug }
|> Route.link []
[ Html.text post.title
]
]

View File

@ -8,7 +8,6 @@ module Route.Posts.Slug_ exposing (ActionData, Data, route, RouteParams, Msg, Mo
import BackendTask
import BackendTask.Custom
import Date exposing (Date)
import Effect
import ErrorPage
import FatalError
@ -23,12 +22,12 @@ import Pages.Msg
import Pages.PageUrl
import Path
import Platform.Sub
import Post
import Route
import RouteBuilder
import Server.Request
import Server.Response
import Shared
import Time
import View
@ -104,7 +103,7 @@ data routeParams =
Server.Request.succeed
(BackendTask.Custom.run "getPost"
(Encode.string routeParams.slug)
(Decode.nullable postDecoder)
(Decode.nullable Post.decoder)
|> BackendTask.allowFatal
|> BackendTask.andThen
(\maybePost ->
@ -134,28 +133,6 @@ data routeParams =
)
type alias Post =
{ title : String
, body : String
, slug : String
, publish : Maybe Date
}
postDecoder : Decoder Post
postDecoder =
Decode.map4 Post
(Decode.field "title" Decode.string)
(Decode.field "body" Decode.string)
(Decode.field "slug" Decode.string)
(Decode.field "publish"
(Decode.nullable
(Decode.int |> Decode.map (Time.millisToPosix >> Date.fromPosix Time.utc))
)
)
|> Decode.map (Debug.log "postDecoder")
head : RouteBuilder.StaticPayload Data ActionData RouteParams -> List Head.Tag
head app =
[]

View File

@ -28,6 +28,7 @@ import Pages.PageUrl
import Pages.Script
import Path
import Platform.Sub
import Post
import Route
import RouteBuilder
import Server.Request
@ -110,20 +111,6 @@ type alias Post =
}
postDecoder : Decoder Post
postDecoder =
Decode.map4 Post
(Decode.field "title" Decode.string)
(Decode.field "body" Decode.string)
(Decode.field "slug" Decode.string)
(Decode.field "publish"
(Decode.nullable
(Decode.int |> Decode.map (Time.millisToPosix >> Date.fromPosix Time.utc))
)
)
|> Decode.map (Debug.log "postDecoder")
data :
RouteParams
-> Server.Request.Parser (BackendTask.BackendTask FatalError.FatalError (Server.Response.Response Data ErrorPage.ErrorPage))
@ -131,7 +118,7 @@ data routeParams =
Server.Request.succeed
(BackendTask.Custom.run "getPost"
(Encode.string routeParams.slug)
(Decode.nullable postDecoder)
(Decode.nullable Post.decoder)
|> BackendTask.allowFatal
|> BackendTask.map
(\maybePost ->

View File

@ -0,0 +1,28 @@
module Post exposing (Post, decoder)
import BackendTask.Custom
import Date exposing (Date)
import Json.Decode as Decode exposing (Decoder)
import Time
type alias Post =
{ title : String
, body : String
, slug : String
, publish : Maybe Date
}
decoder : Decoder Post
decoder =
Decode.map4 Post
(Decode.field "title" Decode.string)
(Decode.field "body" Decode.string)
(Decode.field "slug" Decode.string)
(Decode.field "publish"
(Decode.nullable
BackendTask.Custom.dateDecoder
--(Decode.int |> Decode.map (Time.millisToPosix >> Date.fromPosix Time.utc))
)
)