Skip to content

Instantly share code, notes, and snippets.

@RyanCCollins
Created January 5, 2017 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RyanCCollins/956f6d4964ec4999b9aac35d67b22916 to your computer and use it in GitHub Desktop.
Save RyanCCollins/956f6d4964ec4999b9aac35d67b22916 to your computer and use it in GitHub Desktop.
Elm reducer / union type example
type alias Model =
{ error: Maybe String
, message: Maybe String
, isSubmitting: Bool
}
type alias Error =
{ message: String }
type Msg
= ClearAlerts
| SubmissionInitiation
| SubmissionSuccess
| SubmissionError Error
| SubmissionMessage String
initModel : (Model, Cmd Msg)
initModel =
{ error = Nothing
, message = Nothing
, isSubmitting = False
}
![]
-- Update (Note, I remove the Cmd ![] return value from each of these for brevity sake.
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
ClearAlerts ->
{ model | error = Nothing, message = Nothing }
SubmissionInitiation ->
{ model | isSubmitting = True }
SubmissionInitiation ->
{ model | isSubmitting = True }
SubmissionError err ->
{ model | error = err }
SubmissionMessage message ->
{ model | message = message }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment