elm-pages-v3-beta/examples/pokedex/app/Effect.elm

137 lines
3.1 KiB
Elm
Raw Normal View History

2022-03-29 01:11:07 +03:00
module Effect exposing (Effect(..), batch, fromCmd, map, none, perform)
import Browser.Navigation
import Bytes exposing (Bytes)
import Bytes.Decode
import FormDecoder
2022-03-29 01:11:07 +03:00
import Http
import Json.Decode as Decode
2022-05-11 02:29:28 +03:00
import Pages.Fetcher
2022-04-04 19:40:37 +03:00
import Url exposing (Url)
2022-03-29 01:11:07 +03:00
type Effect msg
= None
| Cmd (Cmd msg)
| Batch (List (Effect msg))
| GetStargazers (Result Http.Error Int -> msg)
2022-07-02 19:05:50 +03:00
| SetField { formId : String, name : String, value : String }
2022-04-05 00:45:14 +03:00
| FetchRouteData
2022-05-19 01:31:25 +03:00
{ data : Maybe FormDecoder.FormData
2022-04-04 19:40:37 +03:00
, toMsg : Result Http.Error Url -> msg
}
2022-04-27 19:05:48 +03:00
| Submit
{ values : FormDecoder.FormData
2022-04-27 19:05:48 +03:00
, toMsg : Result Http.Error Url -> msg
}
2022-05-11 02:29:28 +03:00
| SubmitFetcher (Pages.Fetcher.Fetcher msg)
2022-04-04 19:40:37 +03:00
type alias RequestInfo =
{ contentType : String
, body : String
}
2022-03-29 01:11:07 +03:00
none : Effect msg
none =
None
batch : List (Effect msg) -> Effect msg
batch =
Batch
fromCmd : Cmd msg -> Effect msg
fromCmd =
Cmd
map : (a -> b) -> Effect a -> Effect b
map fn effect =
case effect of
None ->
None
Cmd cmd ->
Cmd (Cmd.map fn cmd)
Batch list ->
Batch (List.map (map fn) list)
GetStargazers toMsg ->
GetStargazers (toMsg >> fn)
2022-04-05 00:45:14 +03:00
FetchRouteData fetchInfo ->
FetchRouteData
2022-05-19 01:31:25 +03:00
{ data = fetchInfo.data
2022-04-04 19:40:37 +03:00
, toMsg = fetchInfo.toMsg >> fn
}
2022-04-27 19:05:48 +03:00
Submit fetchInfo ->
Submit
{ values = fetchInfo.values
, toMsg = fetchInfo.toMsg >> fn
}
2022-07-02 19:05:50 +03:00
SetField info ->
SetField info
2022-05-11 02:29:28 +03:00
SubmitFetcher fetcher ->
fetcher
|> Pages.Fetcher.map fn
|> SubmitFetcher
2022-03-29 01:11:07 +03:00
2022-04-04 19:40:37 +03:00
perform :
{ fetchRouteData :
2022-05-19 01:31:25 +03:00
{ data : Maybe FormDecoder.FormData
2022-04-04 19:40:37 +03:00
, toMsg : Result Http.Error Url -> pageMsg
}
-> Cmd msg
2022-04-27 19:05:48 +03:00
, submit :
{ values : FormDecoder.FormData
2022-04-27 19:05:48 +03:00
, toMsg : Result Http.Error Url -> pageMsg
}
-> Cmd msg
, runFetcher :
2022-05-11 02:29:28 +03:00
Pages.Fetcher.Fetcher pageMsg
-> Cmd msg
2022-04-05 03:07:37 +03:00
, fromPageMsg : pageMsg -> msg
, key : Browser.Navigation.Key
2022-07-02 19:05:50 +03:00
, setField : { formId : String, name : String, value : String } -> Cmd msg
2022-04-04 19:40:37 +03:00
}
-> Effect pageMsg
-> Cmd msg
2022-04-05 03:07:37 +03:00
perform ({ fromPageMsg, key } as helpers) effect =
2022-03-29 01:11:07 +03:00
case effect of
None ->
Cmd.none
Cmd cmd ->
Cmd.map fromPageMsg cmd
2022-07-02 19:05:50 +03:00
SetField info ->
helpers.setField info
2022-03-29 01:11:07 +03:00
Batch list ->
2022-04-05 03:07:37 +03:00
Cmd.batch (List.map (perform helpers) list)
2022-03-29 01:11:07 +03:00
GetStargazers toMsg ->
Http.get
{ url =
"https://api.github.com/repos/dillonkearns/elm-pages"
, expect = Http.expectJson (toMsg >> fromPageMsg) (Decode.field "stargazers_count" Decode.int)
}
2022-04-04 19:40:37 +03:00
2022-04-05 00:45:14 +03:00
FetchRouteData fetchInfo ->
helpers.fetchRouteData
2022-05-19 01:31:25 +03:00
fetchInfo
2022-04-27 19:05:48 +03:00
Submit record ->
2022-05-19 01:31:25 +03:00
helpers.submit record
SubmitFetcher record ->
helpers.runFetcher record