autocomplete with team members only - for #9556

andresrcom-team-kick-member-autocomplete
Thibault Duplessis 2021-08-11 08:54:52 +02:00
parent 50690d95d9
commit ae270ce986
4 changed files with 23 additions and 10 deletions

View File

@ -546,9 +546,11 @@ final class User(
} }
case Some(term) => case Some(term) =>
{ {
(get("tour"), get("swiss")) match { (get("tour"), get("swiss"), get("team")) match {
case (Some(tourId), _) => env.tournament.playerRepo.searchPlayers(tourId, term, 10) case (Some(tourId), _, _) => env.tournament.playerRepo.searchPlayers(tourId, term, 10)
case (_, Some(swissId)) => env.swiss.api.searchPlayers(lila.swiss.Swiss.Id(swissId), term, 10) case (_, Some(swissId), _) =>
env.swiss.api.searchPlayers(lila.swiss.Swiss.Id(swissId), term, 10)
case (_, _, Some(teamId)) => env.team.api.searchMembers(teamId, term, 10)
case _ => case _ =>
ctx.me.ifTrue(getBool("friend")) match { ctx.me.ifTrue(getBool("friend")) match {
case Some(follower) => case Some(follower) =>

View File

@ -1,6 +1,7 @@
package views.html.team package views.html.team
import controllers.routes import controllers.routes
import play.api.data.Field
import play.api.data.Form import play.api.data.Form
import lila.api.Context import lila.api.Context
@ -26,9 +27,7 @@ object admin {
"Only invite leaders that you fully trust. Team leaders can kick members and other leaders out of the team." "Only invite leaders that you fully trust. Team leaders can kick members and other leaders out of the team."
), ),
postForm(cls := "leaders", action := routes.Team.leaders(t.id))( postForm(cls := "leaders", action := routes.Team.leaders(t.id))(
form3.group(form("leaders"), frag(usersWhoCanManageThisTeam()))( form3.group(form("leaders"), frag(usersWhoCanManageThisTeam()))(teamMembersAutoComplete(t)),
form3.textarea(_)(rows := 2)
),
form3.actions( form3.actions(
a(href := routes.Team.show(t.id))(trans.cancel()), a(href := routes.Team.show(t.id))(trans.cancel()),
form3.submit(trans.save()) form3.submit(trans.save())
@ -53,9 +52,7 @@ object admin {
div(cls := "page-menu__content box box-pad")( div(cls := "page-menu__content box box-pad")(
h1(title), h1(title),
postForm(action := routes.Team.kick(t.id))( postForm(action := routes.Team.kick(t.id))(
form3.group(form("members"), frag(whoToKick()))( form3.group(form("members"), frag(whoToKick()))(teamMembersAutoComplete(t)),
form3.textarea(_)(rows := 2)
),
form3.actions( form3.actions(
a(href := routes.Team.show(t.id))(trans.cancel()), a(href := routes.Team.show(t.id))(trans.cancel()),
form3.submit(trans.save()) form3.submit(trans.save())
@ -66,6 +63,9 @@ object admin {
} }
} }
private def teamMembersAutoComplete(team: lila.team.Team)(field: Field) =
form3.textarea(field)(rows := 2, dataRel := team.id)
def pmAll(t: lila.team.Team, form: Form[_], tours: List[lila.tournament.Tournament])(implicit def pmAll(t: lila.team.Team, form: Form[_], tours: List[lila.tournament.Tournament])(implicit
ctx: Context ctx: Context
) = { ) = {

View File

@ -225,6 +225,16 @@ final class TeamApi(
cached.invalidateTeamIds(userId) inject teamIds cached.invalidateTeamIds(userId) inject teamIds
} }
def searchMembers(teamId: Team.ID, term: String, nb: Int): Fu[List[User.ID]] =
User.validateId(term) ?? { valid =>
memberRepo.coll.primitive[User.ID](
selector = memberRepo.teamQuery(teamId) ++ $doc("user" $startsWith valid),
sort = $sort desc "user",
nb = nb,
field = "user"
)
}
def kick(team: Team, userId: User.ID, me: User): Funit = def kick(team: Team, userId: User.ID, me: User): Funit =
doQuit(team, userId) >> doQuit(team, userId) >>
(!team.leaders(me.id)).?? { (!team.leaders(me.id)).?? {

View File

@ -12,6 +12,7 @@ lichess.load.then(() => {
}); });
function initTagify(input: HTMLInputElement, maxTags: number) { function initTagify(input: HTMLInputElement, maxTags: number) {
const team = input.dataset['rel'];
const tagify = new Tagify(input, { const tagify = new Tagify(input, {
pattern: /.{3,}/, pattern: /.{3,}/,
maxTags, maxTags,
@ -19,7 +20,7 @@ function initTagify(input: HTMLInputElement, maxTags: number) {
whitelist: input.value.trim().split(/\s*,\s*/), whitelist: input.value.trim().split(/\s*,\s*/),
}); });
const doFetch: (term: string) => Promise<string[]> = debounce( const doFetch: (term: string) => Promise<string[]> = debounce(
(term: string) => xhr.json(xhr.url('/player/autocomplete', { term, names: 1 })), (term: string) => xhr.json(xhr.url('/player/autocomplete', { term, names: 1, team })),
300 300
); );
tagify.on('input', e => { tagify.on('input', e => {