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
import Data.Array (drop, take, (:), filter)
import Data.Either (Either(..))
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Eq (genericEq)
import Data.Generic.Rep.Show (genericShow)
import Data.HTTP.Method (Method(..))
import Data.Lens
......@@ -43,6 +44,26 @@ import Thermite (PerformAction, Render, Spec, defaultPerformAction, modifyState_
type NodeID = 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 =
{ nodeId :: Int
......@@ -57,23 +78,21 @@ type Props =
type State =
{ documentIdsDeleted :: Set Int
, localFavorites :: Map Int Boolean
, localCategories :: Map Int Category
}
initialState :: State
initialState =
{ documentIdsDeleted: mempty
, localFavorites: mempty
, localCategories: mempty
}
_documentIdsDeleted = prop (SProxy :: SProxy "documentIdsDeleted")
_localFavorites = prop (SProxy :: SProxy "localFavorites")
_localCategories = prop (SProxy :: SProxy "localCategories")
data Action
= MarkFavorites Int Boolean
-- | ToggleDocumentToDelete Int
| ToggleDeleteDocument Int
| Trash
= MarkCategory Int Category
| TrashAll
newtype DocumentsView
= DocumentsView
......@@ -82,9 +101,8 @@ newtype DocumentsView
, date :: Int
, title :: String
, source :: String
, fav :: Boolean
, category :: Category
, ngramCount :: Int
, delete :: Boolean
}
......@@ -97,7 +115,7 @@ instance showDocumentsView :: Show DocumentsView where
newtype Response = Response
{ cid :: Int
, hyperdata :: Hyperdata
, favorite :: Boolean
, category :: Category
, ngramCount :: Int
}
......@@ -108,22 +126,6 @@ newtype Hyperdata = Hyperdata
, 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
decodeJson json = do
......@@ -133,6 +135,12 @@ instance decodeHyperdata :: DecodeJson Hyperdata where
pub_year <- obj .? "publication_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
decodeJson json = do
obj <- decodeJson json
......@@ -140,7 +148,7 @@ instance decodeResponse :: DecodeJson Response where
favorite <- obj .? "favorite"
ngramCount <- obj .? "id"
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
filterSpec :: forall state props action. Spec state props action
filterSpec = simpleSpec defaultPerformAction render
where
render d p s c = [] {-[div [ className "col-md-2", style {textAlign : "center", marginLeft : "0px", paddingLeft : "0px"}] [ text " Filter "
, input [className "form-control", placeholder "Filter here"]
]] -}
render d p s c = []
docViewSpec :: Spec {} Props Void
docViewSpec = hideState (const initialState) layoutDocview
......@@ -160,29 +166,16 @@ layoutDocview :: Spec State Props Action
layoutDocview = simpleSpec performAction render
where
performAction :: PerformAction State Props Action
performAction (MarkFavorites nid fav) {nodeId} _ = do
modifyState_ $ _localFavorites <<< at nid ?~ fav
void $ lift $ if fav
then putFavorites nodeId $ FavoriteQuery {favorites: [nid]}
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
performAction (MarkCategory nid cat) {nodeId} _ = do
modifyState_ $ _localCategories <<< at nid ?~ cat
void $ lift $ putCategories nodeId $ CategoryQuery {nodeIds: [nid], category: cat}
performAction TrashAll {nodeId} {documentIdsDeleted} = do
ids <- lift $ deleteAllDocuments nodeId
modifyState_ $ _ {documentIdsDeleted = Set.union documentIdsDeleted $ Set.fromFoldable ids}
render :: Render State Props Action
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 "row"]
[ chart
......@@ -199,7 +192,7 @@ layoutDocview = simpleSpec performAction render
, div [className "col-md-1 col-md-offset-11"]
[ button [ className "btn"
, style {backgroundColor: "peru", color : "white", border : "white"}
, onClick $ (\_ -> dispatch Trash)
, onClick $ (\_ -> dispatch TrashAll)
]
[ i [className "glyphitem glyphicon glyphicon-trash"] []
, text "Trash all"
......@@ -223,8 +216,6 @@ loadPage {nodeId, tabType, listId, corpusId, params: {limit, offset, orderBy}} =
logs "loading documents page: loadPage with Offset and limit"
res <- get $ toUrl Back (Tab tabType offset limit (convOrderBy <$> orderBy)) (Just nodeId)
let docs = res2corpus <$> res
--_ <- logs "Ok: loading page documents"
--_ <- logs $ map show docs
pure $
if mock then take limit $ drop offset sampleData else
docs
......@@ -236,9 +227,8 @@ loadPage {nodeId, tabType, listId, corpusId, params: {limit, offset, orderBy}} =
, date : (\(Hyperdata hr) -> hr.pub_year) r.hyperdata
, title : (\(Hyperdata hr) -> hr.title) r.hyperdata
, source : (\(Hyperdata hr) -> hr.source) r.hyperdata
, fav : r.favorite
, category : r.category
, ngramCount : r.ngramCount
, delete : false
}
convOrderBy (T.ASC (T.ColumnName "Date")) = DateAsc
convOrderBy (T.DESC (T.ColumnName "Date")) = DateDesc
......@@ -271,7 +261,7 @@ renderPage :: forall props path.
(Loader.Action PageParams)
renderPage _ _ {loaded: Nothing} _ = [] -- TODO loading spinner
renderPage loaderDispatch { totalRecords, dispatch, listId, corpusId
, deletionState: {documentIdsDeleted, localFavorites}}
, deletionState: {documentIdsDeleted, localCategories}}
{currentPath: {nodeId, tabType}, loaded: Just res} _ =
[ T.tableElt
{ rows
......@@ -289,47 +279,37 @@ renderPage loaderDispatch { totalRecords, dispatch, listId, corpusId
}
]
where
gi true = "glyphicon glyphicon-star"
gi false = "glyphicon glyphicon-star-empty"
isDeleted (DocumentsView {_id}) = Set.member _id documentIdsDeleted
isFavorite {_id,fav} = maybe fav identity (localFavorites ^. at _id)
gi Favorite = "glyphicon glyphicon-star"
gi _ = "glyphicon glyphicon-star-empty"
trashStyle Trash = style {textDecoration: "line-through"}
trashStyle _ = style {textDecoration: "none"}
getCategory {_id, category} = maybe category identity (localCategories ^. at _id)
corpusDocument (Just corpusId) = R.CorpusDocument corpusId
corpusDocument _ = R.Document
rows = (\(DocumentsView r) ->
let isFav = isFavorite r
isDel = isDeleted $ DocumentsView r in
let cat = getCategory r
isDel = Trash == cat in
{ row:
[ div []
[ a [ className $ gi isFav
, if isDel then style {textDecoration : "line-through"}
else style {textDecoration : "none"}
, onClick $ (\_-> dispatch $ MarkFavorites r._id (not isFav))
[ a [ className $ gi cat
, trashStyle cat
, onClick $ \_-> dispatch $ MarkCategory r._id $ favCategory cat
] []
]
, input [ _type "checkbox"
, checked isDel
, onClick $ (\_ -> dispatch $ ToggleDeleteDocument r._id)
, onClick $ \_ -> dispatch $ MarkCategory r._id $ trashCategory cat
]
-- TODO show date: Year-Month-Day only
, if isDel then
div [ style {textDecoration : "line-through"}][text (show r.date)]
else
div [ ][text (show r.date)]
, if isDel then
a [ href (toLink $ (corpusDocument corpusId) listId r._id)
, style {textDecoration : "line-through"}
, target "_blank"
, div [ trashStyle cat ][text (show r.date)]
, a [ href (toLink $ (corpusDocument corpusId) listId r._id)
, trashStyle cat
, target "_blank"
] [ text r.title ]
else
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]
, div [trashStyle cat] [ text r.source ]
]
, delete: true
}) <$> filter (not <<< isDeleted) res
}) <$> res
pageLoaderClass :: ReactClass (PageLoaderProps (children :: Children))
pageLoaderClass = Loader.createLoaderClass' "PageLoader" loadPage renderPage
......@@ -339,11 +319,23 @@ pageLoader props = React.createElement pageLoaderClass props []
---------------------------------------------------------
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 = 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 = [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
-- TODO
newtype FavoriteQuery = FavoriteQuery
{
favorites :: Array Int
newtype CategoryQuery = CategoryQuery {
nodeIds :: Array Int
, category :: Category
}
instance encodeJsonFQuery :: EncodeJson FavoriteQuery where
encodeJson (FavoriteQuery post)
= "favorites" := post.favorites
~> 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
}
instance encodeJsonCategoryQuery :: EncodeJson CategoryQuery where
encodeJson (CategoryQuery post) =
"ntc_nodesId" := post.nodeIds
~> "ntc_category" := encodeJson post.category
~> jsonEmptyObject
categoryUrl :: Int -> String
categoryUrl nodeId = toUrl Back Node (Just nodeId) <> "/category"
instance encodeJsonDDQuery :: EncodeJson DocumentQuery where
encodeJson (DocumentQuery post)
= "documents" := post.documents
~> jsonEmptyObject
putCategories :: Int -> CategoryQuery -> Aff (Array Int)
putCategories nodeId = put $ categoryUrl nodeId
documentsUrl :: Int -> String
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 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