Commit df8f7022 authored by Przemyslaw Kaminski's avatar Przemyslaw Kaminski

[DocsTable] fixes to the new favorite/deleted -> category change

parent 4f073cb3
...@@ -10,6 +10,7 @@ import Data.Argonaut (class DecodeJson, class EncodeJson, decodeJson, encodeJson ...@@ -10,6 +10,7 @@ import Data.Argonaut (class DecodeJson, class EncodeJson, decodeJson, encodeJson
import Data.Array (drop, take, (:), filter) import Data.Array (drop, take, (:), filter)
import Data.Either (Either(..)) import Data.Either (Either(..))
import Data.Generic.Rep (class Generic) import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Eq (genericEq)
import Data.Generic.Rep.Show (genericShow) import Data.Generic.Rep.Show (genericShow)
import Data.HTTP.Method (Method(..)) import Data.HTTP.Method (Method(..))
import Data.Lens import Data.Lens
...@@ -43,6 +44,26 @@ import Thermite (PerformAction, Render, Spec, defaultPerformAction, modifyState_ ...@@ -43,6 +44,26 @@ import Thermite (PerformAction, Render, Spec, defaultPerformAction, modifyState_
type NodeID = Int type NodeID = Int
type TotalRecords = Int type TotalRecords = Int
data Category = Trash | Normal | Favorite
derive instance genericFavorite :: Generic Category _
instance showCategory :: Show Category where
show = genericShow
instance eqCategory :: Eq Category where
eq = genericEq
instance encodeJsonCategory :: EncodeJson Category where
encodeJson Trash = encodeJson 0
encodeJson Normal = encodeJson 1
encodeJson Favorite = encodeJson 2
favCategory :: Category -> Category
favCategory Normal = Favorite
favCategory Trash = Favorite
favCategory Favorite = Normal
trashCategory :: Category -> Category
trashCategory Normal = Trash
trashCategory Trash = Normal
trashCategory Favorite = Trash
type Props = type Props =
{ nodeId :: Int { nodeId :: Int
...@@ -57,23 +78,21 @@ type Props = ...@@ -57,23 +78,21 @@ type Props =
type State = type State =
{ documentIdsDeleted :: Set Int { documentIdsDeleted :: Set Int
, localFavorites :: Map Int Boolean , localCategories :: Map Int Category
} }
initialState :: State initialState :: State
initialState = initialState =
{ documentIdsDeleted: mempty { documentIdsDeleted: mempty
, localFavorites: mempty , localCategories: mempty
} }
_documentIdsDeleted = prop (SProxy :: SProxy "documentIdsDeleted") _documentIdsDeleted = prop (SProxy :: SProxy "documentIdsDeleted")
_localFavorites = prop (SProxy :: SProxy "localFavorites") _localCategories = prop (SProxy :: SProxy "localCategories")
data Action data Action
= MarkFavorites Int Boolean = MarkCategory Int Category
-- | ToggleDocumentToDelete Int | TrashAll
| ToggleDeleteDocument Int
| Trash
newtype DocumentsView newtype DocumentsView
= DocumentsView = DocumentsView
...@@ -82,9 +101,8 @@ newtype DocumentsView ...@@ -82,9 +101,8 @@ newtype DocumentsView
, date :: Int , date :: Int
, title :: String , title :: String
, source :: String , source :: String
, fav :: Boolean , category :: Category
, ngramCount :: Int , ngramCount :: Int
, delete :: Boolean
} }
...@@ -97,7 +115,7 @@ instance showDocumentsView :: Show DocumentsView where ...@@ -97,7 +115,7 @@ instance showDocumentsView :: Show DocumentsView where
newtype Response = Response newtype Response = Response
{ cid :: Int { cid :: Int
, hyperdata :: Hyperdata , hyperdata :: Hyperdata
, favorite :: Boolean , category :: Category
, ngramCount :: Int , ngramCount :: Int
} }
...@@ -108,22 +126,6 @@ newtype Hyperdata = Hyperdata ...@@ -108,22 +126,6 @@ newtype Hyperdata = Hyperdata
, pub_year :: Int , pub_year :: Int
} }
--instance decodeHyperdata :: DecodeJson Hyperdata where
-- decodeJson json = do
-- obj <- decodeJson json
-- title <- obj .? "title"
-- source <- obj .? "source"
-- pure $ Hyperdata { title,source }
--instance decodeResponse :: DecodeJson Response where
-- decodeJson json = do
-- obj <- decodeJson json
-- cid <- obj .? "id"
-- created <- obj .? "created"
-- favorite <- obj .? "favorite"
-- ngramCount <- obj .? "ngramCount"
-- hyperdata <- obj .? "hyperdata"
-- pure $ Response { cid, created, favorite, ngramCount, hyperdata }
instance decodeHyperdata :: DecodeJson Hyperdata where instance decodeHyperdata :: DecodeJson Hyperdata where
decodeJson json = do decodeJson json = do
...@@ -133,6 +135,12 @@ instance decodeHyperdata :: DecodeJson Hyperdata where ...@@ -133,6 +135,12 @@ instance decodeHyperdata :: DecodeJson Hyperdata where
pub_year <- obj .? "publication_year" pub_year <- obj .? "publication_year"
pure $ Hyperdata { title,source, pub_year} pure $ Hyperdata { title,source, pub_year}
decodeCategory :: Int -> Category
decodeCategory 0 = Trash
decodeCategory 1 = Normal
decodeCategory 2 = Favorite
decodeCategory _ = Normal
instance decodeResponse :: DecodeJson Response where instance decodeResponse :: DecodeJson Response where
decodeJson json = do decodeJson json = do
obj <- decodeJson json obj <- decodeJson json
...@@ -140,7 +148,7 @@ instance decodeResponse :: DecodeJson Response where ...@@ -140,7 +148,7 @@ instance decodeResponse :: DecodeJson Response where
favorite <- obj .? "favorite" favorite <- obj .? "favorite"
ngramCount <- obj .? "id" ngramCount <- obj .? "id"
hyperdata <- obj .? "hyperdata" hyperdata <- obj .? "hyperdata"
pure $ Response { cid, favorite, ngramCount, hyperdata } pure $ Response { cid, category: decodeCategory favorite, ngramCount, hyperdata }
...@@ -148,9 +156,7 @@ instance decodeResponse :: DecodeJson Response where ...@@ -148,9 +156,7 @@ instance decodeResponse :: DecodeJson Response where
filterSpec :: forall state props action. Spec state props action filterSpec :: forall state props action. Spec state props action
filterSpec = simpleSpec defaultPerformAction render filterSpec = simpleSpec defaultPerformAction render
where where
render d p s c = [] {-[div [ className "col-md-2", style {textAlign : "center", marginLeft : "0px", paddingLeft : "0px"}] [ text " Filter " render d p s c = []
, input [className "form-control", placeholder "Filter here"]
]] -}
docViewSpec :: Spec {} Props Void docViewSpec :: Spec {} Props Void
docViewSpec = hideState (const initialState) layoutDocview docViewSpec = hideState (const initialState) layoutDocview
...@@ -160,29 +166,16 @@ layoutDocview :: Spec State Props Action ...@@ -160,29 +166,16 @@ layoutDocview :: Spec State Props Action
layoutDocview = simpleSpec performAction render layoutDocview = simpleSpec performAction render
where where
performAction :: PerformAction State Props Action performAction :: PerformAction State Props Action
performAction (MarkFavorites nid fav) {nodeId} _ = do performAction (MarkCategory nid cat) {nodeId} _ = do
modifyState_ $ _localFavorites <<< at nid ?~ fav modifyState_ $ _localCategories <<< at nid ?~ cat
void $ lift $ if fav void $ lift $ putCategories nodeId $ CategoryQuery {nodeIds: [nid], category: cat}
then putFavorites nodeId $ FavoriteQuery {favorites: [nid]} performAction TrashAll {nodeId} {documentIdsDeleted} = do
else deleteFavorites nodeId $ FavoriteQuery {favorites: [nid]}
performAction (ToggleDeleteDocument nid) {nodeId} {documentIdsDeleted} = do
modifyState_ $ _ {documentIdsDeleted = toggleSet nid documentIdsDeleted}
void $ lift $ if (Set.member nid documentIdsDeleted)
then deleteDocuments nodeId $ DocumentQuery {documents: [nid]}
else putDocuments nodeId $ DocumentQuery {documents: [nid]}
performAction Trash {nodeId} {documentIdsDeleted} = do
ids <- lift $ deleteAllDocuments nodeId ids <- lift $ deleteAllDocuments nodeId
modifyState_ $ _ {documentIdsDeleted = Set.union documentIdsDeleted $ Set.fromFoldable ids} 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 _ =
[ {- br' [
, div [ style {textAlign : "center"}] [ text " Filter "
, input [className "form-control", style {width : "120px", display : "inline-block"}, placeholder "Filter here"]
]
, p [] [text ""]
, br'
-}
div [className "container1"] div [className "container1"]
[ div [className "row"] [ div [className "row"]
[ chart [ chart
...@@ -199,7 +192,7 @@ layoutDocview = simpleSpec performAction render ...@@ -199,7 +192,7 @@ layoutDocview = simpleSpec performAction render
, div [className "col-md-1 col-md-offset-11"] , div [className "col-md-1 col-md-offset-11"]
[ button [ className "btn" [ button [ className "btn"
, style {backgroundColor: "peru", color : "white", border : "white"} , style {backgroundColor: "peru", color : "white", border : "white"}
, onClick $ (\_ -> dispatch Trash) , onClick $ (\_ -> dispatch TrashAll)
] ]
[ i [className "glyphitem glyphicon glyphicon-trash"] [] [ i [className "glyphitem glyphicon glyphicon-trash"] []
, text "Trash all" , text "Trash all"
...@@ -223,8 +216,6 @@ loadPage {nodeId, tabType, listId, corpusId, params: {limit, offset, orderBy}} = ...@@ -223,8 +216,6 @@ loadPage {nodeId, tabType, listId, corpusId, params: {limit, offset, orderBy}} =
logs "loading documents page: loadPage with Offset and limit" logs "loading documents page: loadPage with Offset and limit"
res <- get $ toUrl Back (Tab tabType offset limit (convOrderBy <$> orderBy)) (Just nodeId) res <- get $ toUrl Back (Tab tabType offset limit (convOrderBy <$> orderBy)) (Just nodeId)
let docs = res2corpus <$> res let docs = res2corpus <$> res
--_ <- logs "Ok: loading page documents"
--_ <- logs $ map show docs
pure $ pure $
if mock then take limit $ drop offset sampleData else if mock then take limit $ drop offset sampleData else
docs docs
...@@ -236,9 +227,8 @@ loadPage {nodeId, tabType, listId, corpusId, params: {limit, offset, orderBy}} = ...@@ -236,9 +227,8 @@ loadPage {nodeId, tabType, listId, corpusId, params: {limit, offset, orderBy}} =
, date : (\(Hyperdata hr) -> hr.pub_year) r.hyperdata , date : (\(Hyperdata hr) -> hr.pub_year) r.hyperdata
, title : (\(Hyperdata hr) -> hr.title) r.hyperdata , title : (\(Hyperdata hr) -> hr.title) r.hyperdata
, source : (\(Hyperdata hr) -> hr.source) r.hyperdata , source : (\(Hyperdata hr) -> hr.source) r.hyperdata
, fav : r.favorite , category : r.category
, ngramCount : r.ngramCount , ngramCount : r.ngramCount
, delete : false
} }
convOrderBy (T.ASC (T.ColumnName "Date")) = DateAsc convOrderBy (T.ASC (T.ColumnName "Date")) = DateAsc
convOrderBy (T.DESC (T.ColumnName "Date")) = DateDesc convOrderBy (T.DESC (T.ColumnName "Date")) = DateDesc
...@@ -271,7 +261,7 @@ renderPage :: forall props path. ...@@ -271,7 +261,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: {documentIdsDeleted, localFavorites}} , deletionState: {documentIdsDeleted, localCategories}}
{currentPath: {nodeId, tabType}, loaded: Just res} _ = {currentPath: {nodeId, tabType}, loaded: Just res} _ =
[ T.tableElt [ T.tableElt
{ rows { rows
...@@ -289,47 +279,37 @@ renderPage loaderDispatch { totalRecords, dispatch, listId, corpusId ...@@ -289,47 +279,37 @@ renderPage loaderDispatch { totalRecords, dispatch, listId, corpusId
} }
] ]
where where
gi true = "glyphicon glyphicon-star" gi Favorite = "glyphicon glyphicon-star"
gi false = "glyphicon glyphicon-star-empty" gi _ = "glyphicon glyphicon-star-empty"
isDeleted (DocumentsView {_id}) = Set.member _id documentIdsDeleted trashStyle Trash = style {textDecoration: "line-through"}
isFavorite {_id,fav} = maybe fav identity (localFavorites ^. at _id) trashStyle _ = style {textDecoration: "none"}
getCategory {_id, category} = maybe category identity (localCategories ^. 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 cat = getCategory r
isDel = isDeleted $ DocumentsView r in isDel = Trash == cat in
{ row: { row:
[ div [] [ div []
[ a [ className $ gi isFav [ a [ className $ gi cat
, if isDel then style {textDecoration : "line-through"} , trashStyle cat
else style {textDecoration : "none"} , onClick $ \_-> dispatch $ MarkCategory r._id $ favCategory cat
, onClick $ (\_-> dispatch $ MarkFavorites r._id (not isFav))
] [] ] []
] ]
, input [ _type "checkbox" , input [ _type "checkbox"
, checked isDel , checked isDel
, onClick $ (\_ -> dispatch $ ToggleDeleteDocument r._id) , onClick $ \_ -> dispatch $ MarkCategory r._id $ trashCategory cat
] ]
-- TODO show date: Year-Month-Day only -- TODO show date: Year-Month-Day only
, if isDel then , div [ trashStyle cat ][text (show r.date)]
div [ style {textDecoration : "line-through"}][text (show r.date)] , a [ href (toLink $ (corpusDocument corpusId) listId r._id)
else , trashStyle cat
div [ ][text (show r.date)] , target "_blank"
, if isDel then
a [ href (toLink $ (corpusDocument corpusId) listId r._id)
, style {textDecoration : "line-through"}
, target "_blank"
] [ text r.title ] ] [ text r.title ]
else , div [trashStyle cat] [ text r.source ]
a [ href (toLink $ (corpusDocument corpusId) listId r._id)
, target "_blank" ] [ text r.title ]
, if isDel then
div [style {textDecoration : "line-through"}] [ text r.source]
else
div [] [ text r.source]
] ]
, delete: true , delete: true
}) <$> filter (not <<< isDeleted) res }) <$> res
pageLoaderClass :: ReactClass (PageLoaderProps (children :: Children)) pageLoaderClass :: ReactClass (PageLoaderProps (children :: Children))
pageLoaderClass = Loader.createLoaderClass' "PageLoader" loadPage renderPage pageLoaderClass = Loader.createLoaderClass' "PageLoader" loadPage renderPage
...@@ -339,11 +319,23 @@ pageLoader props = React.createElement pageLoaderClass props [] ...@@ -339,11 +319,23 @@ pageLoader props = React.createElement pageLoaderClass props []
--------------------------------------------------------- ---------------------------------------------------------
sampleData' :: DocumentsView sampleData' :: DocumentsView
sampleData' = DocumentsView {_id : 1, url : "", date : 2010, title : "title", source : "source", fav : false, ngramCount : 1, delete : false} sampleData' = DocumentsView { _id : 1
, url : ""
, date : 2010
, title : "title"
, source : "source"
, category : Normal
, ngramCount : 1}
sampleData :: Array DocumentsView sampleData :: Array DocumentsView
--sampleData = replicate 10 sampleData' --sampleData = replicate 10 sampleData'
sampleData = map (\(Tuple t s) -> DocumentsView {_id : 1, url : "", date : 2017, title: t, source: s, fav : false, ngramCount : 10, delete : false}) sampleDocuments sampleData = map (\(Tuple t s) -> DocumentsView { _id : 1
, url : ""
, date : 2017
, title: t
, source: s
, category : Normal
, ngramCount : 10}) sampleDocuments
sampleDocuments :: Array (Tuple String String) sampleDocuments :: Array (Tuple String String)
sampleDocuments = [Tuple "Macroscopic dynamics of the fusion process" "Journal de Physique Lettres",Tuple "Effects of static and cyclic fatigue at high temperature upon reaction bonded silicon nitride" "Journal de Physique Colloques",Tuple "Reliability of metal/glass-ceramic junctions made by solid state bonding" "Journal de Physique Colloques",Tuple "High temperature mechanical properties and intergranular structure of sialons" "Journal de Physique Colloques",Tuple "SOLUTIONS OF THE LANDAU-VLASOV EQUATION IN NUCLEAR PHYSICS" "Journal de Physique Colloques",Tuple "A STUDY ON THE FUSION REACTION 139La + 12C AT 50 MeV/u WITH THE VUU EQUATION" "Journal de Physique Colloques",Tuple "Atomic structure of \"vitreous\" interfacial films in sialon" "Journal de Physique Colloques",Tuple "MICROSTRUCTURAL AND ANALYTICAL CHARACTERIZATION OF Al2O3/Al-Mg COMPOSITE INTERFACES" "Journal de Physique Colloques",Tuple "Development of oxidation resistant high temperature NbTiAl alloys and intermetallics" "Journal de Physique IV Colloque",Tuple "Determination of brazed joint constitutive law by inverse method" "Journal de Physique IV Colloque",Tuple "Two dimensional estimates from ocean SAR images" "Nonlinear Processes in Geophysics",Tuple "Comparison Between New Carbon Nanostructures Produced by Plasma with Industrial Carbon Black Grades" "Journal de Physique III",Tuple "<i>Letter to the Editor:</i> SCIPION, a new flexible ionospheric sounder in Senegal" "Annales Geophysicae",Tuple "Is reducibility in nuclear multifragmentation related to thermal scaling?" "Physics Letters B",Tuple "Independence of fragment charge distributions of the size of heavy multifragmenting sources" "Physics Letters B",Tuple "Hard photons and neutral pions as probes of hot and dense nuclear matter" "Nuclear Physics A",Tuple "Surveying the nuclear caloric curve" "Physics Letters B",Tuple "A hot expanding source in 50 A MeV Xe+Sn central reactions" "Physics Letters B"] sampleDocuments = [Tuple "Macroscopic dynamics of the fusion process" "Journal de Physique Lettres",Tuple "Effects of static and cyclic fatigue at high temperature upon reaction bonded silicon nitride" "Journal de Physique Colloques",Tuple "Reliability of metal/glass-ceramic junctions made by solid state bonding" "Journal de Physique Colloques",Tuple "High temperature mechanical properties and intergranular structure of sialons" "Journal de Physique Colloques",Tuple "SOLUTIONS OF THE LANDAU-VLASOV EQUATION IN NUCLEAR PHYSICS" "Journal de Physique Colloques",Tuple "A STUDY ON THE FUSION REACTION 139La + 12C AT 50 MeV/u WITH THE VUU EQUATION" "Journal de Physique Colloques",Tuple "Atomic structure of \"vitreous\" interfacial films in sialon" "Journal de Physique Colloques",Tuple "MICROSTRUCTURAL AND ANALYTICAL CHARACTERIZATION OF Al2O3/Al-Mg COMPOSITE INTERFACES" "Journal de Physique Colloques",Tuple "Development of oxidation resistant high temperature NbTiAl alloys and intermetallics" "Journal de Physique IV Colloque",Tuple "Determination of brazed joint constitutive law by inverse method" "Journal de Physique IV Colloque",Tuple "Two dimensional estimates from ocean SAR images" "Nonlinear Processes in Geophysics",Tuple "Comparison Between New Carbon Nanostructures Produced by Plasma with Industrial Carbon Black Grades" "Journal de Physique III",Tuple "<i>Letter to the Editor:</i> SCIPION, a new flexible ionospheric sounder in Senegal" "Annales Geophysicae",Tuple "Is reducibility in nuclear multifragmentation related to thermal scaling?" "Physics Letters B",Tuple "Independence of fragment charge distributions of the size of heavy multifragmenting sources" "Physics Letters B",Tuple "Hard photons and neutral pions as probes of hot and dense nuclear matter" "Nuclear Physics A",Tuple "Surveying the nuclear caloric curve" "Physics Letters B",Tuple "A hot expanding source in 50 A MeV Xe+Sn central reactions" "Physics Letters B"]
...@@ -368,46 +360,26 @@ searchResults squery = post "http://localhost:8008/count" unit ...@@ -368,46 +360,26 @@ searchResults squery = post "http://localhost:8008/count" unit
-- TODO -- TODO
newtype CategoryQuery = CategoryQuery {
newtype FavoriteQuery = FavoriteQuery nodeIds :: Array Int
{ , category :: Category
favorites :: Array Int
} }
instance encodeJsonFQuery :: EncodeJson FavoriteQuery where instance encodeJsonCategoryQuery :: EncodeJson CategoryQuery where
encodeJson (FavoriteQuery post) encodeJson (CategoryQuery post) =
= "favorites" := post.favorites "ntc_nodesId" := post.nodeIds
~> jsonEmptyObject ~> "ntc_category" := encodeJson post.category
~> jsonEmptyObject
favoritesUrl :: Int -> String
favoritesUrl nodeId = toUrl Back Node (Just nodeId) <> "/favorites"
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
}
categoryUrl :: Int -> String
categoryUrl nodeId = toUrl Back Node (Just nodeId) <> "/category"
instance encodeJsonDDQuery :: EncodeJson DocumentQuery where putCategories :: Int -> CategoryQuery -> Aff (Array Int)
encodeJson (DocumentQuery post) putCategories nodeId = put $ categoryUrl nodeId
= "documents" := post.documents
~> jsonEmptyObject
documentsUrl :: Int -> String documentsUrl :: Int -> String
documentsUrl nodeId = toUrl Back Node (Just nodeId) <> "/documents" documentsUrl nodeId = toUrl Back Node (Just nodeId) <> "/documents"
putDocuments :: Int -> DocumentQuery -> Aff (Array Int)
putDocuments nodeId = put $ documentsUrl nodeId
deleteDocuments :: Int -> DocumentQuery -> Aff (Array Int)
deleteDocuments nodeId = deleteWithBody $ documentsUrl nodeId
deleteAllDocuments :: Int -> Aff (Array Int) deleteAllDocuments :: Int -> Aff (Array Int)
deleteAllDocuments nodeId = delete $ documentsUrl nodeId deleteAllDocuments nodeId = delete $ documentsUrl nodeId
......
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