|
|
@@ -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
|