Skip to content

Instantly share code, notes, and snippets.

@mithereal
Last active October 25, 2018 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mithereal/0d7ba10a916f5800cf00fe8154716f5c to your computer and use it in GitHub Desktop.
Save mithereal/0d7ba10a916f5800cf00fe8154716f5c to your computer and use it in GitHub Desktop.
type location = {
street_number: string,
route: string,
locality: string,
administrative_area_level_1: string,
country: string,
postal_code: string
}
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)
}
type state = {
count: int,
inputs: list(input),
altinputs: list(input),
websocket_id: string
};
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 list_array = Array.of_list(encoded_locations);
let enc = list_array
|> Json.Encode.stringArray;
Js.log(encoded_locations);
let payload = Js.Dict.empty();
Js.Dict.set(payload, "id", Js.Json.string(websocket_id));
Js.Dict.set(payload, "locations", Js.Json.string(enc));
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;
};
let payload = Js.Dict.empty();
Js.Dict.set(payload, "id", Js.Json.string(websocket_id));
Js.Dict.set(payload, "locations", Js.Json.string(enc));
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;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment