Instead of client only, make bookmarks a server aware feature

Makes it much more useful
This commit is contained in:
eikek
2022-01-09 23:50:34 +01:00
parent 063ae56488
commit 9415f72ec0
19 changed files with 618 additions and 345 deletions

View File

@ -1,138 +0,0 @@
module Data.BookmarkedQuery exposing
( AllBookmarks
, BookmarkedQuery
, BookmarkedQueryDef
, Bookmarks
, Location(..)
, add
, allBookmarksEmpty
, bookmarksDecoder
, bookmarksEncode
, emptyBookmarks
, exists
, filter
, map
, remove
)
import Api.Model.ShareDetail exposing (ShareDetail)
import Json.Decode as D
import Json.Encode as E
type Location
= User
| Collective
type alias BookmarkedQuery =
{ name : String
, query : String
}
bookmarkedQueryDecoder : D.Decoder BookmarkedQuery
bookmarkedQueryDecoder =
D.map2 BookmarkedQuery
(D.field "name" D.string)
(D.field "query" D.string)
bookmarkedQueryEncode : BookmarkedQuery -> E.Value
bookmarkedQueryEncode bq =
E.object
[ ( "name", E.string bq.name )
, ( "query", E.string bq.query )
]
type alias BookmarkedQueryDef =
{ query : BookmarkedQuery
, location : Location
}
type Bookmarks
= Bookmarks (List BookmarkedQuery)
map : (BookmarkedQuery -> a) -> Bookmarks -> List a
map f bms =
case bms of
Bookmarks items ->
List.map f items
filter : (BookmarkedQuery -> Bool) -> Bookmarks -> Bookmarks
filter f bms =
case bms of
Bookmarks items ->
Bookmarks <| List.filter f items
emptyBookmarks : Bookmarks
emptyBookmarks =
Bookmarks []
type alias AllBookmarks =
{ collective : Bookmarks
, user : Bookmarks
, shares : List ShareDetail
}
allBookmarksEmpty : AllBookmarks
allBookmarksEmpty =
AllBookmarks emptyBookmarks emptyBookmarks []
{-| Checks wether a bookmark of this name already exists.
-}
exists : String -> Bookmarks -> Bool
exists name bookmarks =
case bookmarks of
Bookmarks list ->
List.any (\b -> b.name == name) list
remove : String -> Bookmarks -> Bookmarks
remove name bookmarks =
case bookmarks of
Bookmarks list ->
Bookmarks <| List.filter (\b -> b.name /= name) list
sortByName : Bookmarks -> Bookmarks
sortByName bm =
case bm of
Bookmarks all ->
Bookmarks <| List.sortBy .name all
add : BookmarkedQuery -> Bookmarks -> Bookmarks
add query bookmarks =
case remove query.name bookmarks of
Bookmarks all ->
sortByName (Bookmarks (query :: all))
bookmarksDecoder : D.Decoder Bookmarks
bookmarksDecoder =
D.maybe
(D.field "bookmarks"
(D.list bookmarkedQueryDecoder
|> D.map Bookmarks
|> D.map sortByName
)
)
|> D.map (Maybe.withDefault emptyBookmarks)
bookmarksEncode : Bookmarks -> E.Value
bookmarksEncode bookmarks =
case bookmarks of
Bookmarks all ->
E.object
[ ( "bookmarks", E.list bookmarkedQueryEncode all )
]

View File

@ -0,0 +1,48 @@
module Data.Bookmarks exposing
( AllBookmarks
, Bookmarks
, bookmarksDecoder
, empty
, exists
, sort
)
import Api.Model.BookmarkedQuery exposing (BookmarkedQuery)
import Api.Model.ShareDetail exposing (ShareDetail)
import Json.Decode as D
type alias AllBookmarks =
{ bookmarks : List BookmarkedQuery
, shares : List ShareDetail
}
empty : AllBookmarks
empty =
AllBookmarks [] []
type alias Bookmarks =
List BookmarkedQuery
{-| Checks wether a bookmark of this name already exists.
-}
exists : String -> Bookmarks -> Bool
exists name bookmarks =
List.any (\b -> b.name == name) bookmarks
sort : Bookmarks -> Bookmarks
sort bms =
let
labelName b =
Maybe.withDefault b.name b.label
in
List.sortBy labelName bms
bookmarksDecoder : D.Decoder Bookmarks
bookmarksDecoder =
D.list Api.Model.BookmarkedQuery.decoder