bluedogtraining / bdt-elm / 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"

update : a -> Resettable a -> Resettable a

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 : Resettable a -> Resettable a

Reset to the initial value.

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

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

Getters

getInitialValue : Resettable a -> a

Get the initial value.

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

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

getValue : Resettable a -> a

Get the current value.

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

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

getIsChanged : Resettable a -> Basics.Bool

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