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
, deleteAttachment
, deleteCustomField
, deleteCustomValue
, deleteEquip
, deleteFolder
, deleteImapSettings
@ -78,6 +79,7 @@ module Api exposing
, postSource
, postTag
, putCustomField
, putCustomValue
, putUser
, refreshSession
, register
@ -134,6 +136,7 @@ import Api.Model.Collective exposing (Collective)
import Api.Model.CollectiveSettings exposing (CollectiveSettings)
import Api.Model.ContactList exposing (ContactList)
import Api.Model.CustomFieldList exposing (CustomFieldList)
import Api.Model.CustomFieldValue exposing (CustomFieldValue)
import Api.Model.DirectionValue exposing (DirectionValue)
import Api.Model.EmailSettings exposing (EmailSettings)
import Api.Model.EmailSettingsList exposing (EmailSettingsList)
@ -208,6 +211,35 @@ import Util.Http as Http2
--- 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 query receive =
Http2.authGet

View File

@ -1,8 +1,11 @@
module Comp.CustomFieldDetail exposing
module Comp.CustomFieldForm exposing
( Model
, Msg
, ViewSettings
, fullViewSettings
, init
, initEmpty
, makeField
, update
, view
)
@ -181,13 +184,26 @@ update flags msg model =
--- View
view : Flags -> Model -> Html Msg
view _ model =
type alias ViewSettings =
{ classes : String
, showControls : Bool
}
fullViewSettings : ViewSettings
fullViewSettings =
{ classes = "ui error form segment"
, showControls = True
}
view : ViewSettings -> Model -> Html Msg
view viewSettings model =
let
mkItem cft =
Comp.FixedDropdown.Item cft (Data.CustomFieldType.label cft)
in
div [ class "ui error form segment" ]
div [ class viewSettings.classes ]
([ Html.map DeleteMsg (Comp.YesNoDimmer.view model.deleteDimmer)
, if model.field.id == "" then
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.Model.CustomField exposing (CustomField)
import Api.Model.CustomFieldList exposing (CustomFieldList)
import Comp.CustomFieldDetail
import Comp.CustomFieldForm
import Comp.CustomFieldTable
import Data.Flags exposing (Flags)
import Html exposing (..)
@ -21,7 +21,7 @@ import Http
type alias Model =
{ tableModel : Comp.CustomFieldTable.Model
, detailModel : Maybe Comp.CustomFieldDetail.Model
, detailModel : Maybe Comp.CustomFieldForm.Model
, fields : List CustomField
, query : String
, loading : Bool
@ -30,7 +30,7 @@ type alias Model =
type Msg
= TableMsg Comp.CustomFieldTable.Msg
| DetailMsg Comp.CustomFieldDetail.Msg
| DetailMsg Comp.CustomFieldForm.Msg
| CustomFieldListResp (Result Http.Error CustomFieldList)
| SetQuery String
| InitNewCustomField
@ -68,7 +68,7 @@ update flags msg model =
detail =
case action of
Comp.CustomFieldTable.EditAction item ->
Comp.CustomFieldDetail.init item |> Just
Comp.CustomFieldForm.init item |> Just
Comp.CustomFieldTable.NoAction ->
model.detailModel
@ -80,7 +80,7 @@ update flags msg model =
Just detail ->
let
( dm, dc, back ) =
Comp.CustomFieldDetail.update flags lm detail
Comp.CustomFieldForm.update flags lm detail
cmd =
if back then
@ -120,7 +120,7 @@ update flags msg model =
InitNewCustomField ->
let
sd =
Comp.CustomFieldDetail.initEmpty
Comp.CustomFieldForm.initEmpty
in
( { model | detailModel = Just sd }
, Cmd.none
@ -141,10 +141,14 @@ view flags model =
viewTable model
viewDetail : Flags -> Comp.CustomFieldDetail.Model -> Html Msg
viewDetail : Flags -> Comp.CustomFieldForm.Model -> Html Msg
viewDetail flags detailModel =
let
viewSettings =
Comp.CustomFieldForm.fullViewSettings
in
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
( Model
( FieldResult(..)
, Model
, Msg
, UpdateResult
, init
, initCmd
, initWith
, update
, view
@ -64,10 +66,15 @@ initWith fields =
init : Flags -> ( Model, Cmd Msg )
init flags =
( initWith []
, Api.getCustomFields flags "" CustomFieldResp
, initCmd flags
)
initCmd : Flags -> Cmd Msg
initCmd flags =
Api.getCustomFields flags "" CustomFieldResp
--- Update

View File

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

View File

@ -2,6 +2,7 @@ module Comp.ItemDetail.Update exposing (update)
import Api
import Api.Model.BasicResult exposing (BasicResult)
import Api.Model.CustomFieldValue exposing (CustomFieldValue)
import Api.Model.DirectionValue exposing (DirectionValue)
import Api.Model.IdName exposing (IdName)
import Api.Model.ItemDetail exposing (ItemDetail)
@ -13,7 +14,7 @@ import Api.Model.ReferenceList exposing (ReferenceList)
import Api.Model.Tag exposing (Tag)
import Browser.Navigation as Nav
import Comp.AttachmentMeta
import Comp.CustomFieldMultiInput
import Comp.CustomFieldMultiInput exposing (FieldResult(..))
import Comp.DatePicker
import Comp.DetailEdit
import Comp.Dropdown exposing (isDropdownChangeMsg)
@ -238,6 +239,7 @@ update key flags inav settings msg model =
, getOptions flags
, proposalCmd
, Api.getSentMails flags item.id SentMailsResp
, Cmd.map CustomFieldMsg (Comp.CustomFieldMultiInput.initCmd flags)
]
, sub =
Sub.batch
@ -1286,17 +1288,38 @@ update key flags inav settings msg model =
result =
Comp.CustomFieldMultiInput.update lm model.customFieldsModel
model_ =
{ model | customFieldsModel = result.model }
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.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
{ model = model_
, cmd = cmd_
, cmd = Cmd.batch [ cmd_, action ]
, sub = sub_
, linkTarget = Comp.LinkTarget.LinkNone
}