[doc table] fixes to stars (add 1 more state, refactor)

parent d720b1a6
...@@ -33,6 +33,7 @@ to generate this file without the comments in this block. ...@@ -33,6 +33,7 @@ to generate this file without the comments in this block.
, "dom-simple" , "dom-simple"
, "effect" , "effect"
, "either" , "either"
, "enums"
, "exceptions" , "exceptions"
, "ffi-simple" , "ffi-simple"
, "foldable-traversable" , "foldable-traversable"
......
...@@ -4,6 +4,7 @@ module Gargantext.Components.Category where ...@@ -4,6 +4,7 @@ module Gargantext.Components.Category where
import Gargantext.Prelude import Gargantext.Prelude
import Data.Array as A import Data.Array as A
import Data.Enum (fromEnum)
import Data.Generic.Rep (class Generic) import Data.Generic.Rep (class Generic)
import Data.Map as Map import Data.Map as Map
import Data.Maybe (Maybe(..), fromMaybe) import Data.Maybe (Maybe(..), fromMaybe)
...@@ -12,8 +13,8 @@ import Effect.Aff (launchAff_) ...@@ -12,8 +13,8 @@ import Effect.Aff (launchAff_)
import Effect.Class (liftEffect) import Effect.Class (liftEffect)
import Gargantext.Components.Bootstrap as B import Gargantext.Components.Bootstrap as B
import Gargantext.Components.Bootstrap.Types (Variant(..)) import Gargantext.Components.Bootstrap.Types (Variant(..))
import Gargantext.Components.Category.Types (Category(..), Star(..), cat2score, categories, clickAgain, decodeStar, star2score, stars) import Gargantext.Components.Category.Types (Category(..), Star(..), cat2score, cat2star, categories, categoryNextState, decodeCategory, stars)
import Gargantext.Components.DocsTable.Types (DocumentsView(..), LocalCategories, LocalUserScore) import Gargantext.Components.DocsTable.Types (DocumentsView(..), LocalCategories)
import Gargantext.Components.GraphQL.Context (NodeContext) import Gargantext.Components.GraphQL.Context (NodeContext)
import Gargantext.Components.GraphQL.Endpoints (getNodeContext, updateNodeContextCategory) import Gargantext.Components.GraphQL.Endpoints (getNodeContext, updateNodeContextCategory)
import Gargantext.Config.REST (AffRESTError, RESTError(..)) import Gargantext.Config.REST (AffRESTError, RESTError(..))
...@@ -36,9 +37,9 @@ type RatingProps = ...@@ -36,9 +37,9 @@ type RatingProps =
( chartReload :: T2.ReloadS ( chartReload :: T2.ReloadS
, nodeId :: NodeID , nodeId :: NodeID
, row :: DocumentsView , row :: DocumentsView
, score :: Star , score :: Category
, session :: Session , session :: Session
, setLocalCategories :: R.Setter LocalUserScore -- , setLocalCategories :: R.Setter LocalCategories
) )
rating :: R2.Component RatingProps rating :: R2.Component RatingProps
...@@ -50,11 +51,11 @@ ratingCpt = here.component "rating" cpt where ...@@ -50,11 +51,11 @@ ratingCpt = here.component "rating" cpt where
, row: DocumentsView r , row: DocumentsView r
, score , score
, session , session
, setLocalCategories -- , setLocalCategories
} _ = do } _ = do
pure $ renderRatingSimple { docId: r._id pure $ renderRatingSimple { docId: r._id
, corpusId: nodeId , corpusId: nodeId
, category: star2score score , category: score
, session } [] , session } []
-- -- | Behaviors -- -- | Behaviors
...@@ -86,14 +87,17 @@ ratingCpt = here.component "rating" cpt where ...@@ -86,14 +87,17 @@ ratingCpt = here.component "rating" cpt where
-- , className: ratingClassName score s -- , className: ratingClassName score s
-- } -- }
ratingIcon Star_0 Star_0 = "recycle" ratingIcon :: Category -> Star -> String
ratingIcon Trash Star_0 = "recycle"
ratingIcon _ Star_0 = "trash" ratingIcon _ Star_0 = "trash"
ratingIcon c s = star2score c < star2score s ? "star-o" $ "star" ratingIcon c s = fromEnum (cat2star c) < fromEnum s ? "star-o" $ "star"
ratingVariant :: Star -> Star -> Variant
ratingVariant Star_0 Star_0 = Dark ratingVariant Star_0 Star_0 = Dark
ratingVariant _ Star_0 = Dark ratingVariant _ Star_0 = Dark
ratingVariant _ _ = Dark ratingVariant _ _ = Dark
ratingClassName :: Star -> Star -> String
ratingClassName Star_0 Star_0 = "rating-group__action" ratingClassName Star_0 Star_0 = "rating-group__action"
ratingClassName _ Star_0 = "rating-group__action" ratingClassName _ Star_0 = "rating-group__action"
ratingClassName _ _ = "rating-group__star" ratingClassName _ _ = "rating-group__star"
...@@ -118,10 +122,12 @@ ratingSimpleLoaderCpt = here.component "ratingSimpleLoader" cpt where ...@@ -118,10 +122,12 @@ ratingSimpleLoaderCpt = here.component "ratingSimpleLoader" cpt where
useLoader { errorHandler useLoader { errorHandler
, loader: loadDocumentContext session , loader: loadDocumentContext session
, path: { docId, corpusId } , path: { docId, corpusId }
, render: \{ nc_category } -> renderRatingSimple { docId , render: \{ nc_category } -> do
, corpusId let category = fromMaybe UnRead $ decodeCategory <$> nc_category
, category: fromMaybe 0 nc_category renderRatingSimple { docId
, session } [] } , corpusId
, category
, session } [] }
where where
errorHandler err = do errorHandler err = do
here.warn2 "[pageLayout] RESTError" err here.warn2 "[pageLayout] RESTError" err
...@@ -139,7 +145,7 @@ loadDocumentContext session { docId, corpusId } = getNodeContext session docId c ...@@ -139,7 +145,7 @@ loadDocumentContext session { docId, corpusId } = getNodeContext session docId c
type RenderRatingSimpleProps = type RenderRatingSimpleProps =
( docId :: NodeID ( docId :: NodeID
, corpusId :: NodeID , corpusId :: NodeID
, category :: Int , category :: Category
, session :: Session ) , session :: Session )
renderRatingSimple :: R2.Component RenderRatingSimpleProps renderRatingSimple :: R2.Component RenderRatingSimpleProps
...@@ -151,17 +157,17 @@ renderRatingSimpleCpt = here.component "renderRatingSimple" cpt where ...@@ -151,17 +157,17 @@ renderRatingSimpleCpt = here.component "renderRatingSimple" cpt where
, category , category
, session , session
} _ = do } _ = do
score <- T.useBox $ decodeStar category categoryS <- T.useBox category
pure $ ratingSimple { docId pure $ ratingSimple { docId
, corpusId , corpusId
, score , category: categoryS
, session } [] , session } []
type RatingSimpleProps = type RatingSimpleProps =
( docId :: NodeID ( docId :: NodeID
, corpusId :: NodeID , corpusId :: NodeID
, score :: T.Box Star , category :: T.Box Category
, session :: Session ) , session :: Session )
ratingSimple :: R2.Component RatingSimpleProps ratingSimple :: R2.Component RatingSimpleProps
...@@ -170,19 +176,21 @@ ratingSimpleCpt :: R.Component RatingSimpleProps ...@@ -170,19 +176,21 @@ ratingSimpleCpt :: R.Component RatingSimpleProps
ratingSimpleCpt = here.component "ratingSimple" cpt where ratingSimpleCpt = here.component "ratingSimple" cpt where
cpt { docId cpt { docId
, corpusId , corpusId
, score , category
, session , session
} _ = do } _ = do
score' <- T.useLive T.unequal score category' <- T.useLive T.unequal category
let star' = cat2star category'
let let
onClick c _ = do onClick c _ = do
let c' = score' == c ? clickAgain c $ c -- let c' = score' == c ? clickAgain c $ c
let c' = categoryNextState category' c
-- setLocalCategories $ Map.insert r._id c' -- setLocalCategories $ Map.insert r._id c'
launchAff_ do launchAff_ do
_ <- updateNodeContextCategory session docId corpusId $ star2score c' _ <- updateNodeContextCategory session docId corpusId $ cat2score c'
liftEffect $ T.write_ c' score liftEffect $ T.write_ c' category
pure unit pure unit
pure $ pure $
...@@ -190,17 +198,17 @@ ratingSimpleCpt = here.component "ratingSimple" cpt where ...@@ -190,17 +198,17 @@ ratingSimpleCpt = here.component "ratingSimple" cpt where
{ className: "rating-group" } $ { className: "rating-group" } $
stars <#> \s -> stars <#> \s ->
B.iconButton B.iconButton
{ name: ratingIcon score' s { name: ratingIcon category' s
, callback: onClick s , callback: onClick s
, overlay: false , overlay: false
, variant: ratingVariant score' s , variant: ratingVariant star' s
, className: ratingClassName score' s , className: ratingClassName star' s
} }
newtype RatingQuery = newtype RatingQuery =
RatingQuery { nodeIds :: Array Int RatingQuery { nodeIds :: Array Int
, rating :: Star , rating :: Category
} }
derive instance Generic RatingQuery _ derive instance Generic RatingQuery _
instance JSON.WriteForeign RatingQuery where instance JSON.WriteForeign RatingQuery where
...@@ -267,6 +275,9 @@ icon cat b = btn b $ "fa fa-" <> (color $ size b $ icon' cat b) ...@@ -267,6 +275,9 @@ icon cat b = btn b $ "fa fa-" <> (color $ size b $ icon' cat b)
icon' Favorite false = "heart-o" icon' Favorite false = "heart-o"
icon' Favorite true = "heart" icon' Favorite true = "heart"
icon' ToCite false = "quote-left-o"
icon' ToCite true = "quote-left"
size :: Boolean -> String -> String size :: Boolean -> String -> String
size true s = s <> " btn-lg" size true s = s <> " btn-lg"
size false s = s <> " btn-sm" size false s = s <> " btn-sm"
......
module Gargantext.Components.Category.Types where module Gargantext.Components.Category.Types where
import Data.Generic.Rep (class Generic) import Data.Bounded.Generic (genericTop, genericBottom)
import Data.Enum (class Enum, class BoundedEnum, succ, pred, fromEnum, toEnumWithDefaults)
import Data.Enum.Generic (genericPred, genericSucc, genericCardinality, genericFromEnum, genericToEnum)
import Data.Eq.Generic (genericEq) import Data.Eq.Generic (genericEq)
import Data.Generic.Rep (class Generic)
import Data.Maybe (fromMaybe)
import Data.Ord.Generic (genericCompare)
import Data.Show.Generic (genericShow) import Data.Show.Generic (genericShow)
import Simple.JSON as JSON import Simple.JSON as JSON
import Gargantext.Prelude import Gargantext.Prelude
------------------------------------------------------------------------ ------------------------------------------------------------------------
data Star = Star_0 | Star_1 | Star_2 | Star_3 | Star_4 data Category = Trash | UnRead | Checked | Topic | Favorite | ToCite
{-
stars :: Array Star - `UnRead` is assigned initially for new docs
stars = [Star_0, Star_1, Star_2, Star_3, Star_4] - After reading a doc, `Checked` should be assigned automatically
- Both `Trash` and `UnRead` map to 0 stars in the doc list
derive instance Generic Star _ -}
instance Show Star where show = genericShow
instance Eq Star where eq = genericEq
instance JSON.ReadForeign Star where
readImpl f = do
inst <- JSON.readImpl f
pure $ decodeStar inst
instance JSON.WriteForeign Star where writeImpl = JSON.writeImpl <<< star2score
decodeStar :: Int -> Star
decodeStar 0 = Star_0
decodeStar 1 = Star_1
decodeStar 2 = Star_2
decodeStar 3 = Star_3
decodeStar 4 = Star_4
decodeStar _ = Star_4
star2score :: Star -> Int
star2score Star_0 = 0
star2score Star_1 = 1
star2score Star_2 = 2
star2score Star_3 = 3
star2score Star_4 = 4
clickAgain :: Star -> Star
clickAgain Star_0 = Star_1
clickAgain s = decodeStar (star2score s - 1)
------------------------------------------------------------------------
data Category = Trash | UnRead | Checked | Topic | Favorite
categories :: Array Category categories :: Array Category
categories = [Trash, UnRead, Checked, Topic, Favorite] categories = [Trash, UnRead, Checked, Topic, Favorite, ToCite]
derive instance Generic Category _ derive instance Generic Category _
instance Ord Category where compare = genericCompare
instance Enum Category where
pred = genericPred
succ = genericSucc
instance Bounded Category where
bottom = genericBottom
top = genericTop
instance BoundedEnum Category where
cardinality = genericCardinality
fromEnum = genericFromEnum
toEnum = genericToEnum
instance Show Category where show = genericShow instance Show Category where show = genericShow
instance Eq Category where eq = genericEq instance Eq Category where eq = genericEq
instance JSON.ReadForeign Category where instance JSON.ReadForeign Category where
...@@ -58,26 +43,75 @@ instance JSON.ReadForeign Category where ...@@ -58,26 +43,75 @@ instance JSON.ReadForeign Category where
pure $ decodeCategory inst pure $ decodeCategory inst
instance JSON.WriteForeign Category where writeImpl = JSON.writeImpl <<< cat2score instance JSON.WriteForeign Category where writeImpl = JSON.writeImpl <<< cat2score
catSucc :: Category -> Category
catSucc c = fromMaybe ToCite $ succ c
catPred :: Category -> Category
catPred c = fromMaybe Trash $ pred c
clickAgain :: Category -> Category
clickAgain _ = UnRead
-- | `categoryNextState :: current -> clicked -> new State`
categoryNextState :: Category -> Star -> Category
categoryNextState Trash Star_0 = UnRead
categoryNextState _ Star_0 = Trash
categoryNextState current clicked =
if (cat2star current) == clicked then
clickAgain current
else
star2catSimple clicked
favCategory :: Category -> Category favCategory :: Category -> Category
favCategory Favorite = Topic favCategory Favorite = Topic
favCategory _ = Favorite favCategory _ = Favorite
trashCategory :: Category -> Category trashCategory :: Category -> Category
trashCategory _ = Trash trashCategory = const Trash
-- TODO: ?
--trashCategory Trash = UnRead
decodeCategory :: Int -> Category decodeCategory :: Int -> Category
decodeCategory 0 = Trash decodeCategory = toEnumWithDefaults UnRead UnRead
decodeCategory 1 = UnRead
decodeCategory 2 = Checked
decodeCategory 3 = Topic
decodeCategory 4 = Favorite
decodeCategory _ = UnRead
cat2score :: Category -> Int cat2score :: Category -> Int
cat2score Trash = 0 cat2score = fromEnum
cat2score UnRead = 1
cat2score Checked = 2 ------------------------------------------------------------------------
cat2score Topic = 3 -- | This is just a helper to visualize categories.
cat2score Favorite = 4 data Star = Star_0 | Star_1 | Star_2 | Star_3 | Star_4
stars :: Array Star
stars = [Star_0, Star_1, Star_2, Star_3, Star_4]
derive instance Generic Star _
instance Show Star where show = genericShow
instance Eq Star where eq = genericEq
instance Ord Star where compare = genericCompare
instance Enum Star where
pred = genericPred
succ = genericSucc
instance Bounded Star where
bottom = genericBottom
top = genericTop
instance BoundedEnum Star where
cardinality = genericCardinality
fromEnum = genericFromEnum
toEnum = genericToEnum
cat2star :: Category -> Star
cat2star Trash = Star_0
cat2star UnRead = Star_0
cat2star Checked = Star_1
cat2star Topic = Star_2
cat2star Favorite = Star_3
cat2star ToCite = Star_4
-- | This is a "reverse" of `cat2star`
star2catSimple :: Star -> Category
star2catSimple Star_0 = UnRead
star2catSimple Star_1 = Checked
star2catSimple Star_2 = Topic
star2catSimple Star_3 = Favorite
star2catSimple Star_4 = ToCite
This diff is collapsed.
...@@ -7,14 +7,14 @@ import Data.Generic.Rep (class Generic) ...@@ -7,14 +7,14 @@ import Data.Generic.Rep (class Generic)
import Data.Map (Map) import Data.Map (Map)
import Data.Maybe (Maybe(..), fromMaybe) import Data.Maybe (Maybe(..), fromMaybe)
import Data.Tuple (Tuple(..)) import Data.Tuple (Tuple(..))
import Gargantext.Components.Category.Types (Category, Star(..), decodeStar) import Gargantext.Components.Category.Types (Category(..), Star(..), decodeCategory)
import Simple.JSON as JSON import Simple.JSON as JSON
data Action data Action
= MarkCategory Int Category = MarkCategory Int Category
type DocumentsViewT = type DocumentsViewT =
( category :: Star ( category :: Category
, date :: Int , date :: Int
, ngramCount :: Maybe Int , ngramCount :: Maybe Int
, score :: Maybe Int , score :: Maybe Int
...@@ -65,8 +65,8 @@ type ResponseT = ...@@ -65,8 +65,8 @@ type ResponseT =
, score :: Maybe Int , score :: Maybe Int
, title :: String ) , title :: String )
newtype Response = Response newtype Response = Response
{ cid :: Int { cid :: Int
, category :: Star , category :: Category
| ResponseT | ResponseT
} }
...@@ -74,7 +74,7 @@ instance JSON.ReadForeign Response where ...@@ -74,7 +74,7 @@ instance JSON.ReadForeign Response where
readImpl f = do readImpl f = do
{ category, id, hyperdata, ngramCount, score, title } :: { category :: Int, id :: Int | ResponseT } <- JSON.readImpl f { category, id, hyperdata, ngramCount, score, title } :: { category :: Int, id :: Int | ResponseT } <- JSON.readImpl f
--pure $ Response { category: decodeCategory category, cid, hyperdata, ngramCount, score, title } --pure $ Response { category: decodeCategory category, cid, hyperdata, ngramCount, score, title }
pure $ Response { category: decodeStar category pure $ Response { category: decodeCategory category
, cid: id , cid: id
, hyperdata , hyperdata
, ngramCount , ngramCount
...@@ -110,7 +110,7 @@ sampleData' = DocumentsView { _id : 1 ...@@ -110,7 +110,7 @@ sampleData' = DocumentsView { _id : 1
, date : 2010 , date : 2010
, title : "title" , title : "title"
, source : Just "source" , source : Just "source"
, category : Star_1 , category : UnRead
, ngramCount : Just 1 , ngramCount : Just 1
, score: Just 1 } , score: Just 1 }
...@@ -121,7 +121,7 @@ sampleData = map (\(Tuple t s) -> DocumentsView { _id : 1 ...@@ -121,7 +121,7 @@ sampleData = map (\(Tuple t s) -> DocumentsView { _id : 1
, date : 2017 , date : 2017
, title: t , title: t
, source: Just s , source: Just s
, category : Star_1 , category : UnRead
, ngramCount : Just 10 , ngramCount : Just 10
, score: Just 1 }) sampleDocuments , score: Just 1 }) sampleDocuments
......
...@@ -16,7 +16,6 @@ import Gargantext.Components.AutoUpdate (autoUpdate) ...@@ -16,7 +16,6 @@ import Gargantext.Components.AutoUpdate (autoUpdate)
import Gargantext.Components.Bootstrap as B import Gargantext.Components.Bootstrap as B
import Gargantext.Components.Bootstrap.Types (ComponentStatus(..), SpinnerTheme(..)) import Gargantext.Components.Bootstrap.Types (ComponentStatus(..), SpinnerTheme(..))
import Gargantext.Components.Category (ratingSimpleLoader) import Gargantext.Components.Category (ratingSimpleLoader)
import Gargantext.Components.Category.Types (decodeStar)
import Gargantext.Components.Document.Types (DocPath, Document(..), LoadedData, initialState) import Gargantext.Components.Document.Types (DocPath, Document(..), LoadedData, initialState)
import Gargantext.Components.NgramsTable.AutoSync (useAutoSync) import Gargantext.Components.NgramsTable.AutoSync (useAutoSync)
import Gargantext.Components.Node (NodePoly(..)) import Gargantext.Components.Node (NodePoly(..))
......
...@@ -395,6 +395,7 @@ pageCpt = here.component "page" cpt ...@@ -395,6 +395,7 @@ pageCpt = here.component "page" cpt
wrapColElts = const identity wrapColElts = const identity
-- TODO: how to interprete other scores? -- TODO: how to interprete other scores?
gi Trash = "fa fa-star-empty" gi Trash = "fa fa-star-empty"
gi UnRead = "fa fa-star-empty"
gi _ = "fa fa-star" gi _ = "fa fa-star"
documentUrl id { listId, nodeId } = documentUrl id { listId, 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