Prepare sending mail

This commit is contained in:
Eike Kettner
2020-01-08 22:44:34 +01:00
parent 51ce48997c
commit 7a3289c41d
12 changed files with 268 additions and 12 deletions

View File

@ -40,6 +40,7 @@ module Api exposing
, putUser
, refreshSession
, register
, sendMail
, setCollectiveSettings
, setConcEquip
, setConcPerson
@ -86,6 +87,7 @@ import Api.Model.Person exposing (Person)
import Api.Model.PersonList exposing (PersonList)
import Api.Model.ReferenceList exposing (ReferenceList)
import Api.Model.Registration exposing (Registration)
import Api.Model.SimpleMail exposing (SimpleMail)
import Api.Model.Source exposing (Source)
import Api.Model.SourceList exposing (SourceList)
import Api.Model.Tag exposing (Tag)
@ -105,6 +107,24 @@ import Util.Http as Http2
--- Mail Send
sendMail :
Flags
-> { conn : String, item : String, mail : SimpleMail }
-> (Result Http.Error BasicResult -> msg)
-> Cmd msg
sendMail flags opts receive =
Http2.authPost
{ url = flags.config.baseUrl ++ "/api/v1/sec/email/send/" ++ opts.conn ++ "/" ++ opts.item
, account = getAccount flags
, body = Http.jsonBody (Api.Model.SimpleMail.encode opts.mail)
, expect = Http.expectJson receive Api.Model.BasicResult.decoder
}
--- Mail Settings

View File

@ -33,6 +33,7 @@ import Html.Events exposing (onClick, onInput)
import Http
import Markdown
import Page exposing (Page(..))
import Util.Http
import Util.Maybe
import Util.Size
import Util.String
@ -60,6 +61,7 @@ type alias Model =
, dueDatePicker : DatePicker
, itemMail : Comp.ItemMail.Model
, mailOpen : Bool
, mailSendResult : Maybe BasicResult
}
@ -121,6 +123,7 @@ emptyModel =
, dueDatePicker = Comp.DatePicker.emptyModel
, itemMail = Comp.ItemMail.emptyModel
, mailOpen = False
, mailSendResult = Nothing
}
@ -165,6 +168,7 @@ type Msg
| RemoveDate
| ItemMailMsg Comp.ItemMail.Msg
| ToggleMail
| SendMailResp (Result Http.Error BasicResult)
@ -714,16 +718,49 @@ update key flags next msg model =
( { model
| itemMail = Comp.ItemMail.clear im
, mailOpen = False
, mailSendResult = Nothing
}
, Cmd.none
)
Comp.ItemMail.FormSend sm ->
Debug.todo "implement send"
let
mail =
{ item = model.item.id
, mail = sm.mail
, conn = sm.conn
}
in
( model, Api.sendMail flags mail SendMailResp )
ToggleMail ->
( { model | mailOpen = not model.mailOpen }, Cmd.none )
SendMailResp (Ok br) ->
let
mm =
if br.success then
Comp.ItemMail.clear model.itemMail
else
model.itemMail
in
( { model
| itemMail = mm
, mailSendResult = Just br
}
, Cmd.none
)
SendMailResp (Err err) ->
let
errmsg =
Util.Http.errorToString err
in
( { model | mailSendResult = Just (BasicResult False errmsg) }
, Cmd.none
)
-- view
@ -793,7 +830,7 @@ view inav model =
, onClick ToggleMail
, href "#"
]
[ i [ class "mail icon" ] []
[ i [ class "mail outline icon" ] []
]
]
, renderMailForm model
@ -1258,4 +1295,23 @@ renderMailForm model =
[ text "Send this item via E-Mail"
]
, Html.map ItemMailMsg (Comp.ItemMail.view model.itemMail)
, div
[ classList
[ ( "ui message", True )
, ( "error"
, Maybe.map .success model.mailSendResult
|> Maybe.map not
|> Maybe.withDefault False
)
, ( "success"
, Maybe.map .success model.mailSendResult
|> Maybe.withDefault False
)
, ( "invisible hidden", model.mailSendResult == Nothing )
]
]
[ Maybe.map .message model.mailSendResult
|> Maybe.withDefault ""
|> text
]
]

View File

@ -42,8 +42,14 @@ type Msg
| Send
type alias MailInfo =
{ conn : String
, mail : SimpleMail
}
type FormAction
= FormSend SimpleMail
= FormSend MailInfo
| FormCancel
| FormNone
@ -132,14 +138,19 @@ update msg model =
( model, FormCancel )
Send ->
let
rec =
String.split "," model.receiver
case ( model.formError, Comp.Dropdown.getSelected model.connectionModel ) of
( Nothing, conn :: [] ) ->
let
rec =
String.split "," model.receiver
sm =
SimpleMail rec model.subject model.body model.attachAll []
in
( model, FormSend sm )
sm =
SimpleMail rec model.subject model.body model.attachAll []
in
( model, FormSend { conn = conn, mail = sm } )
_ ->
( model, FormNone )
isValid : Model -> Bool