Skip to content

Commit

Permalink
Merge pull request #432 from reflex-frp/aa/formdata-configurable
Browse files Browse the repository at this point in the history
Add variant of postForms where xhrrequest is more configurable
  • Loading branch information
ali-abrar authored Feb 2, 2022
2 parents a5ae0c8 + 122b27a commit 6e3f0ff
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 4 additions & 0 deletions reflex-dom-core/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Revision history for reflex-dom-core

## Unreleased

* Add a variant of `postForms` that allows the `XhrRequestConfig` to be specified.

## 0.7.0.0

* Breaking change: Remove HasJSContext and MonadJS. This change also removes the `js` type parameter from `Prerender`. Change `Prerender js t m` to `Prerender t m`.
Expand Down
16 changes: 14 additions & 2 deletions reflex-dom-core/src/Reflex/Dom/Xhr/FormData.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{-# LANGUAGE OverloadedStrings #-}
module Reflex.Dom.Xhr.FormData
( postForms
, postForms'
, FormValue (..)
, fileToFormValue
)
Expand Down Expand Up @@ -32,13 +33,24 @@ postForms
=> Text -- ^ The target url
-> Event t (f (Map Text (FormValue blob))) -- ^ Maps of text keys and values that will be sent as "FormData"
-> m (Event t (f XhrResponse))
postForms url payload = do
postForms t = postForms' t def

-- | Like 'postForms' but doesn't use a default 'XhrRequestConfig', so a custom one can be provided.
postForms'
:: ( IsBlob blob, MonadJSM (Performable m)
, PerformEvent t m, TriggerEvent t m
, Traversable f)
=> Text -- ^ The target url
-> XhrRequestConfig a
-> Event t (f (Map Text (FormValue blob))) -- ^ Maps of text keys and values that will be sent as "FormData"
-> m (Event t (f XhrResponse))
postForms' url cfg payload = do
performMkRequestsAsync $ ffor payload $ \fs -> for fs $ \u -> liftJSM $ do
fd <- FD.newFormData Nothing
iforM_ u $ \k v -> case v of
FormValue_Text t -> FD.append fd k t
FormValue_File b fn -> FD.appendBlob fd k b fn
return $ xhrRequest "POST" url $ def & xhrRequestConfig_sendData .~ fd
return $ xhrRequest "POST" url $ cfg & xhrRequestConfig_sendData .~ fd

-- | Converts a File (e.g., the output of a 'FileInput') into a 'FormValue'. The filename will be included if it is available.
fileToFormValue :: MonadJSM m => File -> m (FormValue File)
Expand Down

0 comments on commit 6e3f0ff

Please sign in to comment.