edit tournament WIP

This commit is contained in:
Thibault Duplessis 2020-03-24 13:19:05 -06:00
parent 71d1f44eb1
commit 6926165052
2 changed files with 43 additions and 43 deletions

View file

@ -228,7 +228,7 @@ final class Tournament(
def form = Auth { implicit ctx => me => def form = Auth { implicit ctx => me =>
NoLameOrBot { NoLameOrBot {
teamC.teamsIBelongTo(me) map { teams => teamC.teamsIBelongTo(me) map { teams =>
Ok(html.tournament.form(forms(me), me, teams)) Ok(html.tournament.form.create(forms(me), me, teams))
} }
} }
} }
@ -237,7 +237,7 @@ final class Tournament(
NoLameOrBot { NoLameOrBot {
env.team.api.owns(teamId, me.id) map { env.team.api.owns(teamId, me.id) map {
_ ?? { _ ?? {
Ok(html.tournament.form(forms(me, teamId.some), me, Nil)) Ok(html.tournament.form.create(forms(me, teamId.some), me, Nil))
} }
} }
} }
@ -284,7 +284,7 @@ final class Tournament(
implicit val req = ctx.body implicit val req = ctx.body
negotiate( negotiate(
html = forms(me).bindFromRequest.fold( html = forms(me).bindFromRequest.fold(
err => BadRequest(html.tournament.form(err, me, teams)).fuccess, err => BadRequest(html.tournament.form.create(err, me, teams)).fuccess,
setup => setup =>
rateLimitCreation(me, setup.isPrivate, ctx.req) { rateLimitCreation(me, setup.isPrivate, ctx.req) {
api.createTournament(setup, me, teams, getUserTeamIds) map { tour => api.createTournament(setup, me, teams, getUserTeamIds) map { tour =>
@ -396,6 +396,18 @@ final class Tournament(
} }
} }
def edit(id: String) = Auth { implicit ctx => me =>
repo byId id flatMap {
_ ?? {
case tour if tour.createdBy == me.id && !tour.isFinished =>
teamC.teamsIBelongTo(me) map { teams =>
Ok(html.tournament.form.edit(tour, form)).fuccess
}
case tour => Redirect(routes.Tournament.show(tour.id)).fuccess
}
}
}
private val streamerCache = env.memo.cacheApi[Tour.ID, Set[UserModel.ID]](64, "tournament.streamers") { private val streamerCache = env.memo.cacheApi[Tour.ID, Set[UserModel.ID]](64, "tournament.streamers") {
_.refreshAfterWrite(15.seconds) _.refreshAfterWrite(15.seconds)
.maximumSize(64) .maximumSize(64)

View file

@ -2,18 +2,19 @@ package views.html
package tournament package tournament
import play.api.data.{ Field, Form } import play.api.data.{ Field, Form }
import play.api.i18n.Lang
import lila.api.Context import lila.api.Context
import lila.app.templating.Environment._ import lila.app.templating.Environment._
import lila.app.ui.ScalatagsTemplate._ import lila.app.ui.ScalatagsTemplate._
import lila.tournament.{ Condition, DataForm } import lila.tournament.{ Condition, DataForm, Tournament }
import lila.user.User import lila.user.User
import controllers.routes import controllers.routes
object form { object form {
def create(form: Form[_], me: User, teams: List[lila.hub.LightTeam])(implicit ctx: Context) = def create(form: Form[_], me: User, myTeams: List[lila.hub.LightTeam])(implicit ctx: Context) =
views.html.base.layout( views.html.base.layout(
title = trans.newTournament.txt(), title = trans.newTournament.txt(),
moreCss = cssTag("tournament.form"), moreCss = cssTag("tournament.form"),
@ -30,23 +31,7 @@ object form {
else trans.createANewTournament() else trans.createANewTournament()
), ),
postForm(cls := "form3", action := routes.Tournament.create)( postForm(cls := "form3", action := routes.Tournament.create)(
DataForm.canPickName(me) ?? { nameField(me, form, isTeamBattle),
form3.group(form("name"), trans.name()) { f =>
div(
form3.input(f),
" ",
if (isTeamBattle) "Team Battle" else "Arena",
br,
small(cls := "form-help")(
trans.safeTournamentName(),
br,
trans.inappropriateNameWarning(),
br,
trans.emptyTournamentName()
)
)
}
},
form3.split( form3.split(
form3.checkbox( form3.checkbox(
form("rated"), form("rated"),
@ -93,7 +78,7 @@ object form {
trans.password(), trans.password(),
help = trans.makePrivateTournament().some help = trans.makePrivateTournament().some
)(form3.input(_)), )(form3.input(_)),
condition(form, auto = true, teams = teams), condition(form, auto = true, teams = myTeams),
input(tpe := "hidden", name := form("berserkable").name, value := "false"), // hack allow disabling berserk input(tpe := "hidden", name := form("berserkable").name, value := "false"), // hack allow disabling berserk
form3.group( form3.group(
form("startDate"), form("startDate"),
@ -113,7 +98,9 @@ object form {
) )
} }
def edit(tour: Tournament, form: Form[_], me: User)(implicit ctx: Context) = { def edit(tour: Tournament, form: Form[_], me: User, myTeams: List[lila.hub.LightTeam])(
implicit ctx: Context
) =
views.html.base.layout( views.html.base.layout(
title = tour.name(), title = tour.name(),
moreCss = cssTag("tournament.form"), moreCss = cssTag("tournament.form"),
@ -122,11 +109,12 @@ object form {
jsTag("tournamentForm.js") jsTag("tournamentForm.js")
) )
) { ) {
val isTeamBattle = form("teamBattleByTeam").value.nonEmpty
main(cls := "page-small")( main(cls := "page-small")(
div(cls := "tour__form box box-pad")( div(cls := "tour__form box box-pad")(
h1(tour.name()), h1(tour.name()),
postForm(cls := "form3", action := routes.Tournament.update(tour.id))( postForm(cls := "form3", action := routes.Tournament.update(tour.id))(
nameField(form), nameField(me, form, isTeamBattle),
form3.split( form3.split(
form3.checkbox( form3.checkbox(
form("rated"), form("rated"),
@ -173,7 +161,7 @@ object form {
trans.password(), trans.password(),
help = trans.makePrivateTournament().some help = trans.makePrivateTournament().some
)(form3.input(_)), )(form3.input(_)),
condition(form, auto = true, teams = teams), condition(form, auto = true, teams = myTeams),
input(tpe := "hidden", name := form("berserkable").name, value := "false"), // hack allow disabling berserk input(tpe := "hidden", name := form("berserkable").name, value := "false"), // hack allow disabling berserk
form3.group( form3.group(
form("startDate"), form("startDate"),
@ -184,8 +172,8 @@ object form {
), ),
isTeamBattle option form3.hidden(form("teamBattleByTeam")), isTeamBattle option form3.hidden(form("teamBattleByTeam")),
form3.actions( form3.actions(
a(href := routes.Tournament.home())(trans.cancel()), a(href := routes.Tournament.show(tour.id))(trans.cancel()),
form3.submit(trans.createANewTournament(), icon = "g".some) form3.submit(trans.save(), icon = "g".some)
) )
) )
), ),
@ -193,24 +181,24 @@ object form {
) )
} }
private def nameField(me: User, form: Form[_]) = private def nameField(me: User, form: Form[_], isTeamBattle: Boolean)(implicit ctx: Context) =
DataForm.canPickName(me) ?? { DataForm.canPickName(me) ?? {
form3.group(form("name"), trans.name()) { f => form3.group(form("name"), trans.name()) { f =>
div( div(
form3.input(f), form3.input(f),
" ", " ",
if (isTeamBattle) "Team Battle" else "Arena", if (isTeamBattle) "Team Battle" else "Arena",
br,
small(cls := "form-help")(
trans.safeTournamentName(),
br, br,
small(cls := "form-help")( trans.inappropriateNameWarning(),
trans.safeTournamentName(), br,
br, trans.emptyTournamentName()
trans.inappropriateNameWarning(),
br,
trans.emptyTournamentName()
)
) )
} )
} }
}
private def autoField(auto: Boolean, field: Field)(visible: Field => Frag) = frag( private def autoField(auto: Boolean, field: Field)(visible: Field => Frag) = frag(
if (auto) form3.hidden(field) else visible(field) if (auto) form3.hidden(field) else visible(field)