Change webapp to support multiple scan-mailbox tasks

This commit is contained in:
Eike Kettner
2020-05-21 20:50:40 +02:00
parent 9f9dd6c0fb
commit 920fcf28dd
8 changed files with 535 additions and 88 deletions

View File

@@ -0,0 +1,242 @@
module Comp.ScanMailboxManage exposing
( Model
, Msg
, init
, update
, view
)
import Api
import Api.Model.BasicResult exposing (BasicResult)
import Api.Model.ScanMailboxSettings exposing (ScanMailboxSettings)
import Api.Model.ScanMailboxSettingsList exposing (ScanMailboxSettingsList)
import Comp.ScanMailboxForm
import Comp.ScanMailboxList
import Data.Flags exposing (Flags)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (onClick)
import Http
import Util.Http
type alias Model =
{ listModel : Comp.ScanMailboxList.Model
, detailModel : Maybe Comp.ScanMailboxForm.Model
, items : List ScanMailboxSettings
, result : Maybe BasicResult
}
type Msg
= ListMsg Comp.ScanMailboxList.Msg
| DetailMsg Comp.ScanMailboxForm.Msg
| GetDataResp (Result Http.Error ScanMailboxSettingsList)
| NewTask
| SubmitResp (Result Http.Error BasicResult)
| DeleteResp (Result Http.Error BasicResult)
initModel : Model
initModel =
{ listModel = Comp.ScanMailboxList.init
, detailModel = Nothing
, items = []
, result = Nothing
}
initCmd : Flags -> Cmd Msg
initCmd flags =
Api.getScanMailbox 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.ScanMailboxList.update lm model.listModel
( detail, cmd ) =
case action of
Comp.ScanMailboxList.NoAction ->
( Nothing, Cmd.none )
Comp.ScanMailboxList.EditAction settings ->
let
( dm, dc ) =
Comp.ScanMailboxForm.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.ScanMailboxForm.update flags lm dm
( model_, cmd_ ) =
case action of
Comp.ScanMailboxForm.NoAction ->
( { model | detailModel = Just mm }
, Cmd.none
)
Comp.ScanMailboxForm.SubmitAction settings ->
( { model
| detailModel = Just mm
, result = Nothing
}
, if settings.id == "" then
Api.createScanMailbox flags settings SubmitResp
else
Api.updateScanMailbox flags settings SubmitResp
)
Comp.ScanMailboxForm.CancelAction ->
( { model
| detailModel = Nothing
, result = Nothing
}
, initCmd flags
)
Comp.ScanMailboxForm.StartOnceAction settings ->
( { model
| detailModel = Just mm
, result = Nothing
}
, Api.startOnceScanMailbox flags settings SubmitResp
)
Comp.ScanMailboxForm.DeleteAction id ->
( { model
| detailModel = Just mm
, result = Nothing
}
, Api.deleteScanMailbox flags id DeleteResp
)
in
( model_
, Cmd.batch
[ Cmd.map DetailMsg mc
, cmd_
]
)
Nothing ->
( model, Cmd.none )
NewTask ->
let
( mm, mc ) =
Comp.ScanMailboxForm.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 : Model -> Html Msg
view 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 settings ->
viewForm settings
Nothing ->
viewList model
]
viewForm : Comp.ScanMailboxForm.Model -> Html Msg
viewForm model =
Html.map DetailMsg (Comp.ScanMailboxForm.view "segment" model)
viewList : Model -> Html Msg
viewList model =
Html.map ListMsg (Comp.ScanMailboxList.view model.listModel model.items)