bluedogtraining/bdt-elm/1.0.0

NOTE: this package is not compatible with Elm 0.19.1

Resettable

This module is useful if you want to track changes to a value.

Definition

type Resettable a

Represent values that may be original or updated. It can be useful if you have a a save button that should be disabled if nothing on a form changed.

type alias MyProfileForm =
    { firstName : Resettable String
    , email : Resettable String
    }


form =
    { firstName = Resettable.init "John Doe"
    , email = Resettable.init ""
    }


shouldEnableSaveButton : Form -> Bool
shouldEnableSaveButton form =
    Resettable.isChanged form.email || Resettable.isChanged form.password

Initialise and update

init : a -> Resettable a

Initialise a value.

init "Bob"     -- Original "Bob"
update : a -> Resettable a -> Resettable a

Updates a Resettable value.

-- update to new value
update "Joshua" (Original "Josh")       -- Updated "Josh" "Joshua"
update "Bob" (Updated "Josh" "Joshua")  -- Updated "Josh" "Bob"


-- if the new value is the original value or a changed value is set to it's original value, keep the original value
update "Josh" (Original "Josh")         -- Original "Bob"
update "Josh" (Updated "Josh" "Joshua") -- Original "Josh"
reset : Resettable a -> Resettable a

Reset to the original value.

reset (Original "Josh")         -- Original "Josh"
reset (Updated "Josh" "Joshua") -- Original "Josh"

Getters

isChanged : Resettable a -> Bool

Whether a value changed.

isChanged (Original "Josh")         -- False
isChanged (Updated "Josh" "Joshua") -- True


when checking a lot of values, it may be helpful to put them in a list:


-- if they all have the same type:
List.any isChanged [ Original "Josh", Updated "Josh" "Joshua" ]   -- True


-- if they have different types
List.any ((==) True) [ isChanged (Original "Josh"), isChanged (Original 12) ]   -- False
getValue : Resettable a -> a

Get the current value.

getValue (Original "Josh")         -- "Josh"
getValue (Updated "Josh" "Joshua") -- "Joshua"
getOriginalValue : Resettable a -> a

Get the original value.

getOriginalValue (Original "Josh")         -- "Josh"
getOriginalValue (Updated "Josh" "Joshua") -- "Josh"