From 5af51c672f6cb763675595370f0f8cceaacd0881 Mon Sep 17 00:00:00 2001 From: Thibault Duplessis Date: Sat, 29 Apr 2017 23:42:28 +0200 Subject: [PATCH] realtime check of username availability - closes #2987 --- app/controllers/User.scala | 9 +++++---- app/views/auth/formFields.scala.html | 1 + app/views/auth/signup.scala.html | 1 + public/javascripts/signup.js | 22 ++++++++++++++++++++++ 4 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 public/javascripts/signup.js diff --git a/app/controllers/User.scala b/app/controllers/User.scala index 047f6bbc63..0cd07b7e2c 100644 --- a/app/controllers/User.scala +++ b/app/controllers/User.scala @@ -316,16 +316,17 @@ object User extends LilaController { def autocomplete = Open { implicit ctx => get("term", ctx.req).filter(_.nonEmpty) match { case None => BadRequest("No search term provided").fuccess - case Some(term) => JsonOk { + case Some(term) => { ctx.me.ifTrue(getBool("friend")) match { - case None => UserRepo usernamesLike term + case None if getBool("exists") => UserRepo nameExists term map { JsBoolean(_) } + case None => UserRepo usernamesLike term map { Json.toJson(_) } case Some(follower) => Env.relation.api.searchFollowedBy(follower, term, 10) flatMap { case Nil => UserRepo usernamesLike term case userIds => UserRepo usernamesByIds userIds - } + } map { Json.toJson(_) } } - } + } map { Ok(_) as JSON } } } diff --git a/app/views/auth/formFields.scala.html b/app/views/auth/formFields.scala.html index cec28b0c69..7cafec4193 100644 --- a/app/views/auth/formFields.scala.html +++ b/app/views/auth/formFields.scala.html @@ -10,6 +10,7 @@ value="@username.value" autofocus="true" /> @errMsg(username) +

This username is already in use, please try another one.

  • diff --git a/app/views/auth/signup.scala.html b/app/views/auth/signup.scala.html index d3dc3f678b..dd2cf5313e 100644 --- a/app/views/auth/signup.scala.html +++ b/app/views/auth/signup.scala.html @@ -1,6 +1,7 @@ @(form: Form[_], recaptchaPublicKey: String)(implicit ctx: Context) @moreJs = { +@jsTag("signup.js") } diff --git a/public/javascripts/signup.js b/public/javascripts/signup.js new file mode 100644 index 0000000000..a16ffc8703 --- /dev/null +++ b/public/javascripts/signup.js @@ -0,0 +1,22 @@ +$(function() { + var $exists = $('.signup_box form .username .exists'); + var runCheck = lichess.fp.debounce(function() { + var name = $username.val(); + if (name.length >= 3) $.ajax({ + method: 'GET', + url: '/player/autocomplete', + data: { + term: name, + exists: 1 + }, + success: function(res) { + $exists.toggle(res); + } + }); + }, 300); + $username = $('.signup_box form input#username') + .on('change keyup paste', function() { + $exists.hide(); + runCheck(); + }); +});