Add Effect module to docs site.

This commit is contained in:
Dillon Kearns 2022-03-21 13:12:36 -07:00
parent 979be6f3e4
commit e6fb1047ec
2 changed files with 58 additions and 6 deletions

View File

@ -0,0 +1,51 @@
module Effect exposing (Effect(..), batch, fromCmd, map, none, perform)
import Http
import Json.Decode as Decode
type Effect msg
= None
| Cmd (Cmd msg)
| Batch (List (Effect msg))
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)
perform : (pageMsg -> msg) -> Effect pageMsg -> Cmd msg
perform fromPageMsg effect =
case effect of
None ->
Cmd.none
Cmd cmd ->
Cmd.map fromPageMsg cmd
Batch list ->
Cmd.batch (List.map (perform fromPageMsg) list)

View File

@ -3,6 +3,7 @@ module Shared exposing (Data, Model, Msg, template)
import Browser.Navigation
import DataSource
import DocsSection
import Effect exposing (Effect)
import Html exposing (Html)
import Html.Styled
import Pages.Flags
@ -60,27 +61,27 @@ init :
, metadata : route
, pageUrl : Maybe PageUrl
}
-> ( Model, Cmd Msg )
-> ( Model, Effect Msg )
init navigationKey flags maybePagePath =
( { showMobileMenu = False
, counter = 0
, navigationKey = navigationKey
}
, Cmd.none
, Effect.none
)
update : Msg -> Model -> ( Model, Cmd Msg )
update : Msg -> Model -> ( Model, Effect Msg )
update msg model =
case msg of
OnPageChange _ ->
( { model | showMobileMenu = False }, Cmd.none )
( { model | showMobileMenu = False }, Effect.none )
ToggleMobileMenu ->
( { model | showMobileMenu = not model.showMobileMenu }, Cmd.none )
( { model | showMobileMenu = not model.showMobileMenu }, Effect.none )
IncrementFromChild ->
( { model | counter = model.counter + 1 }, Cmd.none )
( { model | counter = model.counter + 1 }, Effect.none )
subscriptions : Path -> Model -> Sub Msg