Search code examples
elm

Display total of returned array in ELM


I'm new to ELM, and I'm extending a basic app that displays a presentation from a markdown document.

I'm changing the main.elm document of this glitch app to show a total of the available pages.

If I set the total when I get the response (in this block)

RcvSlides resp ->
    case resp of
        Ok slides ->
            ( { model
                | slides = Just <| Array.fromList <| String.split "^^^^" slides
              }
            , Cmd.none
            )

        Err _ ->
            ( { model | slides = Nothing }
            , Cmd.none
            )

I always get 0, if I do it in the NextSlide case, I can get the total, but not in the first page.

How can I solve this?


Solution

  • It's because you forgot to set total when you receive your slides. Here a working version of your glitch:

    RcvSlides resp ->
        case resp of
            Ok slides ->
                let
                    nextSlides = Array.fromList <| String.split "^^^^" slides
                    nextTotal = Array.length nextSlides
                    nextModel = { model
                        | slides = Just <| nextSlides
                        , total = nextTotal
                        }
                in
                ( nextModel, Cmd.none )
    
            Err _ ->
                ( { model | slides = Nothing }
                , Cmd.none
                )