docspell/modules/webapp/src/main/elm/Comp/ContactField.elm

141 lines
3.8 KiB
Elm
Raw Normal View History

2019-12-29 20:55:12 +00:00
module Comp.ContactField exposing
( Model
, Msg(..)
, emptyModel
, getContacts
, update
, view
)
import Api.Model.Contact exposing (Contact)
import Comp.Dropdown
2019-12-29 20:55:12 +00:00
import Data.ContactType exposing (ContactType)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (onClick, onInput)
type alias Model =
2019-12-29 20:55:12 +00:00
{ items : List Contact
, kind : Comp.Dropdown.Model ContactType
, value : String
}
2019-12-29 20:55:12 +00:00
emptyModel : Model
emptyModel =
{ items = []
2019-12-29 20:55:12 +00:00
, kind =
Comp.Dropdown.makeSingleList
{ makeOption =
\ct ->
{ value = Data.ContactType.toString ct
, text = Data.ContactType.toString ct
}
, placeholder = ""
, options = Data.ContactType.all
, selected = List.head Data.ContactType.all
}
, value = ""
}
2019-12-29 20:55:12 +00:00
getContacts : Model -> List Contact
getContacts model =
List.filter (\c -> c.value /= "") model.items
2019-12-29 20:55:12 +00:00
type Msg
= SetValue String
| TypeMsg (Comp.Dropdown.Msg ContactType)
| AddContact
| Select Contact
| SetItems (List Contact)
2019-12-29 20:55:12 +00:00
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
SetItems contacts ->
2019-12-29 20:55:12 +00:00
( { model | items = contacts, value = "" }, Cmd.none )
SetValue v ->
2019-12-29 20:55:12 +00:00
( { model | value = v }, Cmd.none )
TypeMsg m ->
let
2019-12-29 20:55:12 +00:00
( m1, c1 ) =
Comp.Dropdown.update m model.kind
in
2019-12-29 20:55:12 +00:00
( { model | kind = m1 }, Cmd.map TypeMsg c1 )
AddContact ->
2019-12-29 20:55:12 +00:00
if model.value == "" then
( model, Cmd.none )
else
let
2019-12-29 20:55:12 +00:00
kind =
Comp.Dropdown.getSelected model.kind
|> List.head
|> Maybe.map Data.ContactType.toString
|> Maybe.withDefault ""
in
2019-12-29 20:55:12 +00:00
( { model | items = Contact "" model.value kind :: model.items, value = "" }, Cmd.none )
Select contact ->
let
2019-12-29 20:55:12 +00:00
newItems =
List.filter (\c -> c /= contact) model.items
( m1, c1 ) =
Data.ContactType.fromString contact.kind
|> Maybe.map (\ct -> update (TypeMsg (Comp.Dropdown.SetSelection [ ct ])) model)
|> Maybe.withDefault ( model, Cmd.none )
in
2019-12-29 20:55:12 +00:00
( { m1 | value = contact.value, items = newItems }, c1 )
2019-12-29 20:55:12 +00:00
view : Model -> Html Msg
view model =
div []
2019-12-29 20:55:12 +00:00
[ div [ class "fields" ]
[ div [ class "four wide field" ]
[ Html.map TypeMsg (Comp.Dropdown.view model.kind)
]
, div [ class "twelve wide field" ]
[ div [ class "ui action input" ]
[ input
[ type_ "text"
, onInput SetValue
, value model.value
]
2019-12-29 20:55:12 +00:00
[]
, a [ class "ui button", onClick AddContact, href "" ]
[ text "Add"
]
]
]
]
, div
[ classList
[ ( "field", True )
, ( "invisible", List.isEmpty model.items )
]
]
[ div [ class "ui vertical secondary fluid menu" ]
(List.map renderItem model.items)
]
]
2019-12-29 20:55:12 +00:00
renderItem : Contact -> Html Msg
renderItem contact =
2019-12-29 20:55:12 +00:00
div [ class "link item", onClick (Select contact) ]
[ i [ class "delete icon" ] []
, div [ class "ui blue label" ]
[ text contact.kind
]
2019-12-29 20:55:12 +00:00
, text contact.value
]