Created
October 15, 2018 18:12
-
-
Save mithereal/19ba87bda42aa5069147afb3d8942cd4 to your computer and use it in GitHub Desktop.
This file contains 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
open Googleplaces | |
open AddressShared | |
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 locations = (input: input) => | |
Json.Encode.( | |
object_([ | |
("key", string(input.key)), | |
("id", string(input.id)), | |
("placeholder", string(input.placeholder)), | |
("input_type", string(input.input_type)), | |
("location", input.location |> location), | |
]) | |
); | |
} | |
let request = (websocket_id, inputs) => | |
{ | |
Js.log(inputs); | |
let payload = Js.Dict.empty(); | |
Js.Dict.set(payload, "id", Js.Json.string(websocket_id)); | |
Js.Dict.set(payload, "locations", Json.Encode.list(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; | |
}; | |
let reducer = (action, state) => | |
switch(action) { | |
| INIT => let x = { | |
key: string_of_int(state.count), | |
id: "pickup_address" , | |
placeholder: "address " ++ string_of_int(state.count), | |
input_type: "pickup", | |
location: None | |
}; | |
let y = { | |
key: string_of_int(state.count), | |
id: "dropoff_address" , | |
placeholder: "address " ++ string_of_int(state.count), | |
input_type: "dropoff", | |
location: None | |
}; | |
let modified = [x,y]; | |
ReasonReact.Update({...state, inputs: modified}) | |
| ADDINPUT => | |
let inputs = | |
state.altinputs | |
@ [ | |
{ | |
key: string_of_int(state.count + 1), | |
id: string_of_int(state.count + 1) , | |
placeholder: "address " ++ string_of_int(state.count + 1), | |
input_type: "pickup", | |
location: None | |
}, | |
]; | |
ReasonReact.Update({...state, altinputs: inputs, count: state.count + 1}) | |
| DELETEINPUT(input) => | |
let new_list = Belt.List.keep(state.altinputs, candidate => candidate !== input); | |
ReasonReact.Update({...state, altinputs: new_list, count: state.count - 1}) | |
| MODIFYINPUTTYPE(x, input_type) => ReasonReact.Update({...state, count: state.count - 1}) | |
| MODIFYALTINPUTLOCATION(x, location) => | |
let id = int_of_string(x); | |
let alt = state.altinputs; | |
let el = List.nth(alt,id -1); | |
let newel = { | |
key: el.key, | |
id: el.id, | |
placeholder: el.placeholder, | |
input_type: el.input_type , | |
location: Some(location) | |
}; | |
let altins = List.mapi((index: int, input: input) => | |
if (index == id ) { | |
el | |
}else{ | |
input | |
} | |
, | |
alt, | |
); | |
ReasonReact.Update({...state, altinputs: altins}) | |
| MODIFYINPUTLOCATION(x, location) => | |
let inputs = state.inputs; | |
let el = switch(x){ | |
| "start_autocomplete" => List.hd(inputs) | |
| "end_autocomplete" => let l = List.rev(inputs); | |
List.hd(l) | |
} | |
let newel = { | |
key: el.key, | |
id: el.id, | |
placeholder: el.placeholder, | |
input_type: el.input_type , | |
location: Some(location) | |
}; | |
let altins = List.map((input: input) => | |
if (el.id == input.id ) { | |
newel | |
}else{ | |
input | |
} | |
, | |
inputs, | |
); | |
ReasonReact.Update({...state, inputs: altins}) | |
| BUILDINPUTS(x) => ReasonReact.Update({...state, inputs: [x]}) | |
| CLEARINPUTS => ReasonReact.Update({...state, inputs: []}) | |
| SENDREQUEST(h) => request(h, state.inputs); | |
ReasonReact.NoUpdate | |
}; | |
let component = ReasonReact.reducerComponent("Addresssearch"); | |
let make = (~websocket_id="", _children) => { | |
...component, | |
didMount: self => { | |
self.send(INIT); | |
}, | |
initialState: () => {websocket_id: websocket_id, count: 0, inputs: [], altinputs: []}, | |
reducer, | |
render: ({state, send}) => | |
<div className=Styles.search_address_form> | |
<Input | |
id="start_autocomplete" | |
placeholder="pickup address" | |
appSend = send | |
/> | |
( | |
ReasonReact.array( | |
Array.of_list( | |
List.map( | |
(input: input) => | |
<NewInput | |
input=input | |
appSend=send | |
/>, | |
state.altinputs, | |
), | |
), | |
) | |
) | |
<Input | |
id="end_autocomplete" | |
placeholder="dropoff address" | |
appSend = send | |
/> | |
<button className = Styles.address_add onClick=(_event => send(ADDINPUT))> ( ReasonReact.string("Add Another location") ) </button> | |
<button className = Styles.address_submit onClick=(_event => send(SENDREQUEST(state.websocket_id)))> ( ReasonReact.string("Submit") ) </button> | |
</div> | |
}; | |
open Googleplaces | |
open AddressShared | |
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 locations = (input: input) => | |
Json.Encode.( | |
object_([ | |
("key", string(input.key)), | |
("id", string(input.id)), | |
("placeholder", string(input.placeholder)), | |
("input_type", string(input.input_type)), | |
("location", input.location |> location), | |
]) | |
); | |
} | |
let request = (websocket_id, inputs) => | |
{ | |
Js.log(inputs); | |
let payload = Js.Dict.empty(); | |
Js.Dict.set(payload, "id", Js.Json.string(websocket_id)); | |
Js.Dict.set(payload, "locations", Json.Encode.list(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; | |
}; | |
let reducer = (action, state) => | |
switch(action) { | |
| INIT => let x = { | |
key: string_of_int(state.count), | |
id: "pickup_address" , | |
placeholder: "address " ++ string_of_int(state.count), | |
input_type: "pickup", | |
location: None | |
}; | |
let y = { | |
key: string_of_int(state.count), | |
id: "dropoff_address" , | |
placeholder: "address " ++ string_of_int(state.count), | |
input_type: "dropoff", | |
location: None | |
}; | |
let modified = [x,y]; | |
ReasonReact.Update({...state, inputs: modified}) | |
| ADDINPUT => | |
let inputs = | |
state.altinputs | |
@ [ | |
{ | |
key: string_of_int(state.count + 1), | |
id: string_of_int(state.count + 1) , | |
placeholder: "address " ++ string_of_int(state.count + 1), | |
input_type: "pickup", | |
location: None | |
}, | |
]; | |
ReasonReact.Update({...state, altinputs: inputs, count: state.count + 1}) | |
| DELETEINPUT(input) => | |
let new_list = Belt.List.keep(state.altinputs, candidate => candidate !== input); | |
ReasonReact.Update({...state, altinputs: new_list, count: state.count - 1}) | |
| MODIFYINPUTTYPE(x, input_type) => ReasonReact.Update({...state, count: state.count - 1}) | |
| MODIFYALTINPUTLOCATION(x, location) => | |
let id = int_of_string(x); | |
let alt = state.altinputs; | |
let el = List.nth(alt,id -1); | |
let newel = { | |
key: el.key, | |
id: el.id, | |
placeholder: el.placeholder, | |
input_type: el.input_type , | |
location: Some(location) | |
}; | |
let altins = List.mapi((index: int, input: input) => | |
if (index == id ) { | |
el | |
}else{ | |
input | |
} | |
, | |
alt, | |
); | |
ReasonReact.Update({...state, altinputs: altins}) | |
| MODIFYINPUTLOCATION(x, location) => | |
let inputs = state.inputs; | |
let el = switch(x){ | |
| "start_autocomplete" => List.hd(inputs) | |
| "end_autocomplete" => let l = List.rev(inputs); | |
List.hd(l) | |
} | |
let newel = { | |
key: el.key, | |
id: el.id, | |
placeholder: el.placeholder, | |
input_type: el.input_type , | |
location: Some(location) | |
}; | |
let altins = List.map((input: input) => | |
if (el.id == input.id ) { | |
newel | |
}else{ | |
input | |
} | |
, | |
inputs, | |
); | |
ReasonReact.Update({...state, inputs: altins}) | |
| BUILDINPUTS(x) => ReasonReact.Update({...state, inputs: [x]}) | |
| CLEARINPUTS => ReasonReact.Update({...state, inputs: []}) | |
| SENDREQUEST(h) => request(h, state.inputs); | |
ReasonReact.NoUpdate | |
}; | |
let component = ReasonReact.reducerComponent("Addresssearch"); | |
let make = (~websocket_id="", _children) => { | |
...component, | |
didMount: self => { | |
self.send(INIT); | |
}, | |
initialState: () => {websocket_id: websocket_id, count: 0, inputs: [], altinputs: []}, | |
reducer, | |
render: ({state, send}) => | |
<div className=Styles.search_address_form> | |
<Input | |
id="start_autocomplete" | |
placeholder="pickup address" | |
appSend = send | |
/> | |
( | |
ReasonReact.array( | |
Array.of_list( | |
List.map( | |
(input: input) => | |
<NewInput | |
input=input | |
appSend=send | |
/>, | |
state.altinputs, | |
), | |
), | |
) | |
) | |
<Input | |
id="end_autocomplete" | |
placeholder="dropoff address" | |
appSend = send | |
/> | |
<button className = Styles.address_add onClick=(_event => send(ADDINPUT))> ( ReasonReact.string("Add Another location") ) </button> | |
<button className = Styles.address_submit onClick=(_event => send(SENDREQUEST(state.websocket_id)))> ( ReasonReact.string("Submit") ) </button> | |
</div> | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment