Commit 325e4f4a authored by Stephane Glondu's avatar Stephane Glondu Committed by Stéphane Glondu

Use uuid instead of url as parameter to the booth

parent 2734d8d7
......@@ -315,28 +315,34 @@ let get_prefix str =
let n = String.length str in
if n >= 4 then String.sub str 0 (n-4) else str
let get_url x =
let get_uuid x =
let n = String.length x in
if n <= 1 || String.sub x 0 1 <> "#" then
None
else
let args = Url.decode_arguments (String.sub x 1 (n-1)) in
List.assoc_opt "url" args
List.assoc_opt "uuid" args
let load_url url =
let load_uuid uuid =
let open Lwt_xmlHttpRequest in
Lwt.async (fun () ->
let%lwt raw = get (url ^ "election.json") in
let () = set_textarea "election_params" raw.content in
let%lwt raw =
let%lwt x = Printf.ksprintf get "elections/%s/election.json" uuid in
if x.code = 404 then (
let%lwt x = Printf.ksprintf get "draft/preview/%s/election.json" uuid in
Lwt.return x.content
) else Lwt.return x.content
in
let () = set_textarea "election_params" raw in
Lwt.return (run_handler loadElection ())
)
let load_url_handler _ =
(match get_textarea_opt "url" with
| Some url ->
let encoded = Url.encode_arguments ["url", url] in
let load_uuid_handler _ =
(match get_textarea_opt "uuid" with
| Some uuid ->
let encoded = Url.encode_arguments ["uuid", uuid] in
Dom_html.window##.location##.hash := Js.string encoded;
load_url url
load_uuid uuid
| None -> ()
); Js._false
......@@ -351,19 +357,19 @@ let load_params_handler _ =
let onload_handler _ =
let () =
document##getElementById (Js.string "load_url") >>== fun e ->
e##.onclick := Dom_html.handler load_url_handler
document##getElementById (Js.string "load_uuid") >>== fun e ->
e##.onclick := Dom_html.handler load_uuid_handler
in
let () =
document##getElementById (Js.string "load_params") >>== fun e ->
e##.onclick := Dom_html.handler load_params_handler;
in
let () =
match get_url (Js.to_string Dom_html.window##.location##.hash) with
match get_uuid (Js.to_string Dom_html.window##.location##.hash) with
| None ->
set_element_display "wait_div" "none";
set_element_display "election_loader" "block";
| Some url -> load_url url
| Some uuid -> load_uuid uuid
in Js._false
let () = Dom_html.window##.onload := Dom_html.handler onload_handler
......@@ -362,13 +362,7 @@ let election_draft_pre () =
base ~title ~login_box ~content ()
let preview_booth uuid =
let url =
Eliom_uri.make_string_uri
~service:election_draft_preview ~absolute:true (uuid, ()) |>
rewrite_prefix |>
(fun x -> Filename.chop_suffix x "election.json")
in
let hash = Netencoding.Url.mk_url_encoded_parameters ["url", url] in
let hash = Netencoding.Url.mk_url_encoded_parameters ["uuid", raw_string_of_uuid uuid] in
let service =
Eliom_uri.make_string_uri
~service:election_vote ~absolute:true () |> rewrite_prefix
......@@ -1730,12 +1724,7 @@ let election_home election state () =
in
div ~a:[a_style "text-align:center;"] [
div [
let url =
Eliom_uri.make_string_uri
~service:election_home ~absolute:true (uuid, ()) |>
rewrite_prefix
in
let hash = Netencoding.Url.mk_url_encoded_parameters ["url", url] in
let hash = Netencoding.Url.mk_url_encoded_parameters ["uuid", raw_string_of_uuid uuid] in
make_button ~service:election_vote ~hash ~disabled L.start;
];
div [
......@@ -2869,11 +2858,11 @@ let booth () =
div ~a:[a_id "election_loader"; a_style "display:none;"] [
h1 [pcdata L.belenios_booth];
br ();
pcdata "Load an election by giving its URL:";
div [unsafe_textarea "url" ""];
div [button_no_value ~button_type:`Button ~a:[a_id "load_url"] [pcdata "Load URL"]];
pcdata "Load an election on this server by giving its UUID:";
div [unsafe_textarea "uuid" ""];
div [button_no_value ~button_type:`Button ~a:[a_id "load_uuid"] [pcdata "Load from UUID"]];
br ();
pcdata "Load an election by giving its parameters:";
pcdata "Load any election by giving its parameters:";
div [unsafe_textarea "election_params" ""];
div [button_no_value ~button_type:`Button ~a:[a_id "load_params"] [pcdata "Load parameters"]];
]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment