How can i update two fields in model at once? Now i have request that return me an alias with {price: Float, productId: Int} I need to update two fields in model like model.price and model.productId
I looking for something like that, but its not work (ofc)
case maybeProduct of
Just product ->
( { model | price = product.price &&
model | productId = product.productId}
, Cmd.none
)
Nothing ->
( model
, Cmd.none
)
I found some info where advice that I can create two functions (Model -> Product -> Model) and do something like:
setPrice : Model -> Product -> Model
setPrice model product =
{ model | price = product.price }
setProductId : Model -> Product -> Model
setProductId model product =
{ model | companyId = product.productId }
Just product ->
let
newModel =
product
|> setPrice model
|> setProductId model
in
( newModel
, Cmd.none
)
Nothing ->
( model
, Cmd.none
)
but something dont work. It looks like product dont passing in each function
i recieve
The argument is:
Model
But (|>) is piping it a function that expects:
{ companyId : Int, price : Float }
Where i mistake? Or maybe there is different way to update two fields in model?
Do something like this (this excludes commands by the way - you can add that in to suit your requirements):
{ model | price = new_price, productId = newProductId}
If you want to add Commands then you can do this:
({ model | price = new_price, productId = newProductId}, nameOfYourCommand)
Here is a link that @JackLeow very kindly posted: https://elm-lang.org/docs/records#updating-records
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With