bigardone
bigardone

Reputation: 174

Error with Maybe and nested resources in Elm

I'm building my very first Elm SPA, and I'm organizing my components in different folders and modules. Everything worked fine until I changed the main model from this:

type alias Model =
{ contacts : Contacts.Model.Model
, contact : Contact.Model.Model
, route : Routing.Route
}

To this:

type alias Model =
{ contacts : Contacts.Model.Model
, contact : Maybe Contact.Model.Model
, route : Routing.Route
}

I've made all the necessary changes in the codebase for this to work, but I'm missing something I can't find because in the main Update module I'm constantly getting this compile error:

The type annotation for `update` does not match its definition. - The type annotation is saying:

    Msg
    -> { ..., contact : Maybe Contact.Model.Model }
    -> ( { contact : Maybe Contact.Model.Model
    , contacts : Contacts.Model.Model
    , route : Routing.Route
    }
    , Cmd Msg
    )

But I am inferring that the definition has this type:

    Msg
    -> { ...
    , contact :
          { birth_date : String
          , email : String
          , first_name : String
          , gender : Int
          , headline : String
          , id : Int
          , last_name : String
          , location : String
          , phone_number : String
          , picture : String
          }
    }
    -> ( { contact : Maybe Contact.Model.Model
    , contacts : Contacts.Model.Model
    , route : Routing.Route
    }
    , Cmd Msg
    )

It looks like I'm missing to pass the Maybe Model somewhere but I can't find it. This is how the update function looks like:

update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
    ContactsMsg subMsg ->
        let
            ( updatedContacts, cmd ) =
                Contacts.Update.update subMsg model.contacts
        in
            ( { model | contacts = updatedContacts, contact = Nothing }, Cmd.map ContactsMsg cmd )

    ContactMsg subMsg ->
        let
            ( updatedContact, cmd ) =
                Contact.Update.update subMsg model.contact
        in
            ( { model | contact = updatedContact }, Cmd.map ContactMsg cmd )

Here's the full repo, with the error: https://github.com/bigardone/phoenix-and-elm/tree/feature/routes-refactoring/web/elm

What am I doing wrong? Thank you very much in advance!

Upvotes: 0

Views: 228

Answers (1)

halfzebra
halfzebra

Reputation: 6807

The problem is caused by a type mismatch in Update.update function.

update : Msg -> Model -> ( Model, Cmd Msg )

You have to process the Maybe value in your Update.elm:

update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        ContactsMsg subMsg ->
            let
                ( updatedContacts, cmd ) =
                    Contacts.Update.update subMsg model.contacts
            in
                ( { model | contacts = updatedContacts, contact = Nothing }
                , Cmd.map ContactsMsg cmd
                )

        ContactMsg subMsg ->
            case model.contact of
                Just contact ->
                    let
                        ( updatedContact, cmd ) =
                            Contact.Update.update subMsg contact
                    in
                        ( { model | contact = updatedContact }
                        , Cmd.map ContactMsg cmd
                        )

                Nothing ->
                    ( model, Cmd.none )

Upvotes: 2

Related Questions