Finish mail settings

This commit is contained in:
Eike Kettner
2020-01-07 00:20:28 +01:00
parent f235f3a030
commit 32050a9faf
11 changed files with 391 additions and 57 deletions

View File

@ -4,6 +4,7 @@ module Comp.EmailSettingsForm exposing
, emptyModel
, getSettings
, init
, isValid
, update
, view
)
@ -40,7 +41,7 @@ emptyModel =
{ settings = Api.Model.EmailSettings.empty
, name = ""
, host = ""
, portField = Comp.IntField.init (Just 0) Nothing "SMTP Port"
, portField = Comp.IntField.init (Just 0) Nothing True "SMTP Port"
, portNum = Nothing
, user = Nothing
, passField = Comp.PasswordInput.init
@ -63,7 +64,7 @@ init ems =
{ settings = ems
, name = ems.name
, host = ems.smtpHost
, portField = Comp.IntField.init (Just 0) Nothing "SMTP Port"
, portField = Comp.IntField.init (Just 0) Nothing True "SMTP Port"
, portNum = ems.smtpPort
, user = ems.smtpUser
, passField = Comp.PasswordInput.init
@ -184,7 +185,7 @@ view model =
]
]
, div [ class "fields" ]
[ div [ class "fifteen wide required field" ]
[ div [ class "thirteen wide required field" ]
[ label [] [ text "SMTP Host" ]
, input
[ type_ "text"
@ -194,7 +195,11 @@ view model =
]
[]
]
, Html.map PortMsg (Comp.IntField.view model.portNum model.portField)
, Html.map PortMsg
(Comp.IntField.view model.portNum
"three wide field"
model.portField
)
]
, div [ class "two fields" ]
[ div [ class "field" ]

View File

@ -18,6 +18,8 @@ import Data.Flags exposing (Flags)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (onClick, onInput)
import Http
import Util.Http
type alias Model =
@ -43,9 +45,9 @@ emptyModel =
}
init : ( Model, Cmd Msg )
init =
( emptyModel, Cmd.none )
init : Flags -> ( Model, Cmd Msg )
init flags =
( emptyModel, Api.getMailSettings flags "" MailSettingsResp )
type ViewMode
@ -61,6 +63,10 @@ type Msg
| YesNoMsg Comp.YesNoDimmer.Msg
| RequestDelete
| SetViewMode ViewMode
| Submit
| SubmitResp (Result Http.Error BasicResult)
| LoadSettings
| MailSettingsResp (Result Http.Error EmailSettingsList)
update : Flags -> Msg -> Model -> ( Model, Cmd Msg )
@ -84,8 +90,27 @@ update flags msg model =
let
( tm, tc ) =
Comp.EmailSettingsTable.update m model.tableModel
m2 =
{ model
| tableModel = tm
, viewMode = Maybe.map (\_ -> Form) tm.selected |> Maybe.withDefault Table
, formError =
if tm.selected /= Nothing then
Nothing
else
model.formError
, formModel =
case tm.selected of
Just ems ->
Comp.EmailSettingsForm.init ems
Nothing ->
model.formModel
}
in
( { model | tableModel = tm }, Cmd.map TableMsg tc )
( m2, Cmd.map TableMsg tc )
FormMsg m ->
let
@ -95,21 +120,84 @@ update flags msg model =
( { model | formModel = fm }, Cmd.map FormMsg fc )
SetQuery str ->
( { model | query = str }, Cmd.none )
let
m =
{ model | query = str }
in
( m, Api.getMailSettings flags str MailSettingsResp )
YesNoMsg m ->
let
( dm, flag ) =
Comp.YesNoDimmer.update m model.deleteConfirm
( mid, _ ) =
Comp.EmailSettingsForm.getSettings model.formModel
cmd =
case ( flag, mid ) of
( True, Just name ) ->
Api.deleteMailSettings flags name SubmitResp
_ ->
Cmd.none
in
( { model | deleteConfirm = dm }, Cmd.none )
( { model | deleteConfirm = dm }, cmd )
RequestDelete ->
( model, Cmd.none )
update flags (YesNoMsg Comp.YesNoDimmer.activate) model
SetViewMode m ->
( { model | viewMode = m }, Cmd.none )
Submit ->
let
( mid, ems ) =
Comp.EmailSettingsForm.getSettings model.formModel
valid =
Comp.EmailSettingsForm.isValid model.formModel
in
if valid then
( { model | loading = True }, Api.createMailSettings flags mid ems SubmitResp )
else
( { model | formError = Just "Please fill required fields." }, Cmd.none )
LoadSettings ->
( { model | loading = True }, Api.getMailSettings flags model.query MailSettingsResp )
SubmitResp (Ok res) ->
if res.success then
let
( m2, c2 ) =
update flags (SetViewMode Table) model
( m3, c3 ) =
update flags LoadSettings m2
in
( { m3 | loading = False }, Cmd.batch [ c2, c3 ] )
else
( { model | formError = Just res.message, loading = False }, Cmd.none )
SubmitResp (Err err) ->
( { model | formError = Just (Util.Http.errorToString err), loading = False }, Cmd.none )
MailSettingsResp (Ok ems) ->
let
m2 =
{ model
| viewMode = Table
, loading = False
, tableModel = Comp.EmailSettingsTable.init ems.items
}
in
( m2, Cmd.none )
MailSettingsResp (Err _) ->
( { model | loading = False }, Cmd.none )
view : Model -> Html Msg
view model =
@ -171,10 +259,18 @@ viewForm model =
[ Maybe.withDefault "" model.formError |> text
]
, div [ class "ui divider" ] []
, button [ class "ui primary button" ]
, button
[ class "ui primary button"
, onClick Submit
, href "#"
]
[ text "Submit"
]
, a [ class "ui secondary button", onClick (SetViewMode Table), href "" ]
, a
[ class "ui secondary button"
, onClick (SetViewMode Table)
, href ""
]
[ text "Cancel"
]
, if model.formModel.settings.name /= "" then

View File

@ -2,6 +2,7 @@ module Comp.EmailSettingsTable exposing
( Model
, Msg
, emptyModel
, init
, update
, view
)
@ -9,10 +10,12 @@ module Comp.EmailSettingsTable exposing
import Api.Model.EmailSettings exposing (EmailSettings)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (onClick)
type alias Model =
{ emailSettings : List EmailSettings
, selected : Maybe EmailSettings
}
@ -24,6 +27,7 @@ emptyModel =
init : List EmailSettings -> Model
init ems =
{ emailSettings = ems
, selected = Nothing
}
@ -33,17 +37,40 @@ type Msg
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
( model, Cmd.none )
case msg of
Select ems ->
( { model | selected = Just ems }, Cmd.none )
view : Model -> Html Msg
view model =
table [ class "ui table" ]
table [ class "ui selectable pointer table" ]
[ thead []
[ th [] [ text "Name" ]
[ th [ class "collapsible" ] [ text "Name" ]
, th [] [ text "Host/Port" ]
, th [] [ text "From" ]
]
, tbody []
[]
(List.map (renderLine model) model.emailSettings)
]
renderLine : Model -> EmailSettings -> Html Msg
renderLine model ems =
let
hostport =
case ems.smtpPort of
Just p ->
ems.smtpHost ++ ":" ++ String.fromInt p
Nothing ->
ems.smtpHost
in
tr
[ classList [ ( "active", model.selected == Just ems ) ]
, onClick (Select ems)
]
[ td [ class "collapsible" ] [ text ems.name ]
, td [] [ text hostport ]
, td [] [ text ems.from ]
]

View File

@ -11,6 +11,7 @@ type alias Model =
, label : String
, error : Maybe String
, lastInput : String
, optional : Bool
}
@ -18,26 +19,27 @@ type Msg
= SetValue String
init : Maybe Int -> Maybe Int -> String -> Model
init min max label =
init : Maybe Int -> Maybe Int -> Bool -> String -> Model
init min max opt label =
{ min = min
, max = max
, label = label
, error = Nothing
, lastInput = ""
, optional = opt
}
tooLow : Model -> Int -> Bool
tooLow model n =
Maybe.map ((<) n) model.min
|> Maybe.withDefault False
|> Maybe.withDefault (not model.optional)
tooHigh : Model -> Int -> Bool
tooHigh model n =
Maybe.map ((>) n) model.max
|> Maybe.withDefault False
|> Maybe.withDefault (not model.optional)
update : Msg -> Model -> ( Model, Maybe Int )
@ -75,16 +77,20 @@ update msg model =
( { m | error = Nothing }, Just n )
Nothing ->
( { m | error = Just ("'" ++ str ++ "' is not a valid number!") }
, Nothing
)
if model.optional && String.trim str == "" then
( { m | error = Nothing }, Nothing )
else
( { m | error = Just ("'" ++ str ++ "' is not a valid number!") }
, Nothing
)
view : Maybe Int -> Model -> Html Msg
view nval model =
view : Maybe Int -> String -> Model -> Html Msg
view nval classes model =
div
[ classList
[ ( "field", True )
[ ( classes, True )
, ( "error", model.error /= Nothing )
]
]