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"