add click event on map
This commit is contained in:
parent
1cbc32b9b6
commit
d0fc9c064d
4 changed files with 72 additions and 38 deletions
|
|
@ -1,7 +1,10 @@
|
||||||
let f nick request =
|
let f nick request =
|
||||||
<%s Format.sprintf "Add a plant to your Collection %s !" nick %>
|
<%s Format.sprintf "Add a plant to your Collection %s !" nick %>
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
|
<div id="map"></div>
|
||||||
<%s! Dream.form_tag ~action:"/add_plant" ~enctype:`Multipart_form_data request %>
|
<%s! Dream.form_tag ~action:"/add_plant" ~enctype:`Multipart_form_data request %>
|
||||||
|
<input type="hidden" id="lat_lng" name="lat_lng" value="THIS IS NOT WORKING">
|
||||||
|
|
||||||
<label for="tags" class="form-label">Tags</label>
|
<label for="tags" class="form-label">Tags</label>
|
||||||
<textarea name="tags" type="text" class="form-control" id="tags" aria-describedby="tagsHelp"></textarea>
|
<textarea name="tags" type="text" class="form-control" id="tags" aria-describedby="tagsHelp"></textarea>
|
||||||
<div id="tagsHelp" class="form-text">Describe your plant with tags</div>
|
<div id="tagsHelp" class="form-text">Describe your plant with tags</div>
|
||||||
|
|
|
||||||
31
src/map.ml
31
src/map.ml
|
|
@ -1,3 +1,4 @@
|
||||||
|
(* TODO only run this on /add_plant and /map *)
|
||||||
let log = Format.printf
|
let log = Format.printf
|
||||||
|
|
||||||
(* get the leaflet object *)
|
(* get the leaflet object *)
|
||||||
|
|
@ -6,6 +7,9 @@ let leaflet =
|
||||||
| Some l -> l
|
| Some l -> l
|
||||||
| None -> failwith "can't load leaflet"
|
| None -> failwith "can't load leaflet"
|
||||||
|
|
||||||
|
(* get popup object *)
|
||||||
|
let popup = Jv.call leaflet "popup" [||]
|
||||||
|
|
||||||
(* create a map *)
|
(* create a map *)
|
||||||
let map =
|
let map =
|
||||||
log "creating map@.";
|
log "creating map@.";
|
||||||
|
|
@ -43,12 +47,23 @@ let () =
|
||||||
let _map : Jv.t = Jv.call tile_layer "addTo" [| map |] in
|
let _map : Jv.t = Jv.call tile_layer "addTo" [| map |] in
|
||||||
()
|
()
|
||||||
|
|
||||||
(* TODO :-)
|
let on_click e =
|
||||||
(* binding callbacks@ *)
|
log "on_click@.";
|
||||||
let () =
|
|
||||||
Format.eprintf "binding callbacks@.";
|
|
||||||
|
|
||||||
Ev.listen Leaflet.Map.click (fun e ->
|
let lat_lng = Jv.get e "latlng" in
|
||||||
Console.log [ e |> Ev.as_type |> Leaflet.Ev.MouseEvent.latlng ] )
|
ignore @@ Jv.call popup "setLatLng" [| lat_lng |];
|
||||||
@@ Leaflet.Map.as_target map
|
ignore @@ Jv.call popup "setContent" [| Jv.of_string "YOU CLICKED HERE" |];
|
||||||
*)
|
ignore @@ Jv.call popup "openOn" [| map |];
|
||||||
|
|
||||||
|
(*TODO use Brr to insert lat_lng in the form *)
|
||||||
|
let open Brr in
|
||||||
|
let lat_lng_input = El.input ~at:At.[ id (Jstr.v "lat_lng") ] () in
|
||||||
|
ignore
|
||||||
|
@@ El.set_at (Jstr.of_string "value")
|
||||||
|
(Some (Jstr.of_string "FUCK"))
|
||||||
|
lat_lng_input;
|
||||||
|
()
|
||||||
|
|
||||||
|
let () =
|
||||||
|
(*add on_click callback to map*)
|
||||||
|
ignore @@ Jv.call map "on" [| Jv.of_string "click"; Jv.repr on_click |]
|
||||||
|
|
|
||||||
|
|
@ -154,17 +154,22 @@ let add_plant_post request =
|
||||||
| None -> render_unsafe "Not logged in" request
|
| None -> render_unsafe "Not logged in" request
|
||||||
| Some nick -> (
|
| Some nick -> (
|
||||||
match%lwt Dream.multipart request with
|
match%lwt Dream.multipart request with
|
||||||
| `Ok [ ("files", files); ("tags", tags) ]
|
| `Ok [ ("files", files); ("lat_lng", lat_lng); ("tags", tags) ]
|
||||||
| `Ok (("files", files) :: ("tags", tags) :: _ :: _) -> (
|
| `Ok (("files", files) :: ("lat_lng", lat_lng) :: ("tags", tags) :: _ :: _)
|
||||||
|
-> (
|
||||||
match tags with
|
match tags with
|
||||||
| [] -> render_unsafe "Field tag is empty" request
|
| [] -> render_unsafe "Field tag is empty" request
|
||||||
| [ (_, tags) ] ->
|
| [ (_, tags) ] -> (
|
||||||
let res =
|
match lat_lng with
|
||||||
match User.add_plant tags files nick with
|
| [] -> render_unsafe "Field tag is empty" request
|
||||||
| Ok () -> "Your plant was uploaded!"
|
| [ (_, lat_lng) ] ->
|
||||||
| Error e -> e
|
let res =
|
||||||
in
|
match User.add_plant lat_lng tags files nick with
|
||||||
render_unsafe res request
|
| Ok () -> "Your plant was uploaded!"
|
||||||
|
| Error e -> e
|
||||||
|
in
|
||||||
|
render_unsafe res request
|
||||||
|
| _lat_lng -> Dream.empty `Bad_Request )
|
||||||
| _tags -> Dream.empty `Bad_Request )
|
| _tags -> Dream.empty `Bad_Request )
|
||||||
| `Ok _ -> Dream.empty `Bad_Request
|
| `Ok _ -> Dream.empty `Bad_Request
|
||||||
| `Expired _
|
| `Expired _
|
||||||
|
|
|
||||||
53
src/user.ml
53
src/user.ml
|
|
@ -111,6 +111,10 @@ module Q = struct
|
||||||
let get_tags =
|
let get_tags =
|
||||||
Caqti_request.collect Caqti_type.string Caqti_type.string
|
Caqti_request.collect Caqti_type.string Caqti_type.string
|
||||||
"SELECT tag FROM plant_tag WHERE plant_id=?;"
|
"SELECT tag FROM plant_tag WHERE plant_id=?;"
|
||||||
|
|
||||||
|
let get_plant_gps =
|
||||||
|
Caqti_request.find_opt Caqti_type.string Caqti_type.string
|
||||||
|
"SELECT gps FROM plant_gps WHERE plant_id=?;"
|
||||||
end
|
end
|
||||||
|
|
||||||
module Db =
|
module Db =
|
||||||
|
|
@ -201,6 +205,12 @@ let view_plant plant_id =
|
||||||
| Ok count -> (
|
| Ok count -> (
|
||||||
match count with
|
match count with
|
||||||
| Some count -> (
|
| Some count -> (
|
||||||
|
let gps =
|
||||||
|
match Db.find_opt Q.get_plant_gps plant_id with
|
||||||
|
| Ok (Some gps) -> gps
|
||||||
|
| Ok None -> ""
|
||||||
|
| Error e -> Format.sprintf "db error: %s" (Caqti_error.show e)
|
||||||
|
in
|
||||||
let images =
|
let images =
|
||||||
String.concat "\n"
|
String.concat "\n"
|
||||||
(List.map
|
(List.map
|
||||||
|
|
@ -215,7 +225,7 @@ let view_plant plant_id =
|
||||||
| Ok tags ->
|
| Ok tags ->
|
||||||
let tags = String.concat " " tags in
|
let tags = String.concat " " tags in
|
||||||
(* TODO add link to gps/map too *)
|
(* TODO add link to gps/map too *)
|
||||||
images ^ tags )
|
images ^ tags ^ gps )
|
||||||
| None -> "db error" )
|
| None -> "db error" )
|
||||||
| Error e -> Format.sprintf "db error: %s" (Caqti_error.show e)
|
| Error e -> Format.sprintf "db error: %s" (Caqti_error.show e)
|
||||||
|
|
||||||
|
|
@ -323,27 +333,28 @@ let upload_avatar files nick =
|
||||||
(* TODO do the same for text input: check length, forbidden chars and have a forbidden words filter*)
|
(* TODO do the same for text input: check length, forbidden chars and have a forbidden words filter*)
|
||||||
let is_valid_image _content = true
|
let is_valid_image _content = true
|
||||||
|
|
||||||
let add_plant tags files nick =
|
let add_plant gps tags files nick =
|
||||||
match files with
|
let tags_len = String.length tags in
|
||||||
| files -> (
|
if tags_len > 1000 then
|
||||||
let tags_len = String.length tags in
|
Error "tags too long"
|
||||||
if tags_len > 1000 then
|
else
|
||||||
Error "tags too long"
|
let tag_list = Str.split (Str.regexp " +") tags in
|
||||||
|
(* id for plant*)
|
||||||
|
let ok_list = List.map (fun (_, content) -> is_valid_image content) files in
|
||||||
|
let valid_files = List.for_all (fun valid -> valid) ok_list in
|
||||||
|
if not valid_files then
|
||||||
|
Error "Invalid image"
|
||||||
else
|
else
|
||||||
let tag_list = Str.split (Str.regexp " +") tags in
|
(* add plant to db *)
|
||||||
(* id for plant*)
|
let plant_id = Uuidm.to_string (Uuidm.v4_gen random_state ()) in
|
||||||
let ok_list =
|
(* add to plant_id <-> user*)
|
||||||
List.map (fun (_, content) -> is_valid_image content) files
|
let res_plant = Db.exec Q.upload_plant_id (plant_id, nick) in
|
||||||
in
|
match res_plant with
|
||||||
let valid_files = List.for_all (fun valid -> valid) ok_list in
|
| Error e -> Error (Format.sprintf "db error: %s" (Caqti_error.show e))
|
||||||
if not valid_files then
|
| Ok _ -> (
|
||||||
Error "Invalid image"
|
(* add to plant_id <-> gps table*)
|
||||||
else
|
let res_gps = Db.exec Q.upload_plant_gps (plant_id, gps) in
|
||||||
(* add plant to db *)
|
match res_gps with
|
||||||
let plant_id = Uuidm.to_string (Uuidm.v4_gen random_state ()) in
|
|
||||||
(* add to plant_id <-> user*)
|
|
||||||
let res_plant = Db.exec Q.upload_plant_id (plant_id, nick) in
|
|
||||||
match res_plant with
|
|
||||||
| Error e -> Error (Format.sprintf "db error: %s" (Caqti_error.show e))
|
| Error e -> Error (Format.sprintf "db error: %s" (Caqti_error.show e))
|
||||||
| Ok _ -> (
|
| Ok _ -> (
|
||||||
(* add to plant_id <-> tag table*)
|
(* add to plant_id <-> tag table*)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue