Project.purs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. module LinkedIn.Profile.Project where
  2. import Prelude
  3. import Data.Argonaut.Encode (class EncodeJson)
  4. import Data.Argonaut.Encode.Generic (genericEncodeJson)
  5. import Data.Either (Either, note)
  6. import Data.Generic.Rep (class Generic)
  7. import Data.List as L
  8. import Data.Maybe (Maybe(..))
  9. import Data.Show.Generic (genericShow)
  10. import LinkedIn.UI.Basic.Types (TimeSpan)
  11. import LinkedIn.UI.Components.ArtDecoCard (ArtDecoCardElement, toCenterContent, toHeaderBold, toHeaderNormal)
  12. import LinkedIn.UI.Elements.Types (UIElement(..))
  13. import LinkedIn.UI.Strings.Types (UIString(..))
  14. data Project = Project {
  15. name :: String,
  16. timeSpan :: Maybe TimeSpan,
  17. description :: Maybe String
  18. }
  19. derive instance Generic Project _
  20. instance Show Project where
  21. show = genericShow
  22. instance EncodeJson Project where
  23. encodeJson a = genericEncodeJson a
  24. fromUI ∷ ArtDecoCardElement UIElement → Either String Project
  25. fromUI card = ado
  26. name <- note "No position found" $ extractName bold
  27. in
  28. Project {
  29. name,
  30. timeSpan: extractTimeSpan =<< normal,
  31. description: extractDescription =<< L.index content 0
  32. }
  33. where
  34. normal = toHeaderNormal card
  35. content = toCenterContent card
  36. bold = toHeaderBold card
  37. extractName :: UIElement -> Maybe String
  38. extractName = case _ of
  39. UIElement (UIStringPlain str) -> Just str
  40. _ -> Nothing
  41. extractTimeSpan ∷ UIElement → Maybe TimeSpan
  42. extractTimeSpan = case _ of
  43. UIElement (UIStringTimeSpan s) -> Just s
  44. UIElement (UIStringDotSeparated (UIStringTimeSpan s) _) -> Just s
  45. _ -> Nothing
  46. extractDescription ∷ UIElement → Maybe String
  47. extractDescription = case _ of
  48. UIElement (UIStringPlain d) -> Just d
  49. _ -> Nothing