Fix hackernews example.

This commit is contained in:
Dillon Kearns 2022-06-29 12:13:34 -07:00
parent f1a2c3c0a9
commit 6850d98562
3 changed files with 32 additions and 30 deletions

View File

@ -3,6 +3,7 @@ module Effect exposing (Effect(..), batch, fromCmd, map, none, perform)
import Browser.Navigation
import Bytes exposing (Bytes)
import Bytes.Decode
import FormDecoder
import Http
import Json.Decode as Decode
import Pages.Fetcher
@ -14,15 +15,13 @@ type Effect msg
| Cmd (Cmd msg)
| Batch (List (Effect msg))
| GetStargazers (Result Http.Error Int -> msg)
| SetField { formId : String, name : String, value : String }
| FetchRouteData
{ body : Maybe { contentType : String, body : String }
, path : Maybe String
{ data : Maybe FormDecoder.FormData
, toMsg : Result Http.Error Url -> msg
}
| Submit
{ values : List ( String, String )
, path : Maybe (List String)
, method : Maybe String
{ values : FormDecoder.FormData
, toMsg : Result Http.Error Url -> msg
}
| SubmitFetcher (Pages.Fetcher.Fetcher msg)
@ -66,19 +65,19 @@ map fn effect =
FetchRouteData fetchInfo ->
FetchRouteData
{ body = fetchInfo.body
, path = fetchInfo.path
{ data = fetchInfo.data
, toMsg = fetchInfo.toMsg >> fn
}
Submit fetchInfo ->
Submit
{ values = fetchInfo.values
, path = fetchInfo.path
, method = fetchInfo.method
, toMsg = fetchInfo.toMsg >> fn
}
SetField info ->
SetField info
SubmitFetcher fetcher ->
fetcher
|> Pages.Fetcher.map fn
@ -87,16 +86,12 @@ map fn effect =
perform :
{ fetchRouteData :
{ body : Maybe { contentType : String, body : String }
, path : Maybe String
{ data : Maybe FormDecoder.FormData
, toMsg : Result Http.Error Url -> pageMsg
}
-> Cmd msg
, submit :
{ values : List ( String, String )
, encType : Maybe String
, method : Maybe String
, path : Maybe String
{ values : FormDecoder.FormData
, toMsg : Result Http.Error Url -> pageMsg
}
-> Cmd msg
@ -105,6 +100,7 @@ perform :
-> Cmd msg
, fromPageMsg : pageMsg -> msg
, key : Browser.Navigation.Key
, setField : { formId : String, name : String, value : String } -> Cmd msg
}
-> Effect pageMsg
-> Cmd msg
@ -116,6 +112,9 @@ perform ({ fromPageMsg, key } as helpers) effect =
Cmd cmd ->
Cmd.map fromPageMsg cmd
SetField info ->
helpers.setField info
Batch list ->
Cmd.batch (List.map (perform helpers) list)
@ -128,19 +127,10 @@ perform ({ fromPageMsg, key } as helpers) effect =
FetchRouteData fetchInfo ->
helpers.fetchRouteData
{ body = fetchInfo.body
, path = fetchInfo.path
, toMsg = fetchInfo.toMsg
}
fetchInfo
Submit record ->
helpers.submit
{ values = record.values
, path = Nothing --fetchInfo.path
, method = record.method
, encType = Nothing -- TODO
, toMsg = record.toMsg
}
helpers.submit record
SubmitFetcher record ->
helpers.runFetcher record

View File

@ -2,6 +2,7 @@ module Route.Feed__ exposing (ActionData, Data, Model, Msg, route)
import DataSource exposing (DataSource)
import DataSource.Http
import DataSource.Port
import Effect exposing (Effect)
import ErrorPage exposing (ErrorPage)
import Head
@ -9,6 +10,8 @@ import Head.Seo as Seo
import Html exposing (Html)
import Html.Attributes as Attr
import Json.Decode exposing (Decoder)
import Json.Encode as Encode
import Pages.Msg
import Pages.PageUrl exposing (PageUrl)
import Pages.Url
import Path exposing (Path)
@ -83,7 +86,10 @@ pages =
type alias Data =
{ stories : List Item, currentPage : Int }
{ stories : List Item
, currentPage : Int
, hello : String
}
type alias ActionData =
@ -136,7 +142,11 @@ data routeParams =
DataSource.Http.get getStoriesUrl
(Story.decoder |> Json.Decode.list)
in
getStories |> DataSource.map (\stories -> Response.render { stories = stories, currentPage = currentPage })
DataSource.map3 Data
getStories
(DataSource.succeed currentPage)
(DataSource.Port.get "hello" (Encode.string "World") Json.Decode.string)
|> DataSource.map Response.render
)
@ -174,11 +184,12 @@ view :
-> Shared.Model
-> Model
-> StaticPayload Data ActionData RouteParams
-> View Msg
-> View (Pages.Msg.Msg Msg)
view maybeUrl sharedModel model static =
{ title = title static.routeParams
, body =
[ paginationView static.data.stories static.routeParams static.data.currentPage
, Html.div [] [ Html.text static.data.hello ]
, Html.main_
[ Attr.class "news-list"
]

View File

@ -11,6 +11,7 @@ import Html.Attributes as Attr
import Html.Keyed
import Json.Decode as Decode
import Json.Encode as Encode
import Pages.Msg
import Pages.PageUrl exposing (PageUrl)
import Pages.Url
import Path exposing (Path)
@ -131,7 +132,7 @@ view :
-> Shared.Model
-> Model
-> StaticPayload Data ActionData RouteParams
-> View Msg
-> View (Pages.Msg.Msg Msg)
view maybeUrl sharedModel model static =
{ title = static.data.story |> Tuple.first |> (\(Item common _) -> common.title)
, body =