Skip to content

Instantly share code, notes, and snippets.

@Chadtech
Last active November 8, 2017 15:08
Show Gist options
  • Save Chadtech/479b11bf01d077d28e1c828523cd5737 to your computer and use it in GitHub Desktop.
Save Chadtech/479b11bf01d077d28e1c828523cd5737 to your computer and use it in GitHub Desktop.
type Update
= SmallUpdate
| BigUpdate
| EnormousUpdate
validate : Appointment -> Appointment -> List Update
validate originalModel newModel =
[ differentCustomer
, differentTime
, newService
]
|> check originalModel newModel
type alias Validation =
Appointment -> Appointment -> List Update
check : Appointment -> Appointment -> List (List Validation) -> List Update
check originalModel newModel validations =
List.map (\v -> v originalModel newModel) validations
|> List.concat
differentCustomer : Validation
differentCustomer originalModel newModel =
if originalModel.customer == newModel.customer then
[]
else
[ BigUpdate ]
-- etc..
type alias Model =
{ --..
, updates : List Update
}
-- Called after every significant update
checkForUpdates : Model -> Model
checkForUpdates model =
let
updates =
validate model.originalAppointment (toAppointment model)
in
if List.isEmpty updates then
{ model
| updates = []
, updateButtonDisabled = True
}
else
{ model
| updates = updates
, updateButtonDisabled = False
}
update msg model =
case msg of
-- ..
UpdateButtonClicked ->
model => updatesToCmds model.updates
updatesToCmds : List Update -> Cmd Msg
updatesToCmds =
List.map fromUpdate >> Cmd.batch
fromUpdate : Update -> Cmd Msg
fromUpdate update =
case update of
BigUpdate ->
Ports.send OneKindOfApiCall
SmallUpdate ->
Ports.send AnotherKindOfApiCall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment