Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple form data values for same name #417

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions reflex-dom-core/src/Reflex/Dom/Xhr/FormData.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Data.Default
import Data.Map (Map)
import Data.Text (Text)
import Data.Traversable
import Data.Foldable
import qualified GHCJS.DOM.FormData as FD
import Foreign.JavaScript.TH
import GHCJS.DOM.File (getName)
Expand All @@ -20,9 +21,11 @@ import Language.Javascript.JSaddle.Monad (MonadJSM, liftJSM)
import Reflex
import Reflex.Dom.Xhr

-- | A FormData value may be a blob/file or a string. The file can optionally be provided with filename.
data FormValue blob = FormValue_Text Text
| FormValue_File blob (Maybe Text) -- maybe filename
-- | A FormData value may be a blob/file, a string, or a list of such values. The file can optionally be provided with filename.
data FormValue blob
= FormValue_Text Text
| FormValue_File blob (Maybe Text) -- maybe filename
| FormValue_List [FormValue blob]

-- | Performs a POST request with the provided FormData payload
postForms
Expand All @@ -35,10 +38,13 @@ postForms
postForms url payload = do
performMkRequestsAsync $ ffor payload $ \fs -> for fs $ \u -> liftJSM $ do
fd <- FD.newFormData Nothing
iforM_ u $ \k v -> case v of
iforM_ u $ \k v -> appendFormValue k v fd
return $ xhrRequest "POST" url $ def & xhrRequestConfig_sendData .~ fd
where
appendFormValue k v fd = 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
FormValue_List vs' -> forM_ vs' $ \v' -> appendFormValue k v' 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