Update hackernews demo site.

This commit is contained in:
Dillon Kearns 2022-05-11 14:50:58 -07:00
parent 3099d12593
commit cbb422e52b
2 changed files with 44 additions and 0 deletions

View File

@ -5,6 +5,7 @@
"requires": true,
"packages": {
"": {
"name": "process-url",
"version": "1.0.0",
"license": "ISC",
"dependencies": {

View File

@ -2,6 +2,7 @@ module Effect exposing (Effect(..), batch, fromCmd, map, none, perform)
import Browser.Navigation
import Http
import Json.Decode as Decode
import Url exposing (Url)
@ -9,11 +10,18 @@ type Effect msg
= None
| Cmd (Cmd msg)
| Batch (List (Effect msg))
| GetStargazers (Result Http.Error Int -> msg)
| FetchRouteData
{ body : Maybe { contentType : String, body : String }
, path : Maybe String
, toMsg : Result Http.Error Url -> msg
}
| Submit
{ values : List ( String, String )
, path : Maybe (List String)
, method : Maybe String
, toMsg : Result Http.Error Url -> msg
}
type alias RequestInfo =
@ -49,6 +57,9 @@ map fn effect =
Batch list ->
Batch (List.map (map fn) list)
GetStargazers toMsg ->
GetStargazers (toMsg >> fn)
FetchRouteData fetchInfo ->
FetchRouteData
{ body = fetchInfo.body
@ -56,6 +67,14 @@ map fn effect =
, toMsg = fetchInfo.toMsg >> fn
}
Submit fetchInfo ->
Submit
{ values = fetchInfo.values
, path = fetchInfo.path
, method = fetchInfo.method
, toMsg = fetchInfo.toMsg >> fn
}
perform :
{ fetchRouteData :
@ -64,6 +83,14 @@ perform :
, toMsg : Result Http.Error Url -> pageMsg
}
-> Cmd msg
, submit :
{ values : List ( String, String )
, encType : Maybe String
, method : Maybe String
, path : Maybe String
, toMsg : Result Http.Error Url -> pageMsg
}
-> Cmd msg
, fromPageMsg : pageMsg -> msg
, key : Browser.Navigation.Key
}
@ -80,9 +107,25 @@ perform ({ fromPageMsg, key } as helpers) effect =
Batch list ->
Cmd.batch (List.map (perform helpers) 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)
}
FetchRouteData fetchInfo ->
helpers.fetchRouteData
{ body = fetchInfo.body
, path = fetchInfo.path
, toMsg = fetchInfo.toMsg
}
Submit record ->
helpers.submit
{ values = record.values
, path = Nothing --fetchInfo.path
, method = record.method
, encType = Nothing -- TODO
, toMsg = record.toMsg
}