Allow to create fields when editing items

This commit is contained in:
Eike Kettner 2020-11-21 22:53:06 +01:00
parent af1cca7d83
commit cc6db61a3a
6 changed files with 188 additions and 21 deletions

View File

@ -20,6 +20,7 @@ module Api exposing
, deleteAllItems , deleteAllItems
, deleteAttachment , deleteAttachment
, deleteCustomField , deleteCustomField
, deleteCustomValue
, deleteEquip , deleteEquip
, deleteFolder , deleteFolder
, deleteImapSettings , deleteImapSettings
@ -78,6 +79,7 @@ module Api exposing
, postSource , postSource
, postTag , postTag
, putCustomField , putCustomField
, putCustomValue
, putUser , putUser
, refreshSession , refreshSession
, register , register
@ -134,6 +136,7 @@ import Api.Model.Collective exposing (Collective)
import Api.Model.CollectiveSettings exposing (CollectiveSettings) import Api.Model.CollectiveSettings exposing (CollectiveSettings)
import Api.Model.ContactList exposing (ContactList) import Api.Model.ContactList exposing (ContactList)
import Api.Model.CustomFieldList exposing (CustomFieldList) import Api.Model.CustomFieldList exposing (CustomFieldList)
import Api.Model.CustomFieldValue exposing (CustomFieldValue)
import Api.Model.DirectionValue exposing (DirectionValue) import Api.Model.DirectionValue exposing (DirectionValue)
import Api.Model.EmailSettings exposing (EmailSettings) import Api.Model.EmailSettings exposing (EmailSettings)
import Api.Model.EmailSettingsList exposing (EmailSettingsList) import Api.Model.EmailSettingsList exposing (EmailSettingsList)
@ -208,6 +211,35 @@ import Util.Http as Http2
--- Custom Fields --- Custom Fields
deleteCustomValue :
Flags
-> String
-> String
-> (Result Http.Error BasicResult -> msg)
-> Cmd msg
deleteCustomValue flags item field receive =
Http2.authDelete
{ url = flags.config.baseUrl ++ "/api/v1/sec/item/" ++ item ++ "/customfield/" ++ field
, account = getAccount flags
, expect = Http.expectJson receive Api.Model.BasicResult.decoder
}
putCustomValue :
Flags
-> String
-> CustomFieldValue
-> (Result Http.Error BasicResult -> msg)
-> Cmd msg
putCustomValue flags item fieldValue receive =
Http2.authPut
{ url = flags.config.baseUrl ++ "/api/v1/sec/item/" ++ item ++ "/customfield"
, account = getAccount flags
, body = Http.jsonBody (Api.Model.CustomFieldValue.encode fieldValue)
, expect = Http.expectJson receive Api.Model.BasicResult.decoder
}
getCustomFields : Flags -> String -> (Result Http.Error CustomFieldList -> msg) -> Cmd msg getCustomFields : Flags -> String -> (Result Http.Error CustomFieldList -> msg) -> Cmd msg
getCustomFields flags query receive = getCustomFields flags query receive =
Http2.authGet Http2.authGet

View File

@ -1,8 +1,11 @@
module Comp.CustomFieldDetail exposing module Comp.CustomFieldForm exposing
( Model ( Model
, Msg , Msg
, ViewSettings
, fullViewSettings
, init , init
, initEmpty , initEmpty
, makeField
, update , update
, view , view
) )
@ -181,13 +184,26 @@ update flags msg model =
--- View --- View
view : Flags -> Model -> Html Msg type alias ViewSettings =
view _ model = { classes : String
, showControls : Bool
}
fullViewSettings : ViewSettings
fullViewSettings =
{ classes = "ui error form segment"
, showControls = True
}
view : ViewSettings -> Model -> Html Msg
view viewSettings model =
let let
mkItem cft = mkItem cft =
Comp.FixedDropdown.Item cft (Data.CustomFieldType.label cft) Comp.FixedDropdown.Item cft (Data.CustomFieldType.label cft)
in in
div [ class "ui error form segment" ] div [ class viewSettings.classes ]
([ Html.map DeleteMsg (Comp.YesNoDimmer.view model.deleteDimmer) ([ Html.map DeleteMsg (Comp.YesNoDimmer.view model.deleteDimmer)
, if model.field.id == "" then , if model.field.id == "" then
div [] div []
@ -253,7 +269,12 @@ view _ model =
] ]
] ]
] ]
++ viewButtons model ++ (if viewSettings.showControls then
viewButtons model
else
[]
)
) )

View File

@ -10,7 +10,7 @@ module Comp.CustomFieldManage exposing
import Api import Api
import Api.Model.CustomField exposing (CustomField) import Api.Model.CustomField exposing (CustomField)
import Api.Model.CustomFieldList exposing (CustomFieldList) import Api.Model.CustomFieldList exposing (CustomFieldList)
import Comp.CustomFieldDetail import Comp.CustomFieldForm
import Comp.CustomFieldTable import Comp.CustomFieldTable
import Data.Flags exposing (Flags) import Data.Flags exposing (Flags)
import Html exposing (..) import Html exposing (..)
@ -21,7 +21,7 @@ import Http
type alias Model = type alias Model =
{ tableModel : Comp.CustomFieldTable.Model { tableModel : Comp.CustomFieldTable.Model
, detailModel : Maybe Comp.CustomFieldDetail.Model , detailModel : Maybe Comp.CustomFieldForm.Model
, fields : List CustomField , fields : List CustomField
, query : String , query : String
, loading : Bool , loading : Bool
@ -30,7 +30,7 @@ type alias Model =
type Msg type Msg
= TableMsg Comp.CustomFieldTable.Msg = TableMsg Comp.CustomFieldTable.Msg
| DetailMsg Comp.CustomFieldDetail.Msg | DetailMsg Comp.CustomFieldForm.Msg
| CustomFieldListResp (Result Http.Error CustomFieldList) | CustomFieldListResp (Result Http.Error CustomFieldList)
| SetQuery String | SetQuery String
| InitNewCustomField | InitNewCustomField
@ -68,7 +68,7 @@ update flags msg model =
detail = detail =
case action of case action of
Comp.CustomFieldTable.EditAction item -> Comp.CustomFieldTable.EditAction item ->
Comp.CustomFieldDetail.init item |> Just Comp.CustomFieldForm.init item |> Just
Comp.CustomFieldTable.NoAction -> Comp.CustomFieldTable.NoAction ->
model.detailModel model.detailModel
@ -80,7 +80,7 @@ update flags msg model =
Just detail -> Just detail ->
let let
( dm, dc, back ) = ( dm, dc, back ) =
Comp.CustomFieldDetail.update flags lm detail Comp.CustomFieldForm.update flags lm detail
cmd = cmd =
if back then if back then
@ -120,7 +120,7 @@ update flags msg model =
InitNewCustomField -> InitNewCustomField ->
let let
sd = sd =
Comp.CustomFieldDetail.initEmpty Comp.CustomFieldForm.initEmpty
in in
( { model | detailModel = Just sd } ( { model | detailModel = Just sd }
, Cmd.none , Cmd.none
@ -141,10 +141,14 @@ view flags model =
viewTable model viewTable model
viewDetail : Flags -> Comp.CustomFieldDetail.Model -> Html Msg viewDetail : Flags -> Comp.CustomFieldForm.Model -> Html Msg
viewDetail flags detailModel = viewDetail flags detailModel =
let
viewSettings =
Comp.CustomFieldForm.fullViewSettings
in
div [] div []
[ Html.map DetailMsg (Comp.CustomFieldDetail.view flags detailModel) [ Html.map DetailMsg (Comp.CustomFieldForm.view viewSettings detailModel)
] ]

View File

@ -1,8 +1,10 @@
module Comp.CustomFieldMultiInput exposing module Comp.CustomFieldMultiInput exposing
( Model ( FieldResult(..)
, Model
, Msg , Msg
, UpdateResult , UpdateResult
, init , init
, initCmd
, initWith , initWith
, update , update
, view , view
@ -64,10 +66,15 @@ initWith fields =
init : Flags -> ( Model, Cmd Msg ) init : Flags -> ( Model, Cmd Msg )
init flags = init flags =
( initWith [] ( initWith []
, Api.getCustomFields flags "" CustomFieldResp , initCmd flags
) )
initCmd : Flags -> Cmd Msg
initCmd flags =
Api.getCustomFields flags "" CustomFieldResp
--- Update --- Update

View File

@ -7,6 +7,7 @@ module Comp.DetailEdit exposing
, editPerson , editPerson
, initConcPerson , initConcPerson
, initCorrPerson , initCorrPerson
, initCustomField
, initEquip , initEquip
, initOrg , initOrg
, initTag , initTag
@ -26,9 +27,11 @@ rendered in a modal.
import Api import Api
import Api.Model.BasicResult exposing (BasicResult) import Api.Model.BasicResult exposing (BasicResult)
import Api.Model.Equipment exposing (Equipment) import Api.Model.Equipment exposing (Equipment)
import Api.Model.NewCustomField exposing (NewCustomField)
import Api.Model.Organization exposing (Organization) import Api.Model.Organization exposing (Organization)
import Api.Model.Person exposing (Person) import Api.Model.Person exposing (Person)
import Api.Model.Tag exposing (Tag) import Api.Model.Tag exposing (Tag)
import Comp.CustomFieldForm
import Comp.EquipmentForm import Comp.EquipmentForm
import Comp.OrgForm import Comp.OrgForm
import Comp.PersonForm import Comp.PersonForm
@ -36,6 +39,7 @@ import Comp.TagForm
import Data.Flags exposing (Flags) import Data.Flags exposing (Flags)
import Data.Icons as Icons import Data.Icons as Icons
import Data.UiSettings exposing (UiSettings) import Data.UiSettings exposing (UiSettings)
import Data.Validated
import Html exposing (..) import Html exposing (..)
import Html.Attributes exposing (..) import Html.Attributes exposing (..)
import Html.Events exposing (onClick) import Html.Events exposing (onClick)
@ -58,6 +62,7 @@ type FormModel
| PMC Comp.PersonForm.Model | PMC Comp.PersonForm.Model
| OM Comp.OrgForm.Model | OM Comp.OrgForm.Model
| EM Comp.EquipmentForm.Model | EM Comp.EquipmentForm.Model
| CFM Comp.CustomFieldForm.Model
fold : fold :
@ -65,9 +70,10 @@ fold :
-> (Comp.PersonForm.Model -> a) -> (Comp.PersonForm.Model -> a)
-> (Comp.OrgForm.Model -> a) -> (Comp.OrgForm.Model -> a)
-> (Comp.EquipmentForm.Model -> a) -> (Comp.EquipmentForm.Model -> a)
-> (Comp.CustomFieldForm.Model -> a)
-> FormModel -> FormModel
-> a -> a
fold ft fp fo fe model = fold ft fp fo fe fcf model =
case model of case model of
TM tm -> TM tm ->
ft tm ft tm
@ -84,6 +90,9 @@ fold ft fp fo fe model =
EM em -> EM em ->
fe em fe em
CFM fm ->
fcf fm
init : String -> FormModel -> Model init : String -> FormModel -> Model
init itemId fm = init itemId fm =
@ -168,11 +177,21 @@ initTagByName itemId name =
initTag itemId tm_ initTag itemId tm_
initCustomField : String -> Model
initCustomField itemId =
let
cfm =
Comp.CustomFieldForm.initEmpty
in
init itemId (CFM cfm)
type Msg type Msg
= TagMsg Comp.TagForm.Msg = TagMsg Comp.TagForm.Msg
| PersonMsg Comp.PersonForm.Msg | PersonMsg Comp.PersonForm.Msg
| OrgMsg Comp.OrgForm.Msg | OrgMsg Comp.OrgForm.Msg
| EquipMsg Comp.EquipmentForm.Msg | EquipMsg Comp.EquipmentForm.Msg
| CustomFieldMsg Comp.CustomFieldForm.Msg
| Submit | Submit
| Cancel | Cancel
| SubmitResp (Result Http.Error BasicResult) | SubmitResp (Result Http.Error BasicResult)
@ -186,6 +205,7 @@ type Value
| SubmitPerson Person | SubmitPerson Person
| SubmitOrg Organization | SubmitOrg Organization
| SubmitEquip Equipment | SubmitEquip Equipment
| SubmitCustomField NewCustomField
| CancelForm | CancelForm
@ -207,6 +227,18 @@ makeValue fm =
EM em -> EM em ->
SubmitEquip (Comp.EquipmentForm.getEquipment em) SubmitEquip (Comp.EquipmentForm.getEquipment em)
CFM fieldModel ->
let
cfield =
Comp.CustomFieldForm.makeField fieldModel
in
case cfield of
Data.Validated.Valid field ->
SubmitCustomField field
_ ->
CancelForm
--- Update --- Update
@ -432,6 +464,24 @@ update flags msg model =
, Nothing , Nothing
) )
CFM fm ->
let
cfield =
Comp.CustomFieldForm.makeField fm
in
case cfield of
Data.Validated.Valid newField ->
( { model | submitting = True }
, Api.postCustomField flags newField SubmitResp
, Nothing
)
_ ->
( { model | result = failMsg }
, Cmd.none
, Nothing
)
TagMsg lm -> TagMsg lm ->
case model.form of case model.form of
TM tm -> TM tm ->
@ -517,11 +567,36 @@ update flags msg model =
_ -> _ ->
( model, Cmd.none, Nothing ) ( model, Cmd.none, Nothing )
CustomFieldMsg lm ->
case model.form of
CFM fm ->
let
( fm_, fc_, _ ) =
Comp.CustomFieldForm.update flags lm fm
in
( { model
| form = CFM fm_
, result = Nothing
}
, Cmd.map CustomFieldMsg fc_
, Nothing
)
_ ->
( model, Cmd.none, Nothing )
--- View --- View
customFieldFormSettings : Comp.CustomFieldForm.ViewSettings
customFieldFormSettings =
{ classes = "ui error form"
, showControls = False
}
viewButtons : Model -> List (Html Msg) viewButtons : Model -> List (Html Msg)
viewButtons model = viewButtons model =
[ button [ button
@ -575,6 +650,9 @@ viewIntern settings withButtons model =
EM em -> EM em ->
Html.map EquipMsg (Comp.EquipmentForm.view em) Html.map EquipMsg (Comp.EquipmentForm.view em)
CFM fm ->
Html.map CustomFieldMsg (Comp.CustomFieldForm.view customFieldFormSettings fm)
] ]
++ (if withButtons then ++ (if withButtons then
div [ class "ui divider" ] [] :: viewButtons model div [ class "ui divider" ] [] :: viewButtons model
@ -601,12 +679,14 @@ viewModal settings mm =
(\_ -> "Add Person") (\_ -> "Add Person")
(\_ -> "Add Organization") (\_ -> "Add Organization")
(\_ -> "Add Equipment") (\_ -> "Add Equipment")
(\_ -> "Add Custom Field")
headIcon = headIcon =
fold (\_ -> Icons.tagIcon "") fold (\_ -> Icons.tagIcon "")
(\_ -> Icons.personIcon "") (\_ -> Icons.personIcon "")
(\_ -> Icons.organizationIcon "") (\_ -> Icons.organizationIcon "")
(\_ -> Icons.equipmentIcon "") (\_ -> Icons.equipmentIcon "")
(\_ -> Icons.customFieldIcon "")
in in
div div
[ classList [ classList

View File

@ -2,6 +2,7 @@ module Comp.ItemDetail.Update exposing (update)
import Api import Api
import Api.Model.BasicResult exposing (BasicResult) import Api.Model.BasicResult exposing (BasicResult)
import Api.Model.CustomFieldValue exposing (CustomFieldValue)
import Api.Model.DirectionValue exposing (DirectionValue) import Api.Model.DirectionValue exposing (DirectionValue)
import Api.Model.IdName exposing (IdName) import Api.Model.IdName exposing (IdName)
import Api.Model.ItemDetail exposing (ItemDetail) import Api.Model.ItemDetail exposing (ItemDetail)
@ -13,7 +14,7 @@ import Api.Model.ReferenceList exposing (ReferenceList)
import Api.Model.Tag exposing (Tag) import Api.Model.Tag exposing (Tag)
import Browser.Navigation as Nav import Browser.Navigation as Nav
import Comp.AttachmentMeta import Comp.AttachmentMeta
import Comp.CustomFieldMultiInput import Comp.CustomFieldMultiInput exposing (FieldResult(..))
import Comp.DatePicker import Comp.DatePicker
import Comp.DetailEdit import Comp.DetailEdit
import Comp.Dropdown exposing (isDropdownChangeMsg) import Comp.Dropdown exposing (isDropdownChangeMsg)
@ -238,6 +239,7 @@ update key flags inav settings msg model =
, getOptions flags , getOptions flags
, proposalCmd , proposalCmd
, Api.getSentMails flags item.id SentMailsResp , Api.getSentMails flags item.id SentMailsResp
, Cmd.map CustomFieldMsg (Comp.CustomFieldMultiInput.initCmd flags)
] ]
, sub = , sub =
Sub.batch Sub.batch
@ -1286,17 +1288,38 @@ update key flags inav settings msg model =
result = result =
Comp.CustomFieldMultiInput.update lm model.customFieldsModel Comp.CustomFieldMultiInput.update lm model.customFieldsModel
model_ =
{ model | customFieldsModel = result.model }
cmd_ = cmd_ =
Cmd.map CustomFieldMsg result.cmd Cmd.map CustomFieldMsg result.cmd
action =
case result.result of
NoResult ->
Cmd.none
FieldValueRemove field ->
Api.deleteCustomValue flags model.item.id field.id SaveResp
FieldValueChange field value ->
Api.putCustomValue flags model.item.id (CustomFieldValue field.id value) SaveResp
FieldCreateNew ->
Cmd.none
sub_ = sub_ =
Sub.map CustomFieldMsg result.subs Sub.map CustomFieldMsg result.subs
modalEdit =
if result.result == FieldCreateNew then
Just (Comp.DetailEdit.initCustomField model.item.id)
else
Nothing
model_ =
{ model | customFieldsModel = result.model, modalEdit = modalEdit }
in in
{ model = model_ { model = model_
, cmd = cmd_ , cmd = Cmd.batch [ cmd_, action ]
, sub = sub_ , sub = sub_
, linkTarget = Comp.LinkTarget.LinkNone , linkTarget = Comp.LinkTarget.LinkNone
} }