Created
October 17, 2018 21:51
-
-
Save mithereal/53142fbf18798dc8e7db0d503d880f49 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type marshalled_location = {. | |
"administrative_area_level_1": string, | |
"country": string, | |
"locality": string, "postal_code": string, | |
"route": string, | |
"street_number": string | |
} | |
type input = { | |
key: string, | |
id: string, | |
placeholder: string, | |
input_type: string, | |
location: option(marshalled_location) | |
} | |
module Encode = { | |
let location = (location: location) => | |
Json.Encode.( | |
object_([ | |
("administrative_area_level_1", string(location.administrative_area_level_1)), | |
("country", string(location.country)), | |
("locality", string(location.locality)), | |
("route", string(location.route)), | |
("street_number", string(location.street_number)), | |
]) | |
); | |
let input = (input: input) => | |
Json.Encode.( | |
object_([ | |
("key", string(input.key)), | |
("id", string(input.id)), | |
("placeholder", string(input.placeholder)), | |
("input_type", string(input.input_type)) | |
]) | |
); | |
let locations = (inputs) => { | |
let marshalled = List.map((i: input) => | |
input(i) | |
, | |
inputs, | |
); | |
marshalled | |
}; | |
} | |
let request = (websocket_id, inputs) => | |
{ | |
let encoded_locations = Encode.locations(inputs); | |
let payload = Js.Dict.empty(); | |
Js.Dict.set(payload, "id", Js.Json.string(websocket_id)); | |
Js.Dict.set(payload, "locations", Js.Json.string(inputs)); | |
Js.Promise.( | |
Fetch.fetchWithInit( | |
"/query", | |
Fetch.RequestInit.make( | |
~method_=Post, | |
~body=Fetch.BodyInit.make(Js.Json.stringify(Js.Json.object_(payload))), | |
~headers=Fetch.HeadersInit.make({"Content-Type": "application/json"}), | |
() | |
) | |
) | |
|> then_(Fetch.Response.json) | |
) | |
|> ignore; | |
}; | |
/// output in console | |
(2) [{…}, Array(2)] | |
0: | |
id: "pickup_address" | |
input_type: "pickup" | |
key: "0" | |
placeholder: "address 0" | |
__proto__: Object | |
1: Array(2) | |
0: | |
id: "dropoff_address" | |
input_type: "dropoff" | |
key: "0" | |
placeholder: "address 0" | |
__proto__: Object | |
1: 0 | |
length: 2 | |
__proto__: Array(0) | |
length: 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment