krisajenkins / remotedata

Tools for fetching data from remote sources (incl. HTTP).
MIT License
249 stars 23 forks source link

Setting fields on a Model with a WebData type #13

Closed skinnyjames closed 6 years ago

skinnyjames commented 7 years ago

Apologies if this doesn't belong.

I'm confused as to how to set onInput attributes on a model when it has the type WebData - before it gets sent to the server. Here's an example using elm-mdl. I'm not sure how to set an empty record on the initialModel with a WebData type, and when I try to update the model attributes, it's has a type WebData, so it throws an error.

I'm new to Elm, so I think I'm missing something obvious.


type alias Install =
    { pgUser : String }

install = Install ""

type alias Model =
  { mdl : Material.Model
  , install : WebData Install }

initialModel : Model
initialModel =
  { mdl = Material.model
--, install = RemoteData.succeed install
  , install = RemoteData.NotAsked }

type Msg =  ChangePgUser String

installForm : Model -> Html Msg
installForm model = form [ Attr.id "installation__body"
                   , Attr.class "install__form"
                   , Attr.action "/install"
                   , Attr.method "POST" ] [ installHeader "PostgreSQL Configuration"
                                          , Textfield.render Mdl [0] model.mdl
                                            [ Textfield.label "PostgreSQL User"
                                            , Textfield.floatingLabel
                                            , Textfield.text_
                                            , Options.id "pg-user"
                                            , Options.attribute  <| (Attr.attribute "autocomplete" "new-user")
                                            , Options.onInput ChangePgUser
                                            ][]

update : Msg -> Model -> ( Model, Cmd Msg )
update msg model  =
  case msg of
    ChangePgUser username ->
      let
       -- What to do here?
        oInstall =  model.install
        nInstall = { oInstall | pgUser = username }
      in
        ({ model | install = nInstall }, Cmd.none)

And the error (handling a WebData type)

`oInstall` is being used in an unexpected way.

nInstall = { oInstall | pgUser = username }

Based on its definition, `oInstall` has this type:

    WebData Install

But you are trying to use it as: 

    { b | pgUser : a }
krisajenkins commented 7 years ago

Hey @skinnyjames, try something like this:

update : Msg -> Model -> ( Model, Cmd Msg )
update msg model  =
  case msg of
    ChangePgUser username ->
      let
       -- What to do here?
        oInstall =  model.install
        nInstall = RemoteData.Success { pgUser = username }
      in
        ({ model | install = nInstall }, Cmd.none)