/
form.js
48 lines (44 loc) · 1.19 KB
/
form.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// @flow
import type { CheckoutPayload } from "../flow/ecommerceTypes"
import React from "react"
/**
* Creates a POST form with hidden input fields
* @param url the url for the form action
* @param payload Each key value pair will become an input field
*/
export function createCyberSourceForm(
url: string,
payload: CheckoutPayload
): HTMLFormElement {
const form = document.createElement("form")
form.setAttribute("action", url)
form.setAttribute("method", "post")
form.setAttribute("class", "cybersource-payload")
for (const key: string of Object.keys(payload)) {
const value = payload[key]
const input = document.createElement("input")
input.setAttribute("name", key)
input.setAttribute("value", value)
input.setAttribute("type", "hidden")
form.appendChild(input)
}
return form
}
export const formatErrors = (
errors: string | Object | null
): React$Element<*> | null => {
if (!errors) {
return null
}
let errorString
if (typeof errors === "object") {
if (errors.items) {
errorString = errors.items[0]
} else {
errorString = errors[0]
}
} else {
errorString = errors
}
return <div className="error">{errorString}</div>
}