From a6dd71af9f11959c9cde865650a431712505f0a6 Mon Sep 17 00:00:00 2001 From: Eike Kettner Date: Mon, 30 Nov 2020 22:07:16 +0100 Subject: [PATCH] Set an organization to a person in webapp --- .../webapp/src/main/elm/Comp/DetailEdit.elm | 60 +++++++++++-- modules/webapp/src/main/elm/Comp/Dropdown.elm | 13 +++ .../src/main/elm/Comp/ItemDetail/Update.elm | 40 +++++---- .../src/main/elm/Comp/ItemDetail/View.elm | 1 - .../webapp/src/main/elm/Comp/PersonForm.elm | 56 ++++++++++-- .../webapp/src/main/elm/Comp/PersonManage.elm | 88 ++++++++++++++----- .../webapp/src/main/elm/Comp/PersonTable.elm | 14 ++- .../webapp/src/main/elm/Comp/SearchMenu.elm | 8 +- 8 files changed, 215 insertions(+), 65 deletions(-) diff --git a/modules/webapp/src/main/elm/Comp/DetailEdit.elm b/modules/webapp/src/main/elm/Comp/DetailEdit.elm index 3e84ab4a..e666482d 100644 --- a/modules/webapp/src/main/elm/Comp/DetailEdit.elm +++ b/modules/webapp/src/main/elm/Comp/DetailEdit.elm @@ -30,6 +30,7 @@ import Api.Model.Equipment exposing (Equipment) import Api.Model.NewCustomField exposing (NewCustomField) import Api.Model.Organization exposing (Organization) import Api.Model.Person exposing (Person) +import Api.Model.ReferenceList exposing (ReferenceList) import Api.Model.Tag exposing (Tag) import Comp.CustomFieldForm import Comp.EquipmentForm @@ -134,7 +135,10 @@ editPerson flags persId pm = , loading = True , result = Nothing } - , Api.getPersonFull persId flags GetPersonResp + , Cmd.batch + [ Api.getPersonFull persId flags GetPersonResp + , Api.getOrgLight flags GetOrgsResp + ] ) @@ -150,14 +154,18 @@ editEquip flags equipId em = ) -initCorrPerson : String -> Comp.PersonForm.Model -> Model -initCorrPerson itemId pm = - init itemId (PMR pm) +initCorrPerson : Flags -> String -> Comp.PersonForm.Model -> ( Model, Cmd Msg ) +initCorrPerson flags itemId pm = + ( init itemId (PMR pm) + , Api.getOrgLight flags GetOrgsResp + ) -initConcPerson : String -> Comp.PersonForm.Model -> Model -initConcPerson itemId pm = - init itemId (PMC pm) +initConcPerson : Flags -> String -> Comp.PersonForm.Model -> ( Model, Cmd Msg ) +initConcPerson flags itemId pm = + ( init itemId (PMC pm) + , Api.getOrgLight flags GetOrgsResp + ) initTag : String -> Comp.TagForm.Model -> Model @@ -198,6 +206,7 @@ type Msg | GetOrgResp (Result Http.Error Organization) | GetPersonResp (Result Http.Error Person) | GetEquipResp (Result Http.Error Equipment) + | GetOrgsResp (Result Http.Error ReferenceList) type Value @@ -304,6 +313,43 @@ update flags msg model = , Nothing ) + GetOrgsResp (Ok list) -> + case model.form of + PMC pm -> + let + ( p_, c_ ) = + Comp.PersonForm.update flags (Comp.PersonForm.SetOrgs list.items) pm + in + ( { model + | loading = False + , form = PMC p_ + } + , Cmd.map PersonMsg c_ + , Nothing + ) + + PMR pm -> + let + ( p_, c_ ) = + Comp.PersonForm.update flags (Comp.PersonForm.SetOrgs list.items) pm + in + ( { model + | loading = False + , form = PMR p_ + } + , Cmd.map PersonMsg c_ + , Nothing + ) + + _ -> + ( { model | loading = False }, Cmd.none, Nothing ) + + GetOrgsResp (Err err) -> + ( { model | loading = False, result = Just (BasicResult False (Util.Http.errorToString err)) } + , Cmd.none + , Nothing + ) + GetEquipResp (Ok equip) -> case model.form of EM em -> diff --git a/modules/webapp/src/main/elm/Comp/Dropdown.elm b/modules/webapp/src/main/elm/Comp/Dropdown.elm index 8798abbe..936c0a55 100644 --- a/modules/webapp/src/main/elm/Comp/Dropdown.elm +++ b/modules/webapp/src/main/elm/Comp/Dropdown.elm @@ -10,6 +10,7 @@ module Comp.Dropdown exposing , makeSingleList , mkOption , notSelected + , orgDropdown , setMkOption , update , view @@ -19,6 +20,7 @@ module Comp.Dropdown exposing {-| This needs to be rewritten from scratch! -} +import Api.Model.IdName exposing (IdName) import Data.UiSettings exposing (UiSettings) import Html exposing (..) import Html.Attributes exposing (..) @@ -28,6 +30,17 @@ import Util.Html exposing (onKeyUp) import Util.List +orgDropdown : Model IdName +orgDropdown = + makeModel + { multiple = False + , searchable = \n -> n > 0 + , makeOption = \e -> { value = e.id, text = e.name, additional = "" } + , labelColor = \_ -> \_ -> "" + , placeholder = "Choose an organization" + } + + type alias Option = { value : String , text : String diff --git a/modules/webapp/src/main/elm/Comp/ItemDetail/Update.elm b/modules/webapp/src/main/elm/Comp/ItemDetail/Update.elm index 6d0341c9..0dcb4dd8 100644 --- a/modules/webapp/src/main/elm/Comp/ItemDetail/Update.elm +++ b/modules/webapp/src/main/elm/Comp/ItemDetail/Update.elm @@ -1113,26 +1113,30 @@ update key flags inav settings msg model = resultModel model StartCorrPersonModal -> - resultModel - { model - | modalEdit = - Just - (Comp.DetailEdit.initCorrPerson - model.item.id - Comp.PersonForm.emptyModel - ) - } + let + ( pm, pc ) = + Comp.DetailEdit.initCorrPerson + flags + model.item.id + Comp.PersonForm.emptyModel + in + resultModelCmd + ( { model | modalEdit = Just pm } + , Cmd.map ModalEditMsg pc + ) StartConcPersonModal -> - resultModel - { model - | modalEdit = - Just - (Comp.DetailEdit.initConcPerson - model.item.id - Comp.PersonForm.emptyModel - ) - } + let + ( p, c ) = + Comp.DetailEdit.initConcPerson + flags + model.item.id + Comp.PersonForm.emptyModel + in + resultModelCmd + ( { model | modalEdit = Just p } + , Cmd.map ModalEditMsg c + ) StartEditPersonModal pm -> let diff --git a/modules/webapp/src/main/elm/Comp/ItemDetail/View.elm b/modules/webapp/src/main/elm/Comp/ItemDetail/View.elm index 3f1dc968..f8a8f370 100644 --- a/modules/webapp/src/main/elm/Comp/ItemDetail/View.elm +++ b/modules/webapp/src/main/elm/Comp/ItemDetail/View.elm @@ -17,7 +17,6 @@ import Comp.LinkTarget import Comp.MarkdownInput import Comp.SentMails import Comp.YesNoDimmer -import Data.CustomFieldType import Data.Direction import Data.Fields import Data.Icons as Icons diff --git a/modules/webapp/src/main/elm/Comp/PersonForm.elm b/modules/webapp/src/main/elm/Comp/PersonForm.elm index 2fba1b03..c41c8721 100644 --- a/modules/webapp/src/main/elm/Comp/PersonForm.elm +++ b/modules/webapp/src/main/elm/Comp/PersonForm.elm @@ -9,9 +9,11 @@ module Comp.PersonForm exposing , view1 ) +import Api.Model.IdName exposing (IdName) import Api.Model.Person exposing (Person) import Comp.AddressForm import Comp.ContactField +import Comp.Dropdown import Data.Flags exposing (Flags) import Data.UiSettings exposing (UiSettings) import Html exposing (..) @@ -20,23 +22,25 @@ import Html.Events exposing (onCheck, onInput) type alias Model = - { org : Person + { person : Person , name : String , addressModel : Comp.AddressForm.Model , contactModel : Comp.ContactField.Model , notes : Maybe String , concerning : Bool + , orgModel : Comp.Dropdown.Model IdName } emptyModel : Model emptyModel = - { org = Api.Model.Person.empty + { person = Api.Model.Person.empty , name = "" , addressModel = Comp.AddressForm.emptyModel , contactModel = Comp.ContactField.emptyModel , notes = Nothing , concerning = False + , orgModel = Comp.Dropdown.orgDropdown } @@ -48,15 +52,20 @@ isValid model = getPerson : Model -> Person getPerson model = let - o = - model.org + person = + model.person + + org = + Comp.Dropdown.getSelected model.orgModel + |> List.head in - { o + { person | name = model.name , address = Comp.AddressForm.getAddress model.addressModel , contacts = Comp.ContactField.getContacts model.contactModel , notes = model.notes , concerning = model.concerning + , organization = org } @@ -67,6 +76,8 @@ type Msg | ContactMsg Comp.ContactField.Msg | SetNotes String | SetConcerning Bool + | SetOrgs (List IdName) + | OrgDropdownMsg (Comp.Dropdown.Msg IdName) update : Flags -> Msg -> Model -> ( Model, Cmd Msg ) @@ -79,16 +90,32 @@ update flags msg model = ( m2, c2 ) = update flags (ContactMsg (Comp.ContactField.SetItems t.contacts)) m1 + + ( m3, c3 ) = + update flags + (OrgDropdownMsg + (Comp.Dropdown.SetSelection + (List.filterMap identity [ t.organization ]) + ) + ) + m2 in - ( { m2 - | org = t + ( { m3 + | person = t , name = t.name , notes = t.notes , concerning = t.concerning } - , Cmd.batch [ c1, c2 ] + , Cmd.batch [ c1, c2, c3 ] ) + SetOrgs orgs -> + let + opts = + Comp.Dropdown.SetOptions orgs + in + update flags (OrgDropdownMsg opts) model + AddressMsg am -> let ( m1, c1 ) = @@ -121,6 +148,15 @@ update flags msg model = SetConcerning _ -> ( { model | concerning = not model.concerning }, Cmd.none ) + OrgDropdownMsg lm -> + let + ( dm_, cmd_ ) = + Comp.Dropdown.update lm model.orgModel + in + ( { model | orgModel = dm_ } + , Cmd.map OrgDropdownMsg cmd_ + ) + view : UiSettings -> Model -> Html Msg view settings model = @@ -156,6 +192,10 @@ view1 settings compact model = , label [] [ text "Use for concerning person suggestion only" ] ] ] + , div [ class "field" ] + [ label [] [ text "Organization" ] + , Html.map OrgDropdownMsg (Comp.Dropdown.view settings model.orgModel) + ] , h3 [ class "ui dividing header" ] [ text "Address" ] diff --git a/modules/webapp/src/main/elm/Comp/PersonManage.elm b/modules/webapp/src/main/elm/Comp/PersonManage.elm index beb3dd52..14241bf0 100644 --- a/modules/webapp/src/main/elm/Comp/PersonManage.elm +++ b/modules/webapp/src/main/elm/Comp/PersonManage.elm @@ -10,6 +10,7 @@ import Api import Api.Model.BasicResult exposing (BasicResult) import Api.Model.Person import Api.Model.PersonList exposing (PersonList) +import Api.Model.ReferenceList exposing (ReferenceList) import Comp.PersonForm import Comp.PersonTable import Comp.YesNoDimmer @@ -28,7 +29,7 @@ type alias Model = , formModel : Comp.PersonForm.Model , viewMode : ViewMode , formError : Maybe String - , loading : Bool + , loading : Int , deleteConfirm : Comp.YesNoDimmer.Model , query : String } @@ -45,7 +46,7 @@ emptyModel = , formModel = Comp.PersonForm.emptyModel , viewMode = Table , formError = Nothing - , loading = False + , loading = 0 , deleteConfirm = Comp.YesNoDimmer.emptyModel , query = "" } @@ -63,6 +64,7 @@ type Msg | YesNoMsg Comp.YesNoDimmer.Msg | RequestDelete | SetQuery String + | GetOrgResp (Result Http.Error ReferenceList) update : Flags -> Msg -> Model -> ( Model, Cmd Msg ) @@ -105,17 +107,35 @@ update flags msg model = ( { model | formModel = m2 }, Cmd.map FormMsg c2 ) LoadPersons -> - ( { model | loading = True }, Api.getPersons flags model.query PersonResp ) + ( { model | loading = model.loading + 2 } + , Cmd.batch + [ Api.getPersons flags model.query PersonResp + , Api.getOrgLight flags GetOrgResp + ] + ) - PersonResp (Ok orgs) -> + PersonResp (Ok persons) -> let m2 = - { model | viewMode = Table, loading = False } + { model + | viewMode = Table + , loading = Basics.max 0 (model.loading - 1) + } in - update flags (TableMsg (Comp.PersonTable.SetPersons orgs.items)) m2 + update flags (TableMsg (Comp.PersonTable.SetPersons persons.items)) m2 PersonResp (Err _) -> - ( { model | loading = False }, Cmd.none ) + ( { model | loading = Basics.max 0 (model.loading - 1) }, Cmd.none ) + + GetOrgResp (Ok list) -> + let + m2 = + { model | loading = Basics.max 0 (model.loading - 1) } + in + update flags (FormMsg (Comp.PersonForm.SetOrgs list.items)) m2 + + GetOrgResp (Err _) -> + ( { model | loading = Basics.max 0 (model.loading - 1) }, Cmd.none ) SetViewMode m -> let @@ -148,7 +168,9 @@ update flags msg model = Comp.PersonForm.isValid model.formModel in if valid then - ( { model | loading = True }, Api.postPerson flags person SubmitResp ) + ( { model | loading = model.loading + 1 } + , Api.postPerson flags person SubmitResp + ) else ( { model | formError = Just "Please correct the errors in the form." }, Cmd.none ) @@ -162,13 +184,23 @@ update flags msg model = ( m3, c3 ) = update flags LoadPersons m2 in - ( { m3 | loading = False }, Cmd.batch [ c2, c3 ] ) + ( { m3 | loading = Basics.max 0 (model.loading - 1) }, Cmd.batch [ c2, c3 ] ) else - ( { model | formError = Just res.message, loading = False }, Cmd.none ) + ( { model + | formError = Just res.message + , loading = Basics.max 0 (model.loading - 1) + } + , Cmd.none + ) SubmitResp (Err err) -> - ( { model | formError = Just (Util.Http.errorToString err), loading = False }, Cmd.none ) + ( { model + | formError = Just (Util.Http.errorToString err) + , loading = Basics.max 0 (model.loading - 1) + } + , Cmd.none + ) RequestDelete -> update flags (YesNoMsg Comp.YesNoDimmer.activate) model @@ -198,6 +230,11 @@ update flags msg model = ( m, Api.getPersons flags str PersonResp ) +isLoading : Model -> Bool +isLoading model = + model.loading /= 0 + + view : UiSettings -> Model -> Html Msg view settings model = if model.viewMode == Table then @@ -241,7 +278,7 @@ viewTable model = , div [ classList [ ( "ui dimmer", True ) - , ( "active", model.loading ) + , ( "active", isLoading model ) ] ] [ div [ class "ui loader" ] [] @@ -253,9 +290,9 @@ viewForm : UiSettings -> Model -> Html Msg viewForm settings model = let newPerson = - model.formModel.org.id == "" + model.formModel.person.id == "" in - Html.form [ class "ui segment", onSubmit Submit ] + div [ class "ui segment" ] [ Html.map YesNoMsg (Comp.YesNoDimmer.view model.deleteConfirm) , if newPerson then h3 [ class "ui dividing header" ] @@ -264,10 +301,10 @@ viewForm settings model = else h3 [ class "ui dividing header" ] - [ text ("Edit person: " ++ model.formModel.org.name) + [ text ("Edit person: " ++ model.formModel.person.name) , div [ class "sub header" ] [ text "Id: " - , text model.formModel.org.id + , text model.formModel.person.id ] ] , Html.map FormMsg (Comp.PersonForm.view settings model.formModel) @@ -280,14 +317,25 @@ viewForm settings model = [ Maybe.withDefault "" model.formError |> text ] , div [ class "ui horizontal divider" ] [] - , button [ class "ui primary button", type_ "submit" ] + , button + [ class "ui primary button" + , onClick Submit + ] [ text "Submit" ] - , a [ class "ui secondary button", onClick (SetViewMode Table), href "" ] + , a + [ class "ui secondary button" + , onClick (SetViewMode Table) + , href "" + ] [ text "Cancel" ] , if not newPerson then - a [ class "ui right floated red button", href "", onClick RequestDelete ] + a + [ class "ui right floated red button" + , href "" + , onClick RequestDelete + ] [ text "Delete" ] else @@ -295,7 +343,7 @@ viewForm settings model = , div [ classList [ ( "ui dimmer", True ) - , ( "active", model.loading ) + , ( "active", isLoading model ) ] ] [ div [ class "ui loader" ] [] diff --git a/modules/webapp/src/main/elm/Comp/PersonTable.elm b/modules/webapp/src/main/elm/Comp/PersonTable.elm index d94a7a5b..2296be79 100644 --- a/modules/webapp/src/main/elm/Comp/PersonTable.elm +++ b/modules/webapp/src/main/elm/Comp/PersonTable.elm @@ -53,8 +53,9 @@ view model = [ thead [] [ tr [] [ th [ class "collapsing" ] [] - , th [ class "collapsing" ] [ text "Name" ] , th [ class "collapsing center aligned" ] [ text "Concerning" ] + , th [] [ text "Name" ] + , th [] [ text "Organization" ] , th [] [ text "Address" ] , th [] [ text "Contact" ] ] @@ -79,9 +80,6 @@ renderPersonLine model person = , text "Edit" ] ] - , td [ class "collapsing" ] - [ text person.name - ] , td [ class "center aligned" ] [ if person.concerning then i [ class "check square outline icon" ] [] @@ -89,6 +87,14 @@ renderPersonLine model person = else i [ class "minus square outline icon" ] [] ] + , td [] + [ text person.name + ] + , td [] + [ Maybe.map .name person.organization + |> Maybe.withDefault "-" + |> text + ] , td [] [ Util.Address.toString person.address |> text ] diff --git a/modules/webapp/src/main/elm/Comp/SearchMenu.elm b/modules/webapp/src/main/elm/Comp/SearchMenu.elm index 9f677053..8c65c6fb 100644 --- a/modules/webapp/src/main/elm/Comp/SearchMenu.elm +++ b/modules/webapp/src/main/elm/Comp/SearchMenu.elm @@ -93,13 +93,7 @@ init = , selected = Nothing } , orgModel = - Comp.Dropdown.makeModel - { multiple = False - , searchable = \n -> n > 0 - , makeOption = \e -> { value = e.id, text = e.name, additional = "" } - , labelColor = \_ -> \_ -> "" - , placeholder = "Choose an organization" - } + Comp.Dropdown.orgDropdown , corrPersonModel = Comp.Dropdown.makeSingle { makeOption = \e -> { value = e.id, text = e.name, additional = "" }