Adopt UI to allow multiple notification tasks

This commit is contained in:
Eike Kettner
2020-06-13 14:09:46 +02:00
parent e51e84408b
commit 2c13f9307c
8 changed files with 549 additions and 103 deletions

View File

@ -1,7 +1,9 @@
module Comp.NotificationForm exposing
( Model
( Action(..)
, Model
, Msg
, init
, initWith
, update
, view
)
@ -16,6 +18,7 @@ import Comp.CalEventInput
import Comp.Dropdown
import Comp.EmailInput
import Comp.IntField
import Comp.YesNoDimmer
import Data.CalEvent exposing (CalEvent)
import Data.Flags exposing (Flags)
import Data.UiSettings exposing (UiSettings)
@ -45,9 +48,18 @@ type alias Model =
, scheduleModel : Comp.CalEventInput.Model
, formMsg : Maybe BasicResult
, loading : Int
, yesNoDelete : Comp.YesNoDimmer.Model
}
type Action
= SubmitAction NotificationSettings
| StartOnceAction NotificationSettings
| CancelAction
| DeleteAction String
| NoAction
type Msg
= Submit
| TagIncMsg (Comp.Dropdown.Msg Tag)
@ -60,22 +72,63 @@ type Msg
| ToggleEnabled
| ToggleCapOverdue
| CalEventMsg Comp.CalEventInput.Msg
| SetNotificationSettings (Result Http.Error NotificationSettings)
| SubmitResp (Result Http.Error BasicResult)
| StartOnce
| Cancel
| RequestDelete
| YesNoDeleteMsg Comp.YesNoDimmer.Msg
initCmd : Flags -> Cmd Msg
initCmd flags =
Cmd.batch
[ Api.getMailSettings flags "" ConnResp
, Api.getTags flags "" GetTagsResp
, Api.getNotifyDueItems flags SetNotificationSettings
initWith : Flags -> NotificationSettings -> ( Model, Cmd Msg )
initWith flags s =
let
( im, ic ) =
init flags
smtp =
Util.Maybe.fromString s.smtpConnection
|> Maybe.map List.singleton
|> Maybe.withDefault []
removeAction ( tm, _, tc ) =
( tm, tc )
( nm, nc ) =
Util.Update.andThen1
[ update flags (ConnMsg (Comp.Dropdown.SetSelection smtp)) >> removeAction
, update flags (TagIncMsg (Comp.Dropdown.SetSelection s.tagsInclude)) >> removeAction
, update flags (TagExcMsg (Comp.Dropdown.SetSelection s.tagsExclude)) >> removeAction
]
im
newSchedule =
Data.CalEvent.fromEvent s.schedule
|> Maybe.withDefault Data.CalEvent.everyMonth
( sm, sc ) =
Comp.CalEventInput.init flags newSchedule
in
( { nm
| settings = s
, recipients = s.recipients
, remindDays = Just s.remindDays
, enabled = s.enabled
, capOverdue = s.capOverdue
, schedule = Data.Validated.Unknown newSchedule
, scheduleModel = sm
, formMsg = Nothing
, loading = im.loading
, yesNoDelete = Comp.YesNoDimmer.emptyModel
}
, Cmd.batch
[ nc
, ic
, Cmd.map CalEventMsg sc
]
)
init : Flags -> UiSettings -> ( Model, Cmd Msg )
init flags settings =
init : Flags -> ( Model, Cmd Msg )
init flags =
let
initialSchedule =
Data.Validated.Unknown Data.CalEvent.everyMonth
@ -100,10 +153,12 @@ init flags settings =
, schedule = initialSchedule
, scheduleModel = sm
, formMsg = Nothing
, loading = 3
, loading = 2
, yesNoDelete = Comp.YesNoDimmer.emptyModel
}
, Cmd.batch
[ initCmd flags
[ Api.getMailSettings flags "" ConnResp
, Api.getTags flags "" GetTagsResp
, Cmd.map CalEventMsg sc
]
)
@ -155,12 +210,13 @@ makeSettings model =
model.schedule
withValidSettings : (NotificationSettings -> Cmd Msg) -> Model -> ( Model, Cmd Msg )
withValidSettings : (NotificationSettings -> Action) -> Model -> ( Model, Action, Cmd Msg )
withValidSettings mkcmd model =
case makeSettings model of
Valid set ->
( { model | formMsg = Nothing }
, mkcmd set
, Cmd.none
)
Invalid errs _ ->
@ -168,15 +224,19 @@ withValidSettings mkcmd model =
errMsg =
String.join ", " errs
in
( { model | formMsg = Just (BasicResult False errMsg) }, Cmd.none )
( { model | formMsg = Just (BasicResult False errMsg) }
, NoAction
, Cmd.none
)
Unknown _ ->
( { model | formMsg = Just (BasicResult False "An unknown error occured") }
, NoAction
, Cmd.none
)
update : Flags -> Msg -> Model -> ( Model, Cmd Msg )
update : Flags -> Msg -> Model -> ( Model, Action, Cmd Msg )
update flags msg model =
case msg of
CalEventMsg lmsg ->
@ -192,6 +252,7 @@ update flags msg model =
, scheduleModel = cm
, formMsg = Nothing
}
, NoAction
, Cmd.map CalEventMsg cc
)
@ -205,6 +266,7 @@ update flags msg model =
, recipientsModel = em
, formMsg = Nothing
}
, NoAction
, Cmd.map RecipientMsg ec
)
@ -217,6 +279,7 @@ update flags msg model =
| connectionModel = cm
, formMsg = Nothing
}
, NoAction
, Cmd.map ConnMsg cc
)
@ -246,6 +309,7 @@ update flags msg model =
else
Nothing
}
, NoAction
, Cmd.none
)
@ -254,6 +318,7 @@ update flags msg model =
| formMsg = Just (BasicResult False (Util.Http.errorToString err))
, loading = model.loading - 1
}
, NoAction
, Cmd.none
)
@ -266,6 +331,7 @@ update flags msg model =
| tagInclModel = m2
, formMsg = Nothing
}
, NoAction
, Cmd.map TagIncMsg c2
)
@ -278,6 +344,7 @@ update flags msg model =
| tagExclModel = m2
, formMsg = Nothing
}
, NoAction
, Cmd.map TagExcMsg c2
)
@ -285,18 +352,25 @@ update flags msg model =
let
tagList =
Comp.Dropdown.SetOptions tags.items
removeAction ( tm, _, tc ) =
( tm, tc )
( m, c ) =
Util.Update.andThen1
[ update flags (TagIncMsg tagList) >> removeAction
, update flags (TagExcMsg tagList) >> removeAction
]
{ model | loading = model.loading - 1 }
in
Util.Update.andThen1
[ update flags (TagIncMsg tagList)
, update flags (TagExcMsg tagList)
]
{ model | loading = model.loading - 1 }
( m, NoAction, c )
GetTagsResp (Err err) ->
( { model
| loading = model.loading - 1
, formMsg = Just (BasicResult False (Util.Http.errorToString err))
}
, NoAction
, Cmd.none
)
@ -310,6 +384,7 @@ update flags msg model =
, remindDays = val
, formMsg = Nothing
}
, NoAction
, Cmd.none
)
@ -318,6 +393,7 @@ update flags msg model =
| enabled = not model.enabled
, formMsg = Nothing
}
, NoAction
, Cmd.none
)
@ -326,75 +402,49 @@ update flags msg model =
| capOverdue = not model.capOverdue
, formMsg = Nothing
}
, Cmd.none
)
SetNotificationSettings (Ok s) ->
let
smtp =
Util.Maybe.fromString s.smtpConnection
|> Maybe.map List.singleton
|> Maybe.withDefault []
( nm, nc ) =
Util.Update.andThen1
[ update flags (ConnMsg (Comp.Dropdown.SetSelection smtp))
, update flags (TagIncMsg (Comp.Dropdown.SetSelection s.tagsInclude))
, update flags (TagExcMsg (Comp.Dropdown.SetSelection s.tagsExclude))
]
model
newSchedule =
Data.CalEvent.fromEvent s.schedule
|> Maybe.withDefault Data.CalEvent.everyMonth
( sm, sc ) =
Comp.CalEventInput.init flags newSchedule
in
( { nm
| settings = s
, recipients = s.recipients
, remindDays = Just s.remindDays
, enabled = s.enabled
, capOverdue = s.capOverdue
, schedule = Data.Validated.Unknown newSchedule
, scheduleModel = sm
, formMsg = Nothing
, loading = model.loading - 1
}
, Cmd.batch
[ nc
, Cmd.map CalEventMsg sc
]
)
SetNotificationSettings (Err err) ->
( { model
| formMsg = Just (BasicResult False (Util.Http.errorToString err))
, loading = model.loading - 1
}
, NoAction
, Cmd.none
)
Submit ->
withValidSettings
(\set -> Api.submitNotifyDueItems flags set SubmitResp)
SubmitAction
model
StartOnce ->
withValidSettings
(\set -> Api.startOnceNotifyDueItems flags set SubmitResp)
StartOnceAction
model
SubmitResp (Ok res) ->
( { model | formMsg = Just res }
Cancel ->
( model, CancelAction, Cmd.none )
RequestDelete ->
let
( ym, _ ) =
Comp.YesNoDimmer.update
Comp.YesNoDimmer.activate
model.yesNoDelete
in
( { model | yesNoDelete = ym }
, NoAction
, Cmd.none
)
SubmitResp (Err err) ->
( { model
| formMsg = Just (BasicResult False (Util.Http.errorToString err))
}
YesNoDeleteMsg lm ->
let
( ym, flag ) =
Comp.YesNoDimmer.update lm model.yesNoDelete
act =
if flag then
DeleteAction model.settings.id
else
NoAction
in
( { model | yesNoDelete = ym }
, act
, Cmd.none
)
@ -426,7 +476,8 @@ view extraClasses settings model =
, ( "success", isFormSuccess model )
]
]
[ div
[ Html.map YesNoDeleteMsg (Comp.YesNoDimmer.view model.yesNoDelete)
, div
[ classList
[ ( "ui dimmer", True )
, ( "active", model.loading > 0 )
@ -550,6 +601,21 @@ view extraClasses settings model =
]
[ text "Submit"
]
, button
[ class "ui secondary button"
, onClick Cancel
]
[ text "Cancel"
]
, button
[ classList
[ ( "ui red button", True )
, ( "hidden invisible", model.settings.id == "" )
]
, onClick RequestDelete
]
[ text "Delete"
]
, button
[ class "ui right floated button"
, onClick StartOnce

View File

@ -0,0 +1,93 @@
module Comp.NotificationList exposing
( Action(..)
, Model
, Msg
, init
, update
, view
)
import Api.Model.NotificationSettings exposing (NotificationSettings)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (onClick)
import Util.Html
type alias Model =
{}
type Msg
= EditSettings NotificationSettings
type Action
= NoAction
| EditAction NotificationSettings
init : Model
init =
{}
update : Msg -> Model -> ( Model, Action )
update msg model =
case msg of
EditSettings settings ->
( model, EditAction settings )
view : Model -> List NotificationSettings -> Html Msg
view _ items =
div []
[ table [ class "ui very basic table" ]
[ thead []
[ th [ class "collapsing" ] []
, th [ class "collapsing" ]
[ i [ class "check icon" ] []
]
, th [] [ text "Connection" ]
, th [] [ text "Recipients" ]
, th [] [ text "Schedule" ]
, th [] [ text "Remind Days" ]
]
, tbody []
(List.map viewItem items)
]
]
viewItem : NotificationSettings -> Html Msg
viewItem item =
tr []
[ td [ class "collapsing" ]
[ a
[ href "#"
, class "ui basic small blue label"
, onClick (EditSettings item)
]
[ i [ class "edit icon" ] []
, text "Edit"
]
]
, td [ class "collapsing" ]
[ Util.Html.checkbox item.enabled
]
, td []
[ text item.smtpConnection
]
, td []
[ String.join ", " item.recipients |> text
]
, td []
[ code []
[ text item.schedule
]
]
, td []
[ String.fromInt item.remindDays
|> text
]
]

View File

@ -0,0 +1,243 @@
module Comp.NotificationManage exposing
( Model
, Msg
, init
, update
, view
)
import Api
import Api.Model.BasicResult exposing (BasicResult)
import Api.Model.NotificationSettings exposing (NotificationSettings)
import Api.Model.NotificationSettingsList exposing (NotificationSettingsList)
import Comp.NotificationForm
import Comp.NotificationList
import Data.Flags exposing (Flags)
import Data.UiSettings exposing (UiSettings)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (onClick)
import Http
import Util.Http
type alias Model =
{ listModel : Comp.NotificationList.Model
, detailModel : Maybe Comp.NotificationForm.Model
, items : List NotificationSettings
, result : Maybe BasicResult
}
type Msg
= ListMsg Comp.NotificationList.Msg
| DetailMsg Comp.NotificationForm.Msg
| GetDataResp (Result Http.Error NotificationSettingsList)
| NewTask
| SubmitResp (Result Http.Error BasicResult)
| DeleteResp (Result Http.Error BasicResult)
initModel : Model
initModel =
{ listModel = Comp.NotificationList.init
, detailModel = Nothing
, items = []
, result = Nothing
}
initCmd : Flags -> Cmd Msg
initCmd flags =
Api.getNotifyDueItems flags GetDataResp
init : Flags -> ( Model, Cmd Msg )
init flags =
( initModel, initCmd flags )
--- Update
update : Flags -> Msg -> Model -> ( Model, Cmd Msg )
update flags msg model =
case msg of
GetDataResp (Ok res) ->
( { model
| items = res.items
, result = Nothing
}
, Cmd.none
)
GetDataResp (Err err) ->
( { model | result = Just (BasicResult False (Util.Http.errorToString err)) }
, Cmd.none
)
ListMsg lm ->
let
( mm, action ) =
Comp.NotificationList.update lm model.listModel
( detail, cmd ) =
case action of
Comp.NotificationList.NoAction ->
( Nothing, Cmd.none )
Comp.NotificationList.EditAction settings ->
let
( dm, dc ) =
Comp.NotificationForm.initWith flags settings
in
( Just dm, Cmd.map DetailMsg dc )
in
( { model
| listModel = mm
, detailModel = detail
}
, cmd
)
DetailMsg lm ->
case model.detailModel of
Just dm ->
let
( mm, action, mc ) =
Comp.NotificationForm.update flags lm dm
( model_, cmd_ ) =
case action of
Comp.NotificationForm.NoAction ->
( { model | detailModel = Just mm }
, Cmd.none
)
Comp.NotificationForm.SubmitAction settings ->
( { model
| detailModel = Just mm
, result = Nothing
}
, if settings.id == "" then
Api.createNotifyDueItems flags settings SubmitResp
else
Api.updateNotifyDueItems flags settings SubmitResp
)
Comp.NotificationForm.CancelAction ->
( { model
| detailModel = Nothing
, result = Nothing
}
, initCmd flags
)
Comp.NotificationForm.StartOnceAction settings ->
( { model
| detailModel = Just mm
, result = Nothing
}
, Api.startOnceNotifyDueItems flags settings SubmitResp
)
Comp.NotificationForm.DeleteAction id ->
( { model
| detailModel = Just mm
, result = Nothing
}
, Api.deleteNotifyDueItems flags id DeleteResp
)
in
( model_
, Cmd.batch
[ Cmd.map DetailMsg mc
, cmd_
]
)
Nothing ->
( model, Cmd.none )
NewTask ->
let
( mm, mc ) =
Comp.NotificationForm.init flags
in
( { model | detailModel = Just mm }, Cmd.map DetailMsg mc )
SubmitResp (Ok res) ->
( { model | result = Just res }
, Cmd.none
)
SubmitResp (Err err) ->
( { model | result = Just (BasicResult False (Util.Http.errorToString err)) }
, Cmd.none
)
DeleteResp (Ok res) ->
if res.success then
( { model | result = Nothing, detailModel = Nothing }
, initCmd flags
)
else
( { model | result = Just res }
, Cmd.none
)
DeleteResp (Err err) ->
( { model | result = Just (BasicResult False (Util.Http.errorToString err)) }
, Cmd.none
)
--- View
view : UiSettings -> Model -> Html Msg
view settings model =
div []
[ div [ class "ui menu" ]
[ a
[ class "link item"
, href "#"
, onClick NewTask
]
[ i [ class "add icon" ] []
, text "New Task"
]
]
, div
[ classList
[ ( "ui message", True )
, ( "error", Maybe.map .success model.result == Just False )
, ( "success", Maybe.map .success model.result == Just True )
, ( "invisible hidden", model.result == Nothing )
]
]
[ Maybe.map .message model.result
|> Maybe.withDefault ""
|> text
]
, case model.detailModel of
Just msett ->
viewForm settings msett
Nothing ->
viewList model
]
viewForm : UiSettings -> Comp.NotificationForm.Model -> Html Msg
viewForm settings model =
Html.map DetailMsg (Comp.NotificationForm.view "segment" settings model)
viewList : Model -> Html Msg
viewList model =
Html.map ListMsg (Comp.NotificationList.view model.listModel model.items)

View File

@ -8,7 +8,6 @@ module Comp.ScanMailboxList exposing
)
import Api.Model.ScanMailboxSettings exposing (ScanMailboxSettings)
import Api.Model.ScanMailboxSettingsList exposing (ScanMailboxSettingsList)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (onClick)
@ -41,7 +40,7 @@ update msg model =
view : Model -> List ScanMailboxSettings -> Html Msg
view model items =
view _ items =
div []
[ table [ class "ui very basic table" ]
[ thead []