JobOffer.purs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. module LinkedIn.Jobs.JobOffer where
  2. import Prelude
  3. import Data.Argonaut.Decode (class DecodeJson)
  4. import Data.Argonaut.Decode.Generic (genericDecodeJson)
  5. import Data.Argonaut.Encode (class EncodeJson)
  6. import Data.Argonaut.Encode.Generic (genericEncodeJson)
  7. import Data.Either (Either, note)
  8. import Data.Generic.Rep (class Generic)
  9. import Data.Lens (findOf)
  10. import Data.List (List(..), (:))
  11. import Data.List.Types (NonEmptyList(..))
  12. import Data.Maybe (Maybe(..), isJust)
  13. import Data.NonEmpty ((:|))
  14. import Data.Show.Generic (genericShow)
  15. import LinkedIn.UI.Basic.Types (JobFlexibility)
  16. import LinkedIn.UI.Components.JobsUnifiedTopCard (JobsUnifiedTopCardElement, TopCardInsight(..), TopCardInsightContent(..), _top_to_action_buttons, _top_to_insights, toHeader, toPrimaryDescriptionLink, toPrimaryDescriptionText)
  17. import LinkedIn.UI.Elements.Types (UIElement(..))
  18. import LinkedIn.UI.Strings.Types (UIString(..))
  19. type JobOfferObject = {
  20. title :: String,
  21. companyName :: String,
  22. companyLink :: String,
  23. location :: Maybe String,
  24. flexibility :: Maybe JobFlexibility,
  25. companyDomain :: Maybe String,
  26. companySize :: Maybe String,
  27. hasSimplifiedApplicationProcess :: Boolean
  28. }
  29. newtype JobOffer = JobOffer JobOfferObject
  30. derive instance Eq JobOffer
  31. derive instance Generic JobOffer _
  32. instance Show JobOffer where show = genericShow
  33. instance EncodeJson JobOffer where encodeJson a = genericEncodeJson a
  34. instance DecodeJson JobOffer where decodeJson a = genericDecodeJson a
  35. fromUI ∷ JobsUnifiedTopCardElement UIElement → Either String JobOffer
  36. fromUI card = ado
  37. title <- note "No title found" $ extractTitle header
  38. companyName <- note "No company found" $ extractCompany link
  39. companyLink <- note "No company link found" $ extractCompanyLink link
  40. in
  41. JobOffer {
  42. title,
  43. companyName,
  44. companyLink,
  45. location: extractLocation primaryDescText,
  46. flexibility: extractJobRemote =<< jobInsight,
  47. companyDomain: extractCompanyDomain =<< companyInsight,
  48. companySize: extractCompanySize =<< companyInsight,
  49. hasSimplifiedApplicationProcess: isJust $ extractSimplifiedApplicationProcess =<< applyButton
  50. }
  51. where
  52. header = toHeader card
  53. link = toPrimaryDescriptionLink card
  54. primaryDescText = toPrimaryDescriptionText card
  55. jobInsight = getInsight "job" card
  56. companyInsight = getInsight "company" card
  57. applyButton = findOf _top_to_action_buttons isApply card
  58. where
  59. isApply = case _ of
  60. UIButton {mainClass: Just main} -> main == "jobs-apply-button"
  61. _ -> false
  62. getInsight ∷ String → JobsUnifiedTopCardElement UIElement → Maybe (TopCardInsight UIElement)
  63. getInsight iType card = findOf _top_to_insights (isIcon iType) card
  64. where
  65. isIcon icon = case _ of
  66. TopCardInsight {icon: UIIcon i} -> i == icon
  67. _ -> false
  68. extractTitle :: UIElement -> Maybe String
  69. extractTitle = case _ of
  70. UIElement (UIStringPlain str) -> Just str
  71. _ -> Nothing
  72. extractCompany :: UIElement -> Maybe String
  73. extractCompany = case _ of
  74. UILink _ (UIStringPlain str) -> Just str
  75. _ -> Nothing
  76. extractCompanyLink :: UIElement -> Maybe String
  77. extractCompanyLink = case _ of
  78. UILink link _ -> Just link
  79. _ -> Nothing
  80. extractCompanyDomain ∷ TopCardInsight UIElement → Maybe String
  81. extractCompanyDomain = case _ of
  82. TopCardInsight {content: TopCardInsightContentSingle (UIElement (UIStringDotSeparated (NonEmptyList (_ :| (UIStringPlain str) : _))))} -> Just str
  83. _ -> Nothing
  84. extractCompanySize ∷ TopCardInsight UIElement → Maybe String
  85. extractCompanySize = case _ of
  86. TopCardInsight {content: TopCardInsightContentSingle (UIElement (UIStringDotSeparated (NonEmptyList(UIStringPlain str :| _))))} -> Just str
  87. _ -> Nothing
  88. extractLocation :: UIElement -> Maybe String
  89. extractLocation = case _ of
  90. UIElement (UIStringDotSeparated (NonEmptyList ((UIStringPlain str) :| Nil))) -> Just str
  91. _ -> Nothing
  92. extractJobRemote :: TopCardInsight UIElement -> Maybe JobFlexibility
  93. extractJobRemote = case _ of
  94. TopCardInsight {content: TopCardInsightContentSecondary {primary: (UIElement (UIStringJobFlex flex))}} -> Just flex
  95. _ -> Nothing
  96. extractSimplifiedApplicationProcess ∷ UIElement → Maybe Boolean
  97. extractSimplifiedApplicationProcess = case _ of
  98. UIButton {role: Nothing} -> Just true
  99. _ -> Nothing