bluedogtraining/bdt-elm/27.0.8

NOTE: the latest version is 27.0.14

Resettable

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

Definition

type Resettable a

Represent values that may be initial 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.getIsChanged form.email || Resettable.getIsChanged form.password

Initialise and update

init : a -> Resettable a

Initialise a value.

init "Bob" -- Initial "Bob"

Updates a Resettable value.

-- update to new value
update "Joshua" (Initial "Josh") -- Updated "Josh" "Joshua"


update "Bob" (Updated "Josh" "Joshua") -- Updated "Josh" "Bob"




-- if the new value is the initial value or a changed value is set to it's initial value, keep the initial value
update "Josh" (Initial "Josh") -- Initial "Bob"


update "Josh" (Updated "Josh" "Joshua") -- Initial "Josh"

Reset to the initial value.

reset (Initial "Josh") -- Initial "Josh"


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

Getters

Get the initial value.

getInitialValue (Initial "Josh") -- "Josh"


getInitialValue (Updated "Josh" "Joshua") -- "Josh"

Get the current value.

getValue (Initial "Josh") -- "Josh"


getValue (Updated "Josh" "Joshua") -- "Joshua"

Whether a value changed.

getIsChanged (Initial "Josh")         -- False
getIsChanged (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 getIsChanged [ Initial "Josh", Updated "Josh" "Joshua" ]   -- True


-- if they have different types
List.any ((==) True) [ getIsChanged (Initial "Josh"), getIsChanged (Initial 12) ]   -- False