Skip to content

Commit

Permalink
Use didTouch approach
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten authored and amitaibu committed Oct 31, 2023
1 parent 3ad20ef commit 872b3ae
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
28 changes: 22 additions & 6 deletions IHP/ModelSupport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -781,13 +781,8 @@ didChangeRecord record = isEmpty record.meta.touchedFields
--
-- > when (user |> didChange #profilePictureUrl) (setSuccessMessage "Your Profile Picture has been updated. It might take a few minutes until it shows up everywhere")
didChange :: forall fieldName fieldValue record. (KnownSymbol fieldName, HasField fieldName record fieldValue, HasField "meta" record MetaBag, Eq fieldValue, Typeable record) => Proxy fieldName -> record -> Bool
didChange field record = didTouchField && didChangeField
didChange field record = didTouchField field record && didChangeField
where
didTouchField :: Bool
didTouchField =
record.meta.touchedFields
|> includes (cs $! symbolVal field)

didChangeField :: Bool
didChangeField = originalFieldValue /= fieldValue

Expand All @@ -802,6 +797,27 @@ didChange field record = didTouchField && didChangeField
|> fromMaybe (error "didChange failed to retrieve originalDatabaseRecord")
|> getField @fieldName

-- | Returns 'True' if 'set' was called on that field
--
-- __Example:__ Returns 'False' for freshly fetched records
--
-- >>> let projectId = "227fbba3-0578-4eb8-807d-b9b692c3644f" :: Id Project
-- >>> project <- fetch projectId
-- >>> didTouchField #name project
-- False
--
-- __Example:__ Returns 'True' after setting a field
--
-- >>> let projectId = "227fbba3-0578-4eb8-807d-b9b692c3644f" :: Id Project
-- >>> project <- fetch projectId
-- >>> project |> set #name project.name |> didChange #name
-- True
--
didTouchField :: forall fieldName fieldValue record. (KnownSymbol fieldName, HasField fieldName record fieldValue, HasField "meta" record MetaBag, Eq fieldValue, Typeable record) => Proxy fieldName -> record -> Bool
didTouchField field record =
record.meta.touchedFields
|> includes (cs $! symbolVal field)

-- | Represents fields that have a default value in an SQL schema
--
-- The 'Default' constructor represents the default value from the schema,
Expand Down
14 changes: 12 additions & 2 deletions IHP/View/Form.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import IHP.ViewSupport
import qualified Text.Blaze.Html5 as Html5
import IHP.HSX.ToHtml
import GHC.Types
import IHP.ModelSupport (getModelName, inputValue, isNew, Id', InputValue)
import IHP.ModelSupport (getModelName, inputValue, isNew, Id', InputValue, didTouchField)
import IHP.HSX.QQ (hsx)
import IHP.View.Types
import IHP.View.Classes ()
Expand Down Expand Up @@ -730,6 +730,8 @@ selectField :: forall fieldName model item.
, KnownSymbol (GetModelName model)
, CanSelect item
, InputValue (SelectValue item)
, Typeable model
, Eq (SelectValue item)
) => Proxy fieldName -> [item] -> FormField
selectField field items = FormField
{ fieldType =
Expand All @@ -740,7 +742,9 @@ selectField field items = FormField
SelectInput (map itemToTuple items)
, fieldName = cs fieldName
, fieldLabel = removeIdSuffix $ fieldNameToFieldLabel (cs fieldName)
, fieldValue = inputValue (getField @fieldName model :: SelectValue item)
, fieldValue = if IHP.ModelSupport.didTouchField field model
then inputValue (getField @fieldName model :: SelectValue item)
else ""
, fieldInputId = cs (lcfirst (getModelName @model) <> "_" <> cs fieldName)
, validatorResult = getValidationViolation field model
, fieldClass = ""
Expand Down Expand Up @@ -783,6 +787,8 @@ selectFieldEmptyFieldValueWhenIsNew :: forall fieldName model item.
, KnownSymbol (GetModelName model)
, CanSelect item
, InputValue (SelectValue item)
, Typeable model
, Eq (SelectValue item)
) => Proxy fieldName -> [item] -> FormField
selectFieldEmptyFieldValueWhenIsNew field items = (selectField field items)
{ fieldValue = if isNew model && isEmpty (paramList @Text (cs fieldName))
Expand Down Expand Up @@ -844,6 +850,8 @@ radioField :: forall fieldName model item.
, KnownSymbol (GetModelName model)
, CanSelect item
, InputValue (SelectValue item)
, Typeable model
, Eq (SelectValue item)
) => Proxy fieldName -> [item] -> FormField
radioField field items = (selectField field items)
{ fieldType =
Expand All @@ -869,6 +877,8 @@ radioFieldEmptyFieldValueWhenIsNew :: forall fieldName model item.
, KnownSymbol (GetModelName model)
, CanSelect item
, InputValue (SelectValue item)
, Typeable model
, Eq (SelectValue item)
) => Proxy fieldName -> [item] -> FormField
radioFieldEmptyFieldValueWhenIsNew field items = (radioField field items)
{ fieldValue = selectField.fieldValue
Expand Down

0 comments on commit 872b3ae

Please sign in to comment.