Skip to content

Commit

Permalink
feat(ui): allow name input on teacher token creation
Browse files Browse the repository at this point in the history
  • Loading branch information
AltGr committed May 4, 2023
1 parent 03669eb commit c341fca
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
10 changes: 9 additions & 1 deletion src/app/learnocaml_teacher_tab.ml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,15 @@ let tag_addremove list_id placeholder add_fun remove_fun =

let rec teacher_tab token _select _params () =
let action_new_token () =
retrieve (Learnocaml_api.Create_teacher_token token)
Learnocaml_common.ask_string
~title:"NEW TEACHER TOKEN"
[H.txt @@ "Enter a nickname for the new token:"]
>>= fun nickname ->
let nick = match String.trim nickname with
| "" -> None
| s -> Some s
in
retrieve (Learnocaml_api.Create_teacher_token (token, nick))
>|= fun new_token ->
alert ~title:[%i"TEACHER TOKEN"]
(Printf.sprintf [%if"New teacher token created:\n%s\n\n\
Expand Down
9 changes: 6 additions & 3 deletions src/server/learnocaml_server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,15 @@ module Request_handler = struct
(function
| Failure body -> (`Bad_request, body)
| exn -> (`Internal_server_error, Printexc.to_string exn))
| Api.Create_teacher_token token ->
| Api.Create_teacher_token (token, nick) ->
verify_teacher_token token
>?= fun () ->
Token.create_teacher ()
>>= respond_json cache

>>= fun tok ->
(match nick with | None -> Lwt.return_unit
| Some nickname ->
Save.set tok Save.{empty with nickname})
>>= fun () -> respond_json cache tok
| Api.Fetch_save token ->
lwt_catch_fail
(fun () ->
Expand Down
12 changes: 7 additions & 5 deletions src/state/learnocaml_api.ml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ type _ request =
| Create_token:
string * student token option * string option -> student token request
| Create_teacher_token:
teacher token -> teacher token request
teacher token * string option -> teacher token request
| Fetch_save:
'a token -> Save.t request
| Archive_zip:
Expand Down Expand Up @@ -299,9 +299,10 @@ module Conversions (Json: JSON_CODEC) = struct
| Create_token (secret_candiate, token, nick) ->
get ?token (["sync"; "new"; secret_candiate] @
(match nick with None -> [] | Some n -> [n]))
| Create_teacher_token token ->
| Create_teacher_token (token, nick) ->
assert (Token.is_teacher token);
get ~token ["teacher"; "new"]
get ~token (["teacher"; "new"] @
(match nick with None -> [] | Some n -> [n]))

| Fetch_save token ->
get ~token ["save.json"]
Expand Down Expand Up @@ -415,8 +416,9 @@ module Server (Json: JSON_CODEC) (Rh: REQUEST_HANDLER) = struct
| `GET, ["sync"; "new"; secret_candidate; nick], token ->
Create_token (secret_candidate, token, Some nick) |> k
| `GET, ["teacher"; "new"], Some token when Token.is_teacher token ->
Create_teacher_token token |> k

Create_teacher_token (token, None) |> k
| `GET, ["teacher"; "new"; nick], Some token when Token.is_teacher token ->
Create_teacher_token (token, Some nick) |> k
| `GET, ["save.json"], Some token ->
Fetch_save token |> k
| `GET, ["archive.zip"], Some token ->
Expand Down
2 changes: 1 addition & 1 deletion src/state/learnocaml_api.mli
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ type _ request =
| Create_token:
string * student token option * string option -> student token request
| Create_teacher_token:
teacher token -> teacher token request
teacher token * string option -> teacher token request
| Fetch_save:
'a token -> Save.t request
| Archive_zip:
Expand Down

0 comments on commit c341fca

Please sign in to comment.