Browse Source

Add Foldable + Traversable instances to allow call to toDetached

jherve 1 year ago
parent
commit
cf0ff5cc59
2 changed files with 43 additions and 3 deletions
  1. 14 3
      src/Content.purs
  2. 29 0
      src/LinkedIn/ArtDecoCardAlt.purs

+ 14 - 3
src/Content.purs

@@ -8,17 +8,19 @@ import Data.Either (Either(..))
 import Data.List.NonEmpty (NonEmptyList)
 import Data.List.NonEmpty as NEL
 import Data.Maybe (Maybe(..))
+import Data.Traversable (traverse)
 import Effect (Effect)
 import Effect.Class.Console (logShow)
 import Effect.Console (log)
 import LinkedIn.ArtDecoCard (parseArtDecoCard)
-import LinkedIn.ArtDecoCardAlt (queryArtDecoCardAlt)
+import LinkedIn.ArtDecoCardAlt (ArtDecoCardAltElement, queryArtDecoCardAlt)
 import LinkedIn.ArtDecoTab (parseArtDecoTab)
 import LinkedIn.JobsUnifiedTopCard (parseJobsUnifiedTopCardElement)
 import LinkedIn.Profile.Project as PP
 import LinkedIn.Profile.Skill as PS
 import LinkedIn.Profile.Utils (toUIElement)
 import LinkedIn.Profile.WorkExperience as PWE
+import Web.DOM (Node)
 import Yoga.Tree (Tree, showTree)
 
 main :: Effect Unit
@@ -39,16 +41,25 @@ main = do
   case artDecoCards of
     Nothing -> log "nothing"
     Just l -> do
-      queried <- (\(LinkedInUIElement _ n) -> queryArtDecoCardAlt n) $ NEL.head l
       parsed <- (\(LinkedInUIElement _ n) -> parseArtDecoCard n) $ NEL.head l
       logShow parsed
-      logShow queried
       case parsed of
         Left l -> logShow l
         Right p -> do
           logShow $ toUIElement <$> p
           logShow $ PWE.fromUI p
           logShow $ PP.fromUI p
+
+  case artDecoCards of
+    Nothing -> log "nothing"
+    Just l -> do
+      queried <- (\(LinkedInUIElement _ n) -> queryArtDecoCardAlt n) $ NEL.head l
+      case queried of
+        Left l -> logShow l
+        Right p -> do
+          detached <- traverse toDetached p :: Effect (ArtDecoCardAltElement DetachedNode)
+          logShow detached
+
   case artDecoTabs of
     Nothing -> log "nothing"
     Just l -> do

+ 29 - 0
src/LinkedIn/ArtDecoCardAlt.purs

@@ -5,11 +5,13 @@ import Prelude
 
 import Control.Monad.Writer (Writer, tell)
 import Data.Either (Either(..), hush, note)
+import Data.Foldable (class Foldable, foldMap, foldlDefault, foldrDefault)
 import Data.Generic.Rep (class Generic)
 import Data.List (List)
 import Data.List.Types (NonEmptyList)
 import Data.Maybe (Maybe(..))
 import Data.Show.Generic (genericShow)
+import Data.Traversable (class Traversable, sequence, traverseDefault)
 import Effect (Effect)
 import Effect.Class.Console (log, logShow)
 import LinkedIn (DetachedNode)
@@ -34,6 +36,20 @@ instance Functor ArtDecoCenterHeaderAlt where
   map f (ArtDecoCenterHeaderAlt {bold, normal}) =
     ArtDecoCenterHeaderAlt ({bold: f bold, normal: map f normal})
 
+instance Foldable ArtDecoCenterHeaderAlt where
+  foldMap f (ArtDecoCenterHeaderAlt {bold, normal}) = mempty <> (f bold) <> (foldMap f normal)
+
+  foldl = \x -> foldlDefault x
+  foldr = \x -> foldrDefault x
+
+instance Traversable ArtDecoCenterHeaderAlt where
+  sequence (ArtDecoCenterHeaderAlt {bold, normal}) = ado
+    b <- bold
+    n <- sequence normal
+  in ArtDecoCenterHeaderAlt {bold: b, normal: n}
+
+  traverse = \x -> traverseDefault x
+
 data ArtDecoCardAltElement a = ArtDecoCardAltElement {
   pvs_entity :: ArtDecoCenterHeaderAlt a
 }
@@ -50,6 +66,19 @@ instance Functor ArtDecoCardAltElement where
   map f (ArtDecoCardAltElement {pvs_entity}) =
     ArtDecoCardAltElement ({pvs_entity: map f pvs_entity})
 
+instance Foldable ArtDecoCardAltElement where
+  foldMap f (ArtDecoCardAltElement {pvs_entity}) = mempty <> (foldMap f pvs_entity)
+
+  foldl = \x -> foldlDefault x
+  foldr = \x -> foldrDefault x
+
+instance Traversable ArtDecoCardAltElement where
+  sequence (ArtDecoCardAltElement {pvs_entity}) = ado
+    p <- sequence pvs_entity
+  in ArtDecoCardAltElement {pvs_entity: p}
+
+  traverse = \x -> traverseDefault x
+
 queryArtDecoCardAlt :: Node → Effect (Either QueryError (ArtDecoCardAltElement Node))
 queryArtDecoCardAlt n = runExceptT do
   pvs <- runOne ":scope div.pvs-entity--padded" n