AddCorpusview.purs 5.45 KB
Newer Older
Abinaya Sudhir's avatar
Abinaya Sudhir committed
1 2 3 4 5 6 7 8 9 10 11
module AddCorpusview where

import Control.Monad.Aff (Aff, attempt)
import Control.Monad.Aff.Class (liftAff)
import Control.Monad.Aff.Console (log)
import Control.Monad.Cont.Trans (lift)
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Class (liftEff)
import Control.Monad.Eff.Console (CONSOLE)
import DOM (DOM)
import DOM.HTML (window)
Abinaya Sudhir's avatar
Abinaya Sudhir committed
12
import DOM.HTML.Location (host)
Abinaya Sudhir's avatar
Abinaya Sudhir committed
13 14 15 16 17
import DOM.HTML.Window (localStorage)
import DOM.WebStorage.Storage (getItem, setItem)
import Data.Argonaut (class DecodeJson, class EncodeJson, decodeJson, encodeJson, jsonEmptyObject, (.?), (:=), (~>))
import Data.Either (Either(..))
import Data.HTTP.Method (Method(..))
18 19
import Data.Lens (Lens', Prism', lens, over)
import Data.List (List, fold, fromFoldable, toUnfoldable)
20
import Data.Maybe (Maybe(..), fromMaybe)
Abinaya Sudhir's avatar
Abinaya Sudhir committed
21
import Data.MediaType.Common (applicationJSON)
22
import Data.Tuple (Tuple(..))
Abinaya Sudhir's avatar
Abinaya Sudhir committed
23 24 25
import Network.HTTP.Affjax (AJAX, affjax, defaultRequest)
import Network.HTTP.RequestHeader (RequestHeader(..))
import Prelude hiding (div)
26
import React (ReactElement)
Abinaya Sudhir's avatar
Abinaya Sudhir committed
27
import React.DOM (a, button, div, form, h2, h3, h4, i, input, label, li, p, span, text, ul)
Abinaya Sudhir's avatar
Abinaya Sudhir committed
28 29
import React.DOM.Props (_id, _type, className, href, maxLength, name, onClick, onInput, placeholder, target, value)
import Routing.Hash.Aff (setHash)
30
import Thermite (PerformAction, Render, Spec, _render, cotransform, focus, foreach, modifyState, simpleSpec, withState)
Abinaya Sudhir's avatar
Abinaya Sudhir committed
31 32 33
import Unsafe.Coerce (unsafeCoerce)


34

35
type State =
Abinaya Sudhir's avatar
Abinaya Sudhir committed
36 37
  { select_database :: Boolean
  , unselect_database :: Boolean  --  dummy state
38 39 40 41 42
  , response :: Array Response
  }

newtype Response = Response
  {
43 44
    count :: Int
  , name :: String
Abinaya Sudhir's avatar
Abinaya Sudhir committed
45 46 47
  }

initialState :: State
48
initialState =
Abinaya Sudhir's avatar
Abinaya Sudhir committed
49 50 51
  {
    select_database : true
  , unselect_database : true
52
  , response : []
Abinaya Sudhir's avatar
Abinaya Sudhir committed
53 54 55 56 57 58 59
  }


data Action
  = NoOp
  | SelectDatabase Boolean
  | UnselectDatabase Boolean
60
  | LoadDatabaseDetails
61
  | GO
Abinaya Sudhir's avatar
Abinaya Sudhir committed
62 63 64 65 66 67 68


performAction :: forall eff props. PerformAction (console :: CONSOLE, ajax :: AJAX,dom::DOM | eff) State props Action
performAction NoOp _ _ = void do
  modifyState id

performAction (SelectDatabase selected) _ _ = void do
69
  modifyState \( state) -> state { select_database = selected }
Abinaya Sudhir's avatar
Abinaya Sudhir committed
70 71 72


performAction (UnselectDatabase unselected) _ _ = void do
73 74 75
  modifyState \( state) ->  state { unselect_database = unselected }

performAction (LoadDatabaseDetails) _ _ = void do
76
  res <- lift $ getDatabaseDetails $ QueryString{query_query: "string",query_name: ["Pubmed"]}
77 78 79 80 81
  case res of
     Left err -> cotransform $ \(state) ->  state
     Right resData -> do
       cotransform $ \(state) -> state {response  = resData}

82 83
performAction GO _ _ = void do
  lift $ setHash "/docView"
84
  modifyState id
Abinaya Sudhir's avatar
Abinaya Sudhir committed
85 86 87 88 89 90 91 92



addcorpusviewSpec :: forall props eff . Spec (console::CONSOLE, ajax::AJAX, dom::DOM | eff) State props Action
addcorpusviewSpec = simpleSpec performAction render
  where
    render :: Render State props Action
    render dispatch _ state _ =
93 94 95
      [
        div [className "container"]
        [
96
          div [className "jumbotron"]
97 98 99 100 101 102 103 104 105 106
          [ div [className "row"]
           [
             div [className "col-md-3"]
             [
               h3 [] [text "Treeview"]
             ]
           , div [className "col-md-9"]
             [
               h3 [] [text "Corpusview"]
             , ul [className "list-group"] $ map fn1 state.response
107
             , button [onClick \_ -> dispatch GO] [text "GO"]
108 109 110
             ]

           ]
111
          ]
112 113
        ]
      ]
114 115 116 117 118 119 120
      where
        fn1 (Response o) =
          li [className "list-group-item justify-content-between"]
          [
          span [] [text  o.name]
          ,  span [className "badge badge-default badge-pill"] [ text $ show o.count]
          ]
Abinaya Sudhir's avatar
Abinaya Sudhir committed
121 122


Abinaya Sudhir's avatar
Abinaya Sudhir committed
123

124 125 126 127 128 129 130
-- fn1 :: Response -> ReactElement
-- fn1 (Response o) =
--   li [className "list-group-item justify-content-between"]
--   [
--     span [] [text  o.name]
--   ,  span [className "badge badge-default badge-pill"] [ text $ show o.count]
--   ]
131

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

newtype QueryString = QueryString
  {
    query_query :: String
  ,  query_name :: Array String
  }

queryString :: QueryString
queryString = QueryString
  {
  query_query: "string",
  query_name: [
    "Pubmed"
  ]
  }


instance encodeJsonQueryString :: EncodeJson QueryString where
  encodeJson (QueryString obj) =
    "query_query"       := obj.query_query
    ~> "query_name"        := obj.query_name
    ~> jsonEmptyObject



getDatabaseDetails :: forall eff.  QueryString -> Aff (console::CONSOLE,ajax :: AJAX | eff) (Either String (Array Response))
getDatabaseDetails reqBody = do
159
  let token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1MTk5OTg1ODMsInVzZXJfaWQiOjUsImVtYWlsIjoiYWxleGFuZHJlLmRlbGFub2VAaXNjcGlmLmZyIiwidXNlcm5hbWUiOiJkZXZlbG9wZXIifQ.Os-3wuFNSmRIxCZi98oFNBu2zqGc0McO-dgDayozHJg"
Abinaya Sudhir's avatar
Abinaya Sudhir committed
160
  affResp <- liftAff $ attempt $ affjax defaultRequest
161
    { method = Left POST
162
    , url ="http://localhost:8009/count"
Abinaya Sudhir's avatar
Abinaya Sudhir committed
163 164
    , headers =  [ ContentType applicationJSON
                , Accept applicationJSON
165
              --   , RequestHeader "Authorization" $  "Bearer " <> token
Abinaya Sudhir's avatar
Abinaya Sudhir committed
166
            ]
167
    , content = Just $ encodeJson reqBody
Abinaya Sudhir's avatar
Abinaya Sudhir committed
168 169 170
    }
  case affResp of
    Left err -> do
171
      liftAff $ log $ "error" <> show err
Abinaya Sudhir's avatar
Abinaya Sudhir committed
172
      pure $ Left $ show err
173

Abinaya Sudhir's avatar
Abinaya Sudhir committed
174 175 176 177 178 179 180
    Right a -> do
      liftAff $ log $ "POST method Completed"
      liftAff $ log $ "GET /api response: " <> show a.response
      let res = decodeJson a.response
      pure res


181 182 183
instance decodeJsonresponse :: DecodeJson Response where
  decodeJson json = do
    obj <- decodeJson json
184 185 186
    count <- obj .? "count"
    name <- obj .? "name"
    pure $ Response {count,name }