Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Conditionally change record in update

I would like to have some logic worked upon the Msg and, depending on the result, update the view in a different ways.

I’m flipping some cards, and I want to test two of the selected ones. Then, accept them as a pair or discard and try again.

update : Msg -> Model -> Model
update msg model =
    case msg of
        ClickedCard data ->
            { model
                | activeCard = data.id
                , (if List.lenght selectedCards < 2 then
                      selectedCards = data.id :: model.selectedCards
                  else if (List.take 1 model.selectedCards) == (List.drop 1 model.selectedCards) then
                           completedPairs = ( List.take 1 model.selectedCards , List.drop 1 model.selectedCards ):: model.completedPairs
                          else
                              selectedCards = List.drop 2 model.selectedCards)
            }

        _ ->
            model

But, seems like I can’t insert the logic there. Where should I put it, instead?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

-- PROBLEM IN RECORD ------------------------------------------ src/Flipping.elm

I am partway through parsing a record, but I got stuck here:

126|             { model
127|                 | activeCard = data.id
128|                 , (if List.lenght selectedCards < 2 then
                       ^
I was expecting to see another record field defined next, so I am looking for a
name like userName or plantHeight.

>Solution :

The record update syntax doesn’t work like that.

You can do the following.

update : Msg -> Model -> Model
update msg model =
    case msg of
        ClickedCard data ->
            let 
               newModel = { model | activeCard = data.id }
            in
              if List.length selectedCards < 2 then
               {newModel | selectedCards = data.id :: model.selectedCards}
              else if (List.take 1 model.selectedCards) == (List.drop 1 model.selectedCards) then
               {newModel | completedPairs = ( List.take 1 model.selectedCards , List.drop 1 model.selectedCards ):: model.completedPairs}
              else
               {newModel | selectedCards = List.drop 2 model.selectedCards)}

        _ ->
            model
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading