From 9b3054297479f6cb52931d1839b6860a81857521 Mon Sep 17 00:00:00 2001 From: Eike Kettner Date: Sun, 19 Apr 2020 23:46:46 +0200 Subject: [PATCH] Copy/paste form together --- modules/webapp/src/main/elm/App/Data.elm | 2 +- modules/webapp/src/main/elm/App/Update.elm | 6 +- .../src/main/elm/Comp/NotificationForm.elm | 221 +++++++++++++++++- .../webapp/src/main/elm/Comp/SearchMenu.elm | 23 +- .../src/main/elm/Page/UserSettings/Data.elm | 8 +- .../src/main/elm/Page/UserSettings/Update.elm | 16 +- .../src/main/elm/Page/UserSettings/View.elm | 13 +- modules/webapp/src/main/elm/Util/Tag.elm | 22 ++ 8 files changed, 275 insertions(+), 36 deletions(-) create mode 100644 modules/webapp/src/main/elm/Util/Tag.elm diff --git a/modules/webapp/src/main/elm/App/Data.elm b/modules/webapp/src/main/elm/App/Data.elm index d165bd24..d57fdef5 100644 --- a/modules/webapp/src/main/elm/App/Data.elm +++ b/modules/webapp/src/main/elm/App/Data.elm @@ -61,7 +61,7 @@ init key url flags = , loginModel = Page.Login.Data.emptyModel , manageDataModel = Page.ManageData.Data.emptyModel , collSettingsModel = Page.CollectiveSettings.Data.emptyModel - , userSettingsModel = Page.UserSettings.Data.emptyModel + , userSettingsModel = Page.UserSettings.Data.emptyModel flags , queueModel = Page.Queue.Data.emptyModel , registerModel = Page.Register.Data.emptyModel , uploadModel = Page.Upload.Data.emptyModel diff --git a/modules/webapp/src/main/elm/App/Update.elm b/modules/webapp/src/main/elm/App/Update.elm index 0da1a1ab..56783c91 100644 --- a/modules/webapp/src/main/elm/App/Update.elm +++ b/modules/webapp/src/main/elm/App/Update.elm @@ -323,7 +323,11 @@ initPage model page = model UserSettingPage -> - updateQueue Page.Queue.Data.StopRefresh model + Util.Update.andThen1 + [ updateQueue Page.Queue.Data.StopRefresh + , updateUserSettings Page.UserSettings.Data.Init + ] + model QueuePage -> updateQueue Page.Queue.Data.Init model diff --git a/modules/webapp/src/main/elm/Comp/NotificationForm.elm b/modules/webapp/src/main/elm/Comp/NotificationForm.elm index 8c67631a..bc374065 100644 --- a/modules/webapp/src/main/elm/Comp/NotificationForm.elm +++ b/modules/webapp/src/main/elm/Comp/NotificationForm.elm @@ -6,37 +6,242 @@ module Comp.NotificationForm exposing , view ) +import Api +import Api.Model.EmailSettingsList exposing (EmailSettingsList) import Api.Model.NotificationSettings exposing (NotificationSettings) +import Api.Model.Tag exposing (Tag) +import Api.Model.TagList exposing (TagList) +import Comp.Dropdown +import Comp.EmailInput +import Comp.IntField import Data.Flags exposing (Flags) import Html exposing (..) import Html.Attributes exposing (..) +import Html.Events exposing (onCheck, onClick, onInput) +import Http +import Util.Http +import Util.Tag +import Util.Update type alias Model = { settings : NotificationSettings + , connectionModel : Comp.Dropdown.Model String + , tagInclModel : Comp.Dropdown.Model Tag + , tagExclModel : Comp.Dropdown.Model Tag + , recipients : List String + , recipientsModel : Comp.EmailInput.Model + , remindDays : Maybe Int + , remindDaysModel : Comp.IntField.Model + , enabled : Bool + , timer : String + , formError : Maybe String } type Msg = Submit + | TagIncMsg (Comp.Dropdown.Msg Tag) + | TagExcMsg (Comp.Dropdown.Msg Tag) + | ConnMsg (Comp.Dropdown.Msg String) + | ConnResp (Result Http.Error EmailSettingsList) + | RecipientMsg Comp.EmailInput.Msg + | GetTagsResp (Result Http.Error TagList) + | RemindDaysMsg Comp.IntField.Msg + | ToggleEnabled + | SetSchedule String -init : Model -init = - { settings = Api.Model.NotificationSettings.empty - } +initCmd : Flags -> Cmd Msg +initCmd flags = + Cmd.batch + [ Api.getMailSettings flags "" ConnResp + , Api.getTags flags "" GetTagsResp + ] + + +init : Flags -> ( Model, Cmd Msg ) +init flags = + ( { settings = Api.Model.NotificationSettings.empty + , connectionModel = + Comp.Dropdown.makeSingle + { makeOption = \a -> { value = a, text = a } + , placeholder = "Select connection..." + } + , tagInclModel = Util.Tag.makeDropdownModel + , tagExclModel = Util.Tag.makeDropdownModel + , recipients = [] + , recipientsModel = Comp.EmailInput.init + , remindDays = Just 1 + , remindDaysModel = Comp.IntField.init (Just 1) Nothing True "Remind Days" + , enabled = False + , timer = "*-*-1/7 12:00" + , formError = Nothing + } + , initCmd flags + ) update : Flags -> Msg -> Model -> ( Model, Cmd Msg ) update flags msg model = - ( model, Cmd.none ) + case msg of + SetSchedule str -> + ( { model | timer = str }, Cmd.none ) + + RecipientMsg m -> + let + ( em, ec, rec ) = + Comp.EmailInput.update flags model.recipients m model.recipientsModel + in + ( { model | recipients = rec, recipientsModel = em } + , Cmd.map RecipientMsg ec + ) + + ConnMsg m -> + let + ( cm, _ ) = + -- dropdown doesn't use cmd!! + Comp.Dropdown.update m model.connectionModel + in + ( { model | connectionModel = cm }, Cmd.none ) + + ConnResp (Ok list) -> + let + names = + List.map .name list.items + + cm = + Comp.Dropdown.makeSingleList + { makeOption = \a -> { value = a, text = a } + , placeholder = "Select Connection..." + , options = names + , selected = List.head names + } + in + ( { model + | connectionModel = cm + , formError = + if names == [] then + Just "No E-Mail connections configured. Goto user settings to add one." + + else + Nothing + } + , Cmd.none + ) + + ConnResp (Err err) -> + ( { model | formError = Just (Util.Http.errorToString err) }, Cmd.none ) + + TagIncMsg m -> + let + ( m2, c2 ) = + Comp.Dropdown.update m model.tagInclModel + in + ( { model | tagInclModel = m2 } + , Cmd.map TagIncMsg c2 + ) + + TagExcMsg m -> + let + ( m2, c2 ) = + Comp.Dropdown.update m model.tagExclModel + in + ( { model | tagExclModel = m2 } + , Cmd.map TagExcMsg c2 + ) + + GetTagsResp (Ok tags) -> + let + tagList = + Comp.Dropdown.SetOptions tags.items + in + Util.Update.andThen1 + [ update flags (TagIncMsg tagList) + , update flags (TagExcMsg tagList) + ] + model + + GetTagsResp (Err _) -> + ( model, Cmd.none ) + + RemindDaysMsg m -> + let + ( pm, val ) = + Comp.IntField.update m model.remindDaysModel + in + ( { model + | remindDaysModel = pm + , remindDays = val + } + , Cmd.none + ) + + ToggleEnabled -> + ( { model | enabled = not model.enabled }, Cmd.none ) + + _ -> + ( model, Cmd.none ) -view : Model -> Html Msg -view model = +view : String -> Model -> Html Msg +view extraClasses model = div [ classList [ ( "ui form", True ) + , ( extraClasses, True ) + , ( "error", model.formError /= Nothing ) + ] + ] + [ div [ class "inline field" ] + [ div [ class "ui checkbox" ] + [ input + [ type_ "checkbox" + , onCheck (\_ -> ToggleEnabled) + , checked model.enabled + ] + [] + , label [] [ text "Enabled" ] + ] + ] + , div [ class "required field" ] + [ label [] [ text "Send via" ] + , Html.map ConnMsg (Comp.Dropdown.view model.connectionModel) + ] + , div [ class "required field" ] + [ label [] + [ text "Recipient(s)" + ] + , Html.map RecipientMsg + (Comp.EmailInput.view model.recipients model.recipientsModel) + ] + , div [ class "field" ] + [ label [] [ text "Tags Include (and)" ] + , Html.map TagIncMsg (Comp.Dropdown.view model.tagInclModel) + ] + , div [ class "field" ] + [ label [] [ text "Tags Exclude (or)" ] + , Html.map TagExcMsg (Comp.Dropdown.view model.tagExclModel) + ] + , Html.map RemindDaysMsg + (Comp.IntField.view model.remindDays + "required field" + model.remindDaysModel + ) + , div [ class "required field" ] + [ label [] [ text "Schedule" ] + , input + [ type_ "text" + , onInput SetSchedule + , value model.timer + ] + [] + ] + , div [ class "ui divider" ] [] + , button + [ class "ui primary button" + , onClick Submit + ] + [ text "Submit" ] ] - [] diff --git a/modules/webapp/src/main/elm/Comp/SearchMenu.elm b/modules/webapp/src/main/elm/Comp/SearchMenu.elm index e1be1438..be7ad087 100644 --- a/modules/webapp/src/main/elm/Comp/SearchMenu.elm +++ b/modules/webapp/src/main/elm/Comp/SearchMenu.elm @@ -25,7 +25,7 @@ import Html exposing (..) import Html.Attributes exposing (..) import Html.Events exposing (onCheck, onInput) import Http -import Util.Maybe +import Util.Tag import Util.Update @@ -56,8 +56,8 @@ type alias Model = emptyModel : Model emptyModel = - { tagInclModel = makeTagModel - , tagExclModel = makeTagModel + { tagInclModel = Util.Tag.makeDropdownModel + , tagExclModel = Util.Tag.makeDropdownModel , directionModel = Comp.Dropdown.makeSingleList { makeOption = @@ -130,23 +130,6 @@ type Msg | ResetForm -makeTagModel : Comp.Dropdown.Model Tag -makeTagModel = - Comp.Dropdown.makeModel - { multiple = True - , searchable = \n -> n > 4 - , makeOption = \tag -> { value = tag.id, text = tag.name } - , labelColor = - \tag -> - if Util.Maybe.nonEmpty tag.category then - "basic blue" - - else - "" - , placeholder = "Choose a tag…" - } - - getDirection : Model -> Maybe Direction getDirection model = let diff --git a/modules/webapp/src/main/elm/Page/UserSettings/Data.elm b/modules/webapp/src/main/elm/Page/UserSettings/Data.elm index 079ca296..7519454c 100644 --- a/modules/webapp/src/main/elm/Page/UserSettings/Data.elm +++ b/modules/webapp/src/main/elm/Page/UserSettings/Data.elm @@ -8,6 +8,7 @@ module Page.UserSettings.Data exposing import Comp.ChangePasswordForm import Comp.EmailSettingsManage import Comp.NotificationForm +import Data.Flags exposing (Flags) type alias Model = @@ -18,12 +19,12 @@ type alias Model = } -emptyModel : Model -emptyModel = +emptyModel : Flags -> Model +emptyModel flags = { currentTab = Nothing , changePassModel = Comp.ChangePasswordForm.emptyModel , emailSettingsModel = Comp.EmailSettingsManage.emptyModel - , notificationModel = Comp.NotificationForm.init + , notificationModel = Tuple.first (Comp.NotificationForm.init flags) } @@ -38,3 +39,4 @@ type Msg | ChangePassMsg Comp.ChangePasswordForm.Msg | EmailSettingsMsg Comp.EmailSettingsManage.Msg | NotificationMsg Comp.NotificationForm.Msg + | Init diff --git a/modules/webapp/src/main/elm/Page/UserSettings/Update.elm b/modules/webapp/src/main/elm/Page/UserSettings/Update.elm index fa66d05e..efceaa14 100644 --- a/modules/webapp/src/main/elm/Page/UserSettings/Update.elm +++ b/modules/webapp/src/main/elm/Page/UserSettings/Update.elm @@ -10,6 +10,14 @@ import Page.UserSettings.Data exposing (..) update : Flags -> Msg -> Model -> ( Model, Cmd Msg ) update flags msg model = case msg of + Init -> + let + cmd = + Cmd.map NotificationMsg + (Tuple.second (Comp.NotificationForm.init flags)) + in + ( model, cmd ) + SetTab t -> let m = @@ -28,8 +36,12 @@ update flags msg model = ( m, Cmd.none ) NotificationTab -> - -- todo: get initial settings - ( m, Cmd.none ) + let + initCmd = + Cmd.map NotificationMsg + (Tuple.second (Comp.NotificationForm.init flags)) + in + ( m, initCmd ) in ( m2, cmd ) diff --git a/modules/webapp/src/main/elm/Page/UserSettings/View.elm b/modules/webapp/src/main/elm/Page/UserSettings/View.elm index 11260db5..9bb99c0b 100644 --- a/modules/webapp/src/main/elm/Page/UserSettings/View.elm +++ b/modules/webapp/src/main/elm/Page/UserSettings/View.elm @@ -88,5 +88,16 @@ viewNotificationForm model = [ text "Notification" ] ] - , Html.map NotificationMsg (Comp.NotificationForm.view model.notificationModel) + , p [] + [ text "Docspell can notify you once the due dates of your items come closer. " + , text "Notification is done via e-mail. You need to provide a connection in " + , text "your e-mail settings." + ] + , p [] + [ text "Each time this is executed, docspell finds all items that are due in " + , em [] [ text "Remind Days" ] + , text " days." + ] + , Html.map NotificationMsg + (Comp.NotificationForm.view "segment" model.notificationModel) ] diff --git a/modules/webapp/src/main/elm/Util/Tag.elm b/modules/webapp/src/main/elm/Util/Tag.elm new file mode 100644 index 00000000..3b047866 --- /dev/null +++ b/modules/webapp/src/main/elm/Util/Tag.elm @@ -0,0 +1,22 @@ +module Util.Tag exposing (makeDropdownModel) + +import Api.Model.Tag exposing (Tag) +import Comp.Dropdown +import Util.Maybe + + +makeDropdownModel : Comp.Dropdown.Model Tag +makeDropdownModel = + Comp.Dropdown.makeModel + { multiple = True + , searchable = \n -> n > 4 + , makeOption = \tag -> { value = tag.id, text = tag.name } + , labelColor = + \tag -> + if Util.Maybe.nonEmpty tag.category then + "basic blue" + + else + "" + , placeholder = "Choose a tag…" + }