mirror of
https://github.com/TheAnachronism/docspell.git
synced 2025-06-23 10:58:26 +00:00
Manage bookmarks
This commit is contained in:
399
modules/webapp/src/main/elm/Comp/BookmarkManage.elm
Normal file
399
modules/webapp/src/main/elm/Comp/BookmarkManage.elm
Normal file
@ -0,0 +1,399 @@
|
||||
{-
|
||||
Copyright 2020 Eike K. & Contributors
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
-}
|
||||
|
||||
|
||||
module Comp.BookmarkManage exposing (Model, Msg, init, loadBookmarks, update, view)
|
||||
|
||||
import Api
|
||||
import Api.Model.BasicResult exposing (BasicResult)
|
||||
import Comp.Basic as B
|
||||
import Comp.BookmarkQueryForm
|
||||
import Comp.BookmarkTable
|
||||
import Comp.ItemDetail.Model exposing (Msg(..))
|
||||
import Comp.MenuBar as MB
|
||||
import Data.BookmarkedQuery exposing (AllBookmarks)
|
||||
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 Messages.Comp.BookmarkManage exposing (Texts)
|
||||
import Page exposing (Page(..))
|
||||
import Styles as S
|
||||
|
||||
|
||||
type FormError
|
||||
= FormErrorNone
|
||||
| FormErrorHttp Http.Error
|
||||
| FormErrorInvalid
|
||||
| FormErrorSubmit String
|
||||
|
||||
|
||||
type ViewMode
|
||||
= Table
|
||||
| Form
|
||||
|
||||
|
||||
type DeleteConfirm
|
||||
= DeleteConfirmOff
|
||||
| DeleteConfirmOn
|
||||
|
||||
|
||||
type alias FormData =
|
||||
{ model : Comp.BookmarkQueryForm.Model
|
||||
, oldName : Maybe String
|
||||
}
|
||||
|
||||
|
||||
type alias Model =
|
||||
{ viewMode : ViewMode
|
||||
, bookmarks : AllBookmarks
|
||||
, formData : FormData
|
||||
, loading : Bool
|
||||
, formError : FormError
|
||||
, deleteConfirm : DeleteConfirm
|
||||
}
|
||||
|
||||
|
||||
init : Flags -> ( Model, Cmd Msg )
|
||||
init flags =
|
||||
let
|
||||
( fm, fc ) =
|
||||
Comp.BookmarkQueryForm.init
|
||||
in
|
||||
( { viewMode = Table
|
||||
, bookmarks = Data.BookmarkedQuery.allBookmarksEmpty
|
||||
, formData =
|
||||
{ model = fm
|
||||
, oldName = Nothing
|
||||
}
|
||||
, loading = False
|
||||
, formError = FormErrorNone
|
||||
, deleteConfirm = DeleteConfirmOff
|
||||
}
|
||||
, Cmd.batch
|
||||
[ Cmd.map FormMsg fc
|
||||
, Api.getBookmarks flags LoadBookmarksResp
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
type Msg
|
||||
= LoadBookmarks
|
||||
| TableMsg Data.BookmarkedQuery.Location Comp.BookmarkTable.Msg
|
||||
| FormMsg Comp.BookmarkQueryForm.Msg
|
||||
| InitNewBookmark
|
||||
| SetViewMode ViewMode
|
||||
| Submit
|
||||
| RequestDelete
|
||||
| CancelDelete
|
||||
| DeleteBookmarkNow Data.BookmarkedQuery.Location String
|
||||
| LoadBookmarksResp (Result Http.Error AllBookmarks)
|
||||
| AddBookmarkResp (Result Http.Error BasicResult)
|
||||
| UpdateBookmarkResp (Result Http.Error BasicResult)
|
||||
| DeleteBookmarkResp (Result Http.Error BasicResult)
|
||||
|
||||
|
||||
loadBookmarks : Msg
|
||||
loadBookmarks =
|
||||
LoadBookmarks
|
||||
|
||||
|
||||
|
||||
--- update
|
||||
|
||||
|
||||
update : Flags -> Msg -> Model -> ( Model, Cmd Msg, Sub Msg )
|
||||
update flags msg model =
|
||||
case msg of
|
||||
InitNewBookmark ->
|
||||
let
|
||||
( bm, bc ) =
|
||||
Comp.BookmarkQueryForm.init
|
||||
|
||||
nm =
|
||||
{ model
|
||||
| viewMode = Form
|
||||
, formError = FormErrorNone
|
||||
, formData =
|
||||
{ model = bm, oldName = Nothing }
|
||||
}
|
||||
in
|
||||
( nm, Cmd.map FormMsg bc, Sub.none )
|
||||
|
||||
SetViewMode vm ->
|
||||
( { model | viewMode = vm, formError = FormErrorNone }
|
||||
, if vm == Table then
|
||||
Api.getBookmarks flags LoadBookmarksResp
|
||||
|
||||
else
|
||||
Cmd.none
|
||||
, Sub.none
|
||||
)
|
||||
|
||||
FormMsg lm ->
|
||||
let
|
||||
( fm, fc, fs ) =
|
||||
Comp.BookmarkQueryForm.update flags lm model.formData.model
|
||||
in
|
||||
( { model | formData = { model = fm, oldName = model.formData.oldName }, formError = FormErrorNone }
|
||||
, Cmd.map FormMsg fc
|
||||
, Sub.map FormMsg fs
|
||||
)
|
||||
|
||||
TableMsg loc lm ->
|
||||
let
|
||||
action =
|
||||
Comp.BookmarkTable.update lm
|
||||
in
|
||||
case action of
|
||||
Comp.BookmarkTable.Edit bookmark ->
|
||||
let
|
||||
( bm, bc ) =
|
||||
Comp.BookmarkQueryForm.initWith
|
||||
{ query = bookmark
|
||||
, location = loc
|
||||
}
|
||||
in
|
||||
( { model
|
||||
| viewMode = Form
|
||||
, formError = FormErrorNone
|
||||
, formData = { model = bm, oldName = Just bookmark.name }
|
||||
}
|
||||
, Cmd.map FormMsg bc
|
||||
, Sub.none
|
||||
)
|
||||
|
||||
RequestDelete ->
|
||||
( { model | deleteConfirm = DeleteConfirmOn }, Cmd.none, Sub.none )
|
||||
|
||||
CancelDelete ->
|
||||
( { model | deleteConfirm = DeleteConfirmOff }, Cmd.none, Sub.none )
|
||||
|
||||
DeleteBookmarkNow loc name ->
|
||||
( { model | deleteConfirm = DeleteConfirmOff, loading = True }
|
||||
, Api.deleteBookmark flags loc name DeleteBookmarkResp
|
||||
, Sub.none
|
||||
)
|
||||
|
||||
LoadBookmarks ->
|
||||
( { model | loading = True }
|
||||
, Api.getBookmarks flags LoadBookmarksResp
|
||||
, Sub.none
|
||||
)
|
||||
|
||||
LoadBookmarksResp (Ok list) ->
|
||||
( { model | loading = False, bookmarks = list, formError = FormErrorNone }
|
||||
, Cmd.none
|
||||
, Sub.none
|
||||
)
|
||||
|
||||
LoadBookmarksResp (Err err) ->
|
||||
( { model | loading = False, formError = FormErrorHttp err }, Cmd.none, Sub.none )
|
||||
|
||||
Submit ->
|
||||
case Comp.BookmarkQueryForm.get model.formData.model of
|
||||
Just data ->
|
||||
case model.formData.oldName of
|
||||
Just prevName ->
|
||||
( { model | loading = True }, Api.updateBookmark flags prevName data AddBookmarkResp, Sub.none )
|
||||
|
||||
Nothing ->
|
||||
( { model | loading = True }, Api.addBookmark flags data AddBookmarkResp, Sub.none )
|
||||
|
||||
Nothing ->
|
||||
( { model | formError = FormErrorInvalid }, Cmd.none, Sub.none )
|
||||
|
||||
AddBookmarkResp (Ok res) ->
|
||||
if res.success then
|
||||
( { model | loading = True, viewMode = Table }, Api.getBookmarks flags LoadBookmarksResp, Sub.none )
|
||||
|
||||
else
|
||||
( { model | loading = False, formError = FormErrorSubmit res.message }, Cmd.none, Sub.none )
|
||||
|
||||
AddBookmarkResp (Err err) ->
|
||||
( { model | loading = False, formError = FormErrorHttp err }, Cmd.none, Sub.none )
|
||||
|
||||
UpdateBookmarkResp (Ok res) ->
|
||||
if res.success then
|
||||
( model, Api.getBookmarks flags LoadBookmarksResp, Sub.none )
|
||||
|
||||
else
|
||||
( { model | loading = False, formError = FormErrorSubmit res.message }, Cmd.none, Sub.none )
|
||||
|
||||
UpdateBookmarkResp (Err err) ->
|
||||
( { model | loading = False, formError = FormErrorHttp err }, Cmd.none, Sub.none )
|
||||
|
||||
DeleteBookmarkResp (Ok res) ->
|
||||
if res.success then
|
||||
update flags (SetViewMode Table) { model | loading = False }
|
||||
|
||||
else
|
||||
( { model | formError = FormErrorSubmit res.message, loading = False }, Cmd.none, Sub.none )
|
||||
|
||||
DeleteBookmarkResp (Err err) ->
|
||||
( { model | formError = FormErrorHttp err, loading = False }, Cmd.none, Sub.none )
|
||||
|
||||
|
||||
|
||||
--- view
|
||||
|
||||
|
||||
view : Texts -> UiSettings -> Flags -> Model -> Html Msg
|
||||
view texts settings flags model =
|
||||
if model.viewMode == Table then
|
||||
viewTable texts model
|
||||
|
||||
else
|
||||
viewForm texts settings flags model
|
||||
|
||||
|
||||
viewTable : Texts -> Model -> Html Msg
|
||||
viewTable texts model =
|
||||
div [ class "flex flex-col" ]
|
||||
[ MB.view
|
||||
{ start =
|
||||
[]
|
||||
, end =
|
||||
[ MB.PrimaryButton
|
||||
{ tagger = InitNewBookmark
|
||||
, title = texts.createNewBookmark
|
||||
, icon = Just "fa fa-plus"
|
||||
, label = texts.newBookmark
|
||||
}
|
||||
]
|
||||
, rootClasses = "mb-4"
|
||||
}
|
||||
, div [ class "flex flex-col" ]
|
||||
[ h3 [ class S.header3 ]
|
||||
[ text texts.userBookmarks ]
|
||||
, Html.map (TableMsg Data.BookmarkedQuery.User)
|
||||
(Comp.BookmarkTable.view texts.bookmarkTable model.bookmarks.user)
|
||||
]
|
||||
, div [ class "flex flex-col mt-3" ]
|
||||
[ h3 [ class S.header3 ]
|
||||
[ text texts.collectiveBookmarks ]
|
||||
, Html.map (TableMsg Data.BookmarkedQuery.Collective)
|
||||
(Comp.BookmarkTable.view texts.bookmarkTable model.bookmarks.collective)
|
||||
]
|
||||
, B.loadingDimmer
|
||||
{ label = ""
|
||||
, active = model.loading
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
viewForm : Texts -> UiSettings -> Flags -> Model -> Html Msg
|
||||
viewForm texts _ _ model =
|
||||
let
|
||||
newBookmark =
|
||||
model.formData.oldName == Nothing
|
||||
|
||||
isValid =
|
||||
Comp.BookmarkQueryForm.get model.formData.model /= Nothing
|
||||
in
|
||||
div []
|
||||
[ Html.form []
|
||||
[ if newBookmark then
|
||||
h1 [ class S.header2 ]
|
||||
[ text texts.createNewBookmark
|
||||
]
|
||||
|
||||
else
|
||||
h1 [ class S.header2 ]
|
||||
[ text (Maybe.withDefault "" model.formData.model.name)
|
||||
]
|
||||
, MB.view
|
||||
{ start =
|
||||
[ MB.CustomElement <|
|
||||
B.primaryButton
|
||||
{ handler = onClick Submit
|
||||
, title = texts.basics.submitThisForm
|
||||
, icon = "fa fa-save"
|
||||
, label = texts.basics.submit
|
||||
, disabled = not isValid
|
||||
, attrs = [ href "#" ]
|
||||
}
|
||||
, MB.SecondaryButton
|
||||
{ tagger = SetViewMode Table
|
||||
, title = texts.basics.backToList
|
||||
, icon = Just "fa fa-arrow-left"
|
||||
, label = texts.basics.cancel
|
||||
}
|
||||
]
|
||||
, end =
|
||||
if not newBookmark then
|
||||
[ MB.DeleteButton
|
||||
{ tagger = RequestDelete
|
||||
, title = texts.deleteThisBookmark
|
||||
, icon = Just "fa fa-trash"
|
||||
, label = texts.basics.delete
|
||||
}
|
||||
]
|
||||
|
||||
else
|
||||
[]
|
||||
, rootClasses = "mb-4"
|
||||
}
|
||||
, div
|
||||
[ classList
|
||||
[ ( "hidden", model.formError == FormErrorNone )
|
||||
]
|
||||
, class "my-2"
|
||||
, class S.errorMessage
|
||||
]
|
||||
[ case model.formError of
|
||||
FormErrorNone ->
|
||||
text ""
|
||||
|
||||
FormErrorHttp err ->
|
||||
text (texts.httpError err)
|
||||
|
||||
FormErrorInvalid ->
|
||||
text texts.correctFormErrors
|
||||
|
||||
FormErrorSubmit m ->
|
||||
text m
|
||||
]
|
||||
, div []
|
||||
[ Html.map FormMsg (Comp.BookmarkQueryForm.view texts.bookmarkForm model.formData.model)
|
||||
]
|
||||
, B.loadingDimmer
|
||||
{ active = model.loading
|
||||
, label = texts.basics.loading
|
||||
}
|
||||
, B.contentDimmer
|
||||
(model.deleteConfirm == DeleteConfirmOn)
|
||||
(div [ class "flex flex-col" ]
|
||||
[ div [ class "text-lg" ]
|
||||
[ i [ class "fa fa-info-circle mr-2" ] []
|
||||
, text texts.reallyDeleteBookmark
|
||||
]
|
||||
, div [ class "mt-4 flex flex-row items-center" ]
|
||||
[ B.deleteButton
|
||||
{ label = texts.basics.yes
|
||||
, icon = "fa fa-check"
|
||||
, disabled = False
|
||||
, handler =
|
||||
onClick
|
||||
(DeleteBookmarkNow model.formData.model.location
|
||||
(Maybe.withDefault "" model.formData.model.name)
|
||||
)
|
||||
, attrs = [ href "#" ]
|
||||
}
|
||||
, B.secondaryButton
|
||||
{ label = texts.basics.no
|
||||
, icon = "fa fa-times"
|
||||
, disabled = False
|
||||
, handler = onClick CancelDelete
|
||||
, attrs = [ href "#", class "ml-2" ]
|
||||
}
|
||||
]
|
||||
]
|
||||
)
|
||||
]
|
||||
]
|
@ -5,7 +5,7 @@
|
||||
-}
|
||||
|
||||
|
||||
module Comp.BookmarkQueryForm exposing (Model, Msg, get, init, initQuery, update, view)
|
||||
module Comp.BookmarkQueryForm exposing (Model, Msg, get, init, initQuery, initWith, update, view)
|
||||
|
||||
import Api
|
||||
import Comp.Basic as B
|
||||
@ -57,6 +57,20 @@ init =
|
||||
initQuery ""
|
||||
|
||||
|
||||
initWith : BookmarkedQueryDef -> ( Model, Cmd Msg )
|
||||
initWith bm =
|
||||
let
|
||||
( m, c ) =
|
||||
initQuery bm.query.query
|
||||
in
|
||||
( { m
|
||||
| name = Just bm.query.name
|
||||
, location = bm.location
|
||||
}
|
||||
, c
|
||||
)
|
||||
|
||||
|
||||
isValid : Model -> Bool
|
||||
isValid model =
|
||||
Comp.PowerSearchInput.isValid model.queryModel
|
||||
|
67
modules/webapp/src/main/elm/Comp/BookmarkTable.elm
Normal file
67
modules/webapp/src/main/elm/Comp/BookmarkTable.elm
Normal file
@ -0,0 +1,67 @@
|
||||
{-
|
||||
Copyright 2020 Eike K. & Contributors
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
-}
|
||||
|
||||
|
||||
module Comp.BookmarkTable exposing
|
||||
( Msg(..)
|
||||
, SelectAction(..)
|
||||
, update
|
||||
, view
|
||||
)
|
||||
|
||||
import Comp.Basic as B
|
||||
import Data.BookmarkedQuery exposing (BookmarkedQuery, Bookmarks)
|
||||
import Html exposing (..)
|
||||
import Html.Attributes exposing (..)
|
||||
import Messages.Comp.BookmarkTable exposing (Texts)
|
||||
import Styles as S
|
||||
|
||||
|
||||
type Msg
|
||||
= Select BookmarkedQuery
|
||||
|
||||
|
||||
type SelectAction
|
||||
= Edit BookmarkedQuery
|
||||
|
||||
|
||||
update : Msg -> SelectAction
|
||||
update msg =
|
||||
case msg of
|
||||
Select share ->
|
||||
Edit share
|
||||
|
||||
|
||||
|
||||
--- View
|
||||
|
||||
|
||||
view : Texts -> Bookmarks -> Html Msg
|
||||
view texts bms =
|
||||
table [ class S.tableMain ]
|
||||
[ thead []
|
||||
[ tr []
|
||||
[ th [ class "" ] []
|
||||
, th [ class "text-left" ]
|
||||
[ text texts.basics.name
|
||||
]
|
||||
]
|
||||
]
|
||||
, tbody []
|
||||
(Data.BookmarkedQuery.map (renderBookmarkLine texts) bms)
|
||||
]
|
||||
|
||||
|
||||
renderBookmarkLine : Texts -> BookmarkedQuery -> Html Msg
|
||||
renderBookmarkLine texts bm =
|
||||
tr
|
||||
[ class S.tableRow
|
||||
]
|
||||
[ B.editLinkTableCell texts.basics.edit (Select bm)
|
||||
, td [ class "text-left py-4 md:py-2" ]
|
||||
[ text bm.name
|
||||
]
|
||||
]
|
Reference in New Issue
Block a user