aboutsummaryrefslogtreecommitdiff
path: root/client/src/Util/Ajax.hs
blob: 1e8e4c7d4f9eed547ecc75e4f6d9d946eb3fc853 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
module Util.Ajax
  ( post
  ) where

import           Data.Aeson (ToJSON)
import           Data.Text  (Text)
import           Reflex.Dom (Event, MonadWidget)
import qualified Reflex.Dom as R

post :: forall t m a. (MonadWidget t m, ToJSON a) => Text -> Event t a -> m (Event t (Either Text Text))
post url input =
  fmap getResult <$> R.performRequestAsync xhrRequest
  where xhrRequest = R.postJson url <$> input
        getResult response =
          case R._xhrResponse_responseText response of
            Just responseText ->
              if R._xhrResponse_status response == 200
                then Right responseText
                else Left responseText
            _ -> Left "NoKey"