Commit 11f5e8ff authored by Przemyslaw Kaminski's avatar Przemyslaw Kaminski

[DocTable] remove docs immediately

parent dfb8f679
...@@ -30,7 +30,7 @@ import React (ReactClass, ReactElement, Children) ...@@ -30,7 +30,7 @@ import React (ReactClass, ReactElement, Children)
------------------------------------------------------------------------ ------------------------------------------------------------------------
import Gargantext.Prelude import Gargantext.Prelude
import Gargantext.Config (End(..), NodeType(..), OrderBy(..), Path(..), TabType, toUrl, toLink) import Gargantext.Config (End(..), NodeType(..), OrderBy(..), Path(..), TabType, toUrl, toLink)
import Gargantext.Config.REST (get, put, post, deleteWithBody) import Gargantext.Config.REST (get, put, post, deleteWithBody, delete)
import Gargantext.Components.Loader as Loader import Gargantext.Components.Loader as Loader
import Gargantext.Components.Node (NodePoly(..)) import Gargantext.Components.Node (NodePoly(..))
import Gargantext.Components.Table as T import Gargantext.Components.Table as T
...@@ -56,25 +56,23 @@ type Props = ...@@ -56,25 +56,23 @@ type Props =
} }
type State = type State =
{ documentIdsToDelete :: Set Int { documentIdsDeleted :: Set Int
, documentIdsDeleted :: Set Int
, localFavorites :: Map Int Boolean , localFavorites :: Map Int Boolean
} }
initialState :: State initialState :: State
initialState = initialState =
{ documentIdsToDelete: mempty { documentIdsDeleted: mempty
, documentIdsDeleted: mempty
, localFavorites: mempty , localFavorites: mempty
} }
_documentIdsToDelete = prop (SProxy :: SProxy "documentIdsToDelete")
_documentIdsDeleted = prop (SProxy :: SProxy "documentIdsDeleted") _documentIdsDeleted = prop (SProxy :: SProxy "documentIdsDeleted")
_localFavorites = prop (SProxy :: SProxy "localFavorites") _localFavorites = prop (SProxy :: SProxy "localFavorites")
data Action data Action
= MarkFavorites Int Boolean = MarkFavorites Int Boolean
| ToggleDocumentToDelete Int -- | ToggleDocumentToDelete Int
| ToggleDeleteDocument Int
| Trash | Trash
newtype DocumentsView newtype DocumentsView
...@@ -165,15 +163,16 @@ layoutDocview = simpleSpec performAction render ...@@ -165,15 +163,16 @@ layoutDocview = simpleSpec performAction render
performAction (MarkFavorites nid fav) {nodeId} _ = do performAction (MarkFavorites nid fav) {nodeId} _ = do
modifyState_ $ _localFavorites <<< at nid ?~ fav modifyState_ $ _localFavorites <<< at nid ?~ fav
void $ lift $ if fav void $ lift $ if fav
then putFavorites nodeId (FavoriteQuery {favorites: [nid]}) then putFavorites nodeId $ FavoriteQuery {favorites: [nid]}
else deleteFavorites nodeId (FavoriteQuery {favorites: [nid]}) else deleteFavorites nodeId $ FavoriteQuery {favorites: [nid]}
performAction (ToggleDocumentToDelete nid) _ _ = performAction (ToggleDeleteDocument nid) {nodeId} {documentIdsDeleted} = do
modifyState_ \state -> state {documentIdsToDelete = toggleSet nid state.documentIdsToDelete} modifyState_ $ _ {documentIdsDeleted = toggleSet nid documentIdsDeleted}
performAction Trash {nodeId} {documentIdsToDelete} = do void $ lift $ if (Set.member nid documentIdsDeleted)
void $ lift $ deleteDocuments nodeId (DeleteDocumentQuery {documents: Set.toUnfoldable documentIdsToDelete}) then deleteDocuments nodeId $ DocumentQuery {documents: [nid]}
modifyState_ $ else putDocuments nodeId $ DocumentQuery {documents: [nid]}
(_documentIdsToDelete .~ mempty) >>> performAction Trash {nodeId} {documentIdsDeleted} = do
(_documentIdsDeleted <>~ documentIdsToDelete) ids <- lift $ deleteAllDocuments nodeId
modifyState_ $ _ {documentIdsDeleted = Set.union documentIdsDeleted $ Set.fromFoldable ids}
render :: Render State Props Action render :: Render State Props Action
render dispatch {nodeId, tabType, listId, corpusId, totalRecords, chart} deletionState _ = render dispatch {nodeId, tabType, listId, corpusId, totalRecords, chart} deletionState _ =
...@@ -271,7 +270,7 @@ renderPage :: forall props path. ...@@ -271,7 +270,7 @@ renderPage :: forall props path.
(Loader.Action PageParams) (Loader.Action PageParams)
renderPage _ _ {loaded: Nothing} _ = [] -- TODO loading spinner renderPage _ _ {loaded: Nothing} _ = [] -- TODO loading spinner
renderPage loaderDispatch { totalRecords, dispatch, listId, corpusId renderPage loaderDispatch { totalRecords, dispatch, listId, corpusId
, deletionState: {documentIdsToDelete, documentIdsDeleted, localFavorites}} , deletionState: {documentIdsDeleted, localFavorites}}
{currentPath: {nodeId, tabType}, loaded: Just res} _ = {currentPath: {nodeId, tabType}, loaded: Just res} _ =
[ T.tableElt [ T.tableElt
{ rows { rows
...@@ -291,32 +290,31 @@ renderPage loaderDispatch { totalRecords, dispatch, listId, corpusId ...@@ -291,32 +290,31 @@ renderPage loaderDispatch { totalRecords, dispatch, listId, corpusId
where where
gi true = "glyphicon glyphicon-star" gi true = "glyphicon glyphicon-star"
gi false = "glyphicon glyphicon-star-empty" gi false = "glyphicon glyphicon-star-empty"
toDelete (DocumentsView {_id}) = Set.member _id documentIdsToDelete
isDeleted (DocumentsView {_id}) = Set.member _id documentIdsDeleted isDeleted (DocumentsView {_id}) = Set.member _id documentIdsDeleted
isFavorite {_id,fav} = maybe fav identity (localFavorites ^. at _id) isFavorite {_id,fav} = maybe fav identity (localFavorites ^. at _id)
corpusDocument (Just corpusId) = R.CorpusDocument corpusId corpusDocument (Just corpusId) = R.CorpusDocument corpusId
corpusDocument _ = R.Document corpusDocument _ = R.Document
rows = (\(DocumentsView r) -> rows = (\(DocumentsView r) ->
let isFav = isFavorite r let isFav = isFavorite r
toDel = toDelete $ DocumentsView r in isDel = isDeleted $ DocumentsView r in
{ row: { row:
[ div [] [ div []
[ a [ className $ gi isFav [ a [ className $ gi isFav
, if toDel then style {textDecoration : "line-through"} , if isDel then style {textDecoration : "line-through"}
else style {textDecoration : "none"} else style {textDecoration : "none"}
, onClick $ (\_-> dispatch $ MarkFavorites r._id (not isFav)) , onClick $ (\_-> dispatch $ MarkFavorites r._id (not isFav))
] [] ] []
] ]
, input [ _type "checkbox" , input [ _type "checkbox"
, checked toDel , checked isDel
, onClick $ (\_ -> dispatch $ ToggleDocumentToDelete r._id) , onClick $ (\_ -> dispatch $ ToggleDeleteDocument r._id)
] ]
-- TODO show date: Year-Month-Day only -- TODO show date: Year-Month-Day only
, if toDel then , if isDel then
div [ style {textDecoration : "line-through"}][text (show r.date)] div [ style {textDecoration : "line-through"}][text (show r.date)]
else else
div [ ][text (show r.date)] div [ ][text (show r.date)]
, if toDel then , if isDel then
a [ href (toLink $ (corpusDocument corpusId) listId r._id) a [ href (toLink $ (corpusDocument corpusId) listId r._id)
, style {textDecoration : "line-through"} , style {textDecoration : "line-through"}
, target "_blank" , target "_blank"
...@@ -324,11 +322,10 @@ renderPage loaderDispatch { totalRecords, dispatch, listId, corpusId ...@@ -324,11 +322,10 @@ renderPage loaderDispatch { totalRecords, dispatch, listId, corpusId
else else
a [ href (toLink $ (corpusDocument corpusId) listId r._id) a [ href (toLink $ (corpusDocument corpusId) listId r._id)
, target "_blank" ] [ text r.title ] , target "_blank" ] [ text r.title ]
, if toDel then , if isDel then
div [style {textDecoration : "line-through"}] [ text r.source] div [style {textDecoration : "line-through"}] [ text r.source]
else else
div [] [ text r.source] div [] [ text r.source]
] ]
, delete: true , delete: true
}) <$> filter (not <<< isDeleted) res }) <$> filter (not <<< isDeleted) res
...@@ -372,33 +369,46 @@ searchResults squery = post "http://localhost:8008/count" unit ...@@ -372,33 +369,46 @@ searchResults squery = post "http://localhost:8008/count" unit
newtype FavoriteQuery = FavoriteQuery newtype FavoriteQuery = FavoriteQuery
{ favorites :: Array Int {
} favorites :: Array Int
}
instance encodeJsonFQuery :: EncodeJson FavoriteQuery where instance encodeJsonFQuery :: EncodeJson FavoriteQuery where
encodeJson (FavoriteQuery post) encodeJson (FavoriteQuery post)
= "favorites" := post.favorites = "favorites" := post.favorites
~> jsonEmptyObject ~> jsonEmptyObject
newtype DeleteDocumentQuery = DeleteDocumentQuery favoritesUrl :: Int -> String
{ favoritesUrl nodeId = toUrl Back Node (Just nodeId) <> "/favorites"
documents :: Array Int
} putFavorites :: Int -> FavoriteQuery -> Aff (Array Int)
putFavorites nodeId = put $ favoritesUrl nodeId
deleteFavorites :: Int -> FavoriteQuery -> Aff (Array Int)
deleteFavorites nodeId = deleteWithBody $ favoritesUrl nodeId
newtype DocumentQuery = DocumentQuery
{
documents :: Array Int
}
instance encodeJsonDDQuery :: EncodeJson DeleteDocumentQuery where instance encodeJsonDDQuery :: EncodeJson DocumentQuery where
encodeJson (DeleteDocumentQuery post) encodeJson (DocumentQuery post)
= "documents" := post.documents = "documents" := post.documents
~> jsonEmptyObject ~> jsonEmptyObject
putFavorites :: Int -> FavoriteQuery -> Aff (Array Int) documentsUrl :: Int -> String
putFavorites nodeId = put (toUrl Back Node (Just nodeId) <> "/favorites") documentsUrl nodeId = toUrl Back Node (Just nodeId) <> "/documents"
deleteFavorites :: Int -> FavoriteQuery -> Aff (Array Int) putDocuments :: Int -> DocumentQuery -> Aff (Array Int)
deleteFavorites nodeId = deleteWithBody (toUrl Back Node (Just nodeId) <> "/favorites") putDocuments nodeId = put $ documentsUrl nodeId
deleteDocuments :: Int -> DocumentQuery -> Aff (Array Int)
deleteDocuments nodeId = deleteWithBody $ documentsUrl nodeId
deleteDocuments :: Int -> DeleteDocumentQuery -> Aff (Array Int) deleteAllDocuments :: Int -> Aff (Array Int)
deleteDocuments nodeId = deleteWithBody (toUrl Back Node (Just nodeId) <> "/documents") deleteAllDocuments nodeId = delete $ documentsUrl nodeId
-- TODO: not optimal but Data.Set lacks some function (Set.alter) -- TODO: not optimal but Data.Set lacks some function (Set.alter)
toggleSet :: forall a. Ord a => a -> Set a -> Set a toggleSet :: forall a. Ord a => a -> Set a -> Set a
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment