소스 검색

WIP Implement 90% of query functions for TopCard

jherve 1 년 전
부모
커밋
1937c60eb9
2개의 변경된 파일98개의 추가작업 그리고 2개의 파일을 삭제
  1. 75 1
      src/LinkedIn/JobsUnifiedTopCard.purs
  2. 23 1
      src/LinkedIn/QueryRunner.purs

파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 75 - 1
src/LinkedIn/JobsUnifiedTopCard.purs


+ 23 - 1
src/LinkedIn/QueryRunner.purs

@@ -2,7 +2,8 @@ module LinkedIn.QueryRunner where
 
 import Prelude
 
-import Control.Monad.Except (ExceptT(..), mapExceptT, runExceptT)
+import Control.Monad.Except (ExceptT(..), mapExceptT, runExceptT, throwError)
+import Data.Array as A
 import Data.Either (Either(..), note)
 import Data.Generic.Rep (class Generic)
 import Data.List.Types (NonEmptyList)
@@ -12,11 +13,14 @@ import Data.Traversable (traverse)
 import Effect (Effect)
 import LinkedIn.Utils as U
 import Web.DOM (Node)
+import Web.DOM.Node as N
+import Web.DOM.NodeList as NL
 
 data QueryError =
   QNodeNotFoundError String
   | QNodeListNotFoundError String
   | QTextNotFoundError
+  | QChooseError
 
 derive instance Generic QueryError _
 derive instance Eq QueryError
@@ -41,6 +45,14 @@ queryOne selector node = ExceptT $ do
   maybeNode <- U.queryOne selector node
   pure $ note (QNodeNotFoundError selector) maybeNode
 
+-- TODO : A correct version of this function, which is just plain false here
+queryText ∷ QueryRunner Node
+queryText n = ExceptT $ do
+  children <- N.childNodes n
+  childrenArr <- NL.toArray children
+
+  pure $ note QTextNotFoundError $ A.head childrenArr
+
 queryAll ∷ String → QueryRunner (NonEmptyList Node)
 queryAll selector node = ExceptT $ do
   maybeNodes <- U.queryAll selector node
@@ -51,3 +63,13 @@ subQueryMany query selector n = traverse query =<< queryAll selector n
 
 subQueryOne ∷ ∀ a. QueryRunner a → String → QueryRunner a
 subQueryOne query selector n = query =<< queryOne selector n
+
+chooseOne ∷ ∀ a t m. Monad m ⇒ (t → ExceptT QueryError m a) → (t → ExceptT QueryError m a) → (t → ExceptT QueryError m a)
+chooseOne q1 q2 n = do
+  maybeN1 <- ignoreNotFound $ q1 n
+  maybeN2 <- ignoreNotFound $ q2 n
+
+  case maybeN1, maybeN2 of
+    Nothing, Nothing -> throwError QChooseError
+    Just n1, _ -> pure n1
+    _, Just n2 -> pure n2