Replace L10nInput with I18nInput in season
This commit is contained in:
parent
734178657d
commit
58c3b607a1
|
@ -117,3 +117,17 @@ func (tx *Tx) TranslateLegalText(ctx context.Context, companyID int, slug string
|
|||
_, err := tx.Exec(ctx, "select translate_legal_text($1, $2, $3, $4, $5)", companyID, slug, langTag, name, content)
|
||||
return err
|
||||
}
|
||||
|
||||
func (tx *Tx) AddSeason(ctx context.Context, companyID int, name string, color string) (string, error) {
|
||||
return tx.GetText(ctx, "select add_season($1, $2, $3)", companyID, name, color)
|
||||
}
|
||||
|
||||
func (tx *Tx) EditSeason(ctx context.Context, slug string, name string, color string, active bool) error {
|
||||
_, err := tx.Exec(ctx, "select edit_season($1, $2, $3, $4)", slug, name, color, active)
|
||||
return err
|
||||
}
|
||||
|
||||
func (tx *Tx) TranslateSeason(ctx context.Context, slug string, langTag language.Tag, name string) error {
|
||||
_, err := tx.Exec(ctx, "select translate_season($1, $2, $3)", slug, langTag, name)
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -56,7 +56,7 @@ func (h *AdminHandler) Handler(user *auth.User, company *auth.Company, conn *dat
|
|||
case "new":
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
f := newSeasonForm()
|
||||
f := newSeasonForm(company)
|
||||
f.MustRender(w, r, user, company)
|
||||
default:
|
||||
httplib.MethodNotAllowed(w, r, http.MethodGet)
|
||||
|
@ -82,7 +82,7 @@ func (h *AdminHandler) Handler(user *auth.User, company *auth.Company, conn *dat
|
|||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
f := newSeasonForm()
|
||||
f := newSeasonForm(company)
|
||||
if err := f.FillFromDatabase(r.Context(), conn, head); err != nil {
|
||||
if database.ErrorIsNotFound(err) {
|
||||
http.NotFound(w, r)
|
||||
|
@ -90,10 +90,9 @@ func (h *AdminHandler) Handler(user *auth.User, company *auth.Company, conn *dat
|
|||
}
|
||||
panic(err)
|
||||
}
|
||||
var langTag string
|
||||
langTag, r.URL.Path = httplib.ShiftPath(r.URL.Path)
|
||||
|
||||
switch langTag {
|
||||
head, r.URL.Path = httplib.ShiftPath(r.URL.Path)
|
||||
switch head {
|
||||
case "":
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
|
@ -104,23 +103,7 @@ func (h *AdminHandler) Handler(user *auth.User, company *auth.Company, conn *dat
|
|||
httplib.MethodNotAllowed(w, r, http.MethodGet, http.MethodPut)
|
||||
}
|
||||
default:
|
||||
loc, ok := h.locales.Get(langTag)
|
||||
if !ok {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
l10n := newSeasonL10nForm(f, loc)
|
||||
if err := l10n.FillFromDatabase(r.Context(), conn); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
l10n.MustRender(w, r, user, company)
|
||||
case http.MethodPut:
|
||||
editSeasonL10n(w, r, user, company, conn, l10n)
|
||||
default:
|
||||
httplib.MethodNotAllowed(w, r, http.MethodGet, http.MethodPut)
|
||||
}
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -170,19 +153,9 @@ func collectSeasonEntries(ctx context.Context, company *auth.Company, conn *data
|
|||
, season.name
|
||||
, to_color(color)::text
|
||||
, active
|
||||
, array_agg((lang_tag, endonym, not exists (select 1 from season_i18n as i18n where i18n.season_id = season.season_id and i18n.lang_tag = language.lang_tag)) order by endonym)
|
||||
from season
|
||||
join company using (company_id)
|
||||
, language
|
||||
where lang_tag <> default_lang_tag
|
||||
and language.selectable
|
||||
and season.company_id = $1
|
||||
group by season.slug
|
||||
, season.position
|
||||
, season.name
|
||||
, to_color(color)::text
|
||||
, active
|
||||
order by position, name`, pgx.QueryResultFormats{pgx.BinaryFormatCode}, company.ID)
|
||||
where season.company_id = $1
|
||||
order by position, name`, company.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -191,17 +164,9 @@ func collectSeasonEntries(ctx context.Context, company *auth.Company, conn *data
|
|||
var seasons []*seasonEntry
|
||||
for rows.Next() {
|
||||
entry := &seasonEntry{}
|
||||
var translations database.RecordArray
|
||||
if err = rows.Scan(&entry.Slug, &entry.URL, &entry.Name, &entry.Color, &entry.Active, &translations); err != nil {
|
||||
if err = rows.Scan(&entry.Slug, &entry.URL, &entry.Name, &entry.Color, &entry.Active); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, el := range translations.Elements {
|
||||
entry.Translations = append(entry.Translations, &locale.Translation{
|
||||
URL: entry.URL + "/" + el.Fields[0].Get().(string),
|
||||
Endonym: el.Fields[1].Get().(string),
|
||||
Missing: el.Fields[2].Get().(bool),
|
||||
})
|
||||
}
|
||||
seasons = append(seasons, entry)
|
||||
}
|
||||
|
||||
|
@ -209,13 +174,12 @@ func collectSeasonEntries(ctx context.Context, company *auth.Company, conn *data
|
|||
}
|
||||
|
||||
type seasonEntry struct {
|
||||
ID int
|
||||
Slug string
|
||||
URL string
|
||||
Name string
|
||||
Color string
|
||||
Active bool
|
||||
Translations []*locale.Translation
|
||||
ID int
|
||||
Slug string
|
||||
URL string
|
||||
Name string
|
||||
Color string
|
||||
Active bool
|
||||
}
|
||||
|
||||
var longMonthNames = []string{
|
||||
|
@ -327,7 +291,7 @@ func (page *seasonIndex) MustRender(w http.ResponseWriter, r *http.Request, user
|
|||
template.MustRenderAdminFiles(w, r, user, company, page, "season/index.gohtml", "season/calendar.gohtml")
|
||||
}
|
||||
|
||||
func processSeasonForm(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn, f *seasonForm, act func()) {
|
||||
func processSeasonForm(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn, f *seasonForm, act func(context.Context, *database.Tx) error) {
|
||||
if err := f.Parse(r); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
|
@ -345,39 +309,67 @@ func processSeasonForm(w http.ResponseWriter, r *http.Request, user *auth.User,
|
|||
f.MustRender(w, r, user, company)
|
||||
return
|
||||
}
|
||||
act()
|
||||
tx := conn.MustBegin(r.Context())
|
||||
if err := act(r.Context(), tx); err == nil {
|
||||
tx.MustCommit(r.Context())
|
||||
} else {
|
||||
if rErr := tx.Rollback(r.Context()); rErr != nil {
|
||||
err = rErr
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
httplib.Redirect(w, r, "/admin/seasons", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func addSeason(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn) {
|
||||
f := newSeasonForm()
|
||||
processSeasonForm(w, r, user, company, conn, f, func() {
|
||||
conn.MustExec(r.Context(), "select add_season($1, $2, $3)", company.ID, f.Name, f.Color)
|
||||
f := newSeasonForm(company)
|
||||
processSeasonForm(w, r, user, company, conn, f, func(ctx context.Context, tx *database.Tx) error {
|
||||
var err error
|
||||
if f.Slug, err = tx.AddSeason(ctx, company.ID, f.Name[f.DefaultLang].Val, f.Color.Val); err != nil {
|
||||
return err
|
||||
}
|
||||
return translateSeason(ctx, tx, company, f)
|
||||
})
|
||||
}
|
||||
|
||||
func translateSeason(ctx context.Context, tx *database.Tx, company *auth.Company, f *seasonForm) error {
|
||||
for lang := range company.Locales {
|
||||
l := lang.String()
|
||||
if l == f.DefaultLang {
|
||||
continue
|
||||
}
|
||||
if err := tx.TranslateSeason(ctx, f.Slug, lang, f.Name[l].Val); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func editSeason(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn, f *seasonForm) {
|
||||
processSeasonForm(w, r, user, company, conn, f, func() {
|
||||
conn.MustExec(r.Context(), "select edit_season($1, $2, $3, $4)", f.Slug, f.Name, f.Color, f.Active)
|
||||
processSeasonForm(w, r, user, company, conn, f, func(ctx context.Context, tx *database.Tx) error {
|
||||
if err := tx.EditSeason(ctx, f.Slug, f.Name[f.DefaultLang].Val, f.Color.Val, f.Active.Checked); err != nil {
|
||||
return err
|
||||
}
|
||||
return translateSeason(ctx, tx, company, f)
|
||||
})
|
||||
}
|
||||
|
||||
type seasonForm struct {
|
||||
Slug string
|
||||
Active *form.Checkbox
|
||||
Name *form.Input
|
||||
Color *form.Input
|
||||
DefaultLang string
|
||||
Slug string
|
||||
Active *form.Checkbox
|
||||
Name form.I18nInput
|
||||
Color *form.Input
|
||||
}
|
||||
|
||||
func newSeasonForm() *seasonForm {
|
||||
func newSeasonForm(company *auth.Company) *seasonForm {
|
||||
return &seasonForm{
|
||||
DefaultLang: company.DefaultLanguage.String(),
|
||||
Active: &form.Checkbox{
|
||||
Name: "active",
|
||||
Checked: true,
|
||||
},
|
||||
Name: &form.Input{
|
||||
Name: "label",
|
||||
},
|
||||
Name: form.NewI18nInput(company.Locales, "label"),
|
||||
Color: &form.Input{
|
||||
Name: "season",
|
||||
},
|
||||
|
@ -386,8 +378,26 @@ func newSeasonForm() *seasonForm {
|
|||
|
||||
func (f *seasonForm) FillFromDatabase(ctx context.Context, conn *database.Conn, slug string) error {
|
||||
f.Slug = slug
|
||||
row := conn.QueryRow(ctx, "select name, to_color(color)::text, active from season where slug = $1", slug)
|
||||
return row.Scan(&f.Name.Val, &f.Color.Val, &f.Active.Checked)
|
||||
var name database.RecordArray
|
||||
row := conn.QueryRow(ctx, `
|
||||
select season.name
|
||||
, to_color(color)::text
|
||||
, active
|
||||
, array_agg((lang_tag, i18n.name))
|
||||
from season
|
||||
left join season_i18n as i18n using (season_id)
|
||||
where slug = $1
|
||||
group by season.name
|
||||
, to_color(color)::text
|
||||
, active
|
||||
`, pgx.QueryResultFormats{pgx.BinaryFormatCode}, slug)
|
||||
if err := row.Scan(&f.Name[f.DefaultLang].Val, &f.Color.Val, &f.Active.Checked, &name); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := f.Name.FillArray(name); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *seasonForm) Parse(r *http.Request) error {
|
||||
|
@ -402,7 +412,7 @@ func (f *seasonForm) Parse(r *http.Request) error {
|
|||
|
||||
func (f *seasonForm) Valid(ctx context.Context, conn *database.Conn, l *locale.Locale) (bool, error) {
|
||||
v := form.NewValidator(l)
|
||||
v.CheckRequired(f.Name, l.GettextNoop("Name can not be empty."))
|
||||
v.CheckRequired(f.Name[f.DefaultLang], l.GettextNoop("Name can not be empty."))
|
||||
if v.CheckRequired(f.Color, l.GettextNoop("Color can not be empty.")) {
|
||||
if _, err := v.CheckValidColor(ctx, conn, f.Color, l.Gettext("This color is not valid. It must be like #123abc.")); err != nil {
|
||||
return false, err
|
||||
|
|
|
@ -1,71 +0,0 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2023 jordi fita mas <jfita@peritasoft.com>
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package season
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"dev.tandem.ws/tandem/camper/pkg/auth"
|
||||
"dev.tandem.ws/tandem/camper/pkg/database"
|
||||
"dev.tandem.ws/tandem/camper/pkg/form"
|
||||
httplib "dev.tandem.ws/tandem/camper/pkg/http"
|
||||
"dev.tandem.ws/tandem/camper/pkg/locale"
|
||||
"dev.tandem.ws/tandem/camper/pkg/template"
|
||||
)
|
||||
|
||||
type seasonL10nForm struct {
|
||||
Locale *locale.Locale
|
||||
Slug string
|
||||
Name *form.L10nInput
|
||||
}
|
||||
|
||||
func newSeasonL10nForm(f *seasonForm, loc *locale.Locale) *seasonL10nForm {
|
||||
return &seasonL10nForm{
|
||||
Locale: loc,
|
||||
Slug: f.Slug,
|
||||
Name: f.Name.L10nInput(),
|
||||
}
|
||||
}
|
||||
|
||||
func (l10n *seasonL10nForm) FillFromDatabase(ctx context.Context, conn *database.Conn) error {
|
||||
row := conn.QueryRow(ctx, `
|
||||
select coalesce(i18n.name, '') as l10n_name
|
||||
from season
|
||||
left join season_i18n as i18n on season.season_id = i18n.season_id and i18n.lang_tag = $1
|
||||
where slug = $2
|
||||
`, l10n.Locale.Language, l10n.Slug)
|
||||
return row.Scan(&l10n.Name.Val)
|
||||
}
|
||||
|
||||
func (l10n *seasonL10nForm) MustRender(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company) {
|
||||
template.MustRenderAdmin(w, r, user, company, "season/l10n.gohtml", l10n)
|
||||
}
|
||||
|
||||
func editSeasonL10n(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn, l10n *seasonL10nForm) {
|
||||
if ok, err := form.Handle(l10n, w, r, user); err != nil {
|
||||
return
|
||||
} else if !ok {
|
||||
l10n.MustRender(w, r, user, company)
|
||||
return
|
||||
}
|
||||
conn.MustExec(r.Context(), "select translate_season($1, $2, $3)", l10n.Slug, l10n.Locale.Language, l10n.Name)
|
||||
httplib.Redirect(w, r, "/admin/seasons", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func (l10n *seasonL10nForm) Parse(r *http.Request) error {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
return err
|
||||
}
|
||||
l10n.Name.FillValue(r)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l10n *seasonL10nForm) Valid(l *locale.Locale) bool {
|
||||
v := form.NewValidator(l)
|
||||
v.CheckRequired(&l10n.Name.Input, l.GettextNoop("Name can not be empty."))
|
||||
return v.AllOK
|
||||
}
|
|
@ -28,7 +28,7 @@
|
|||
{{ end }}
|
||||
</h2>
|
||||
{{ CSRFInput }}
|
||||
<fieldset>
|
||||
<fieldset {{template "init-lang" . }}>
|
||||
{{ if .Slug }}
|
||||
{{ with .Active -}}
|
||||
<label>
|
||||
|
@ -42,12 +42,17 @@
|
|||
<input type="hidden" name="{{ .Active.Name }}" value="true">
|
||||
{{ end }}
|
||||
{{ with .Name -}}
|
||||
<label>
|
||||
{{( pgettext "Name" "input")}}<br>
|
||||
<input type="text" name="{{ .Name }}" value="{{ .Val }}"
|
||||
required {{ template "error-attrs" . }}><br>
|
||||
</label>
|
||||
{{ template "error-message" . }}
|
||||
<fieldset>
|
||||
<legend>{{( pgettext "Name" "input")}}</legend>
|
||||
{{ template "lang-selector" . }}
|
||||
{{ range $lang, $input := . -}}
|
||||
<label x-cloak x-show="lang === '{{ $lang }}'"><span>{{ $lang }}</span><br>
|
||||
<input type="text" name="{{ $input.Name }}" value="{{ $input.Val }}"
|
||||
{{ template "error-attrs" $input }}><br>
|
||||
</label>
|
||||
{{- end }}
|
||||
{{ template "error-message" . }}
|
||||
</fieldset>
|
||||
{{- end }}
|
||||
{{ with .Color -}}
|
||||
<label>
|
||||
|
|
|
@ -24,7 +24,6 @@
|
|||
<tr>
|
||||
<th scope="col">{{( pgettext "Color" "header" )}}</th>
|
||||
<th scope="col">{{( pgettext "Name" "header" )}}</th>
|
||||
<th scope="col">{{( pgettext "Translations" "header" )}}</th>
|
||||
<th scope="col">{{( pgettext "Active" "season" )}}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
|
@ -39,13 +38,6 @@
|
|||
</svg>
|
||||
</td>
|
||||
<td><a href="{{ .URL }}">{{ .Name }}</a></td>
|
||||
<td>
|
||||
{{ range .Translations }}
|
||||
<a
|
||||
{{ if .Missing }}class="missing-translation"{{ end }}
|
||||
href="{{ .URL }}">{{ .Endonym }}</a>
|
||||
{{ end }}
|
||||
</td>
|
||||
<td>{{ if .Active }}{{( gettext "Yes" )}}{{ else }}{{( gettext "No" )}}{{ end }}</td>
|
||||
</tr>
|
||||
{{- end }}
|
||||
|
|
|
@ -1,35 +0,0 @@
|
|||
<!--
|
||||
SPDX-FileCopyrightText: 2023 jordi fita mas <jordi@tandem.blog>
|
||||
SPDX-License-Identifier: AGPL-3.0-only
|
||||
-->
|
||||
{{ define "title" -}}
|
||||
{{- /*gotype: dev.tandem.ws/tandem/camper/pkg/season.seasonL10nForm*/ -}}
|
||||
{{printf (pgettext "Translate Season to %s" "title") .Locale.Endonym }}
|
||||
{{- end }}
|
||||
|
||||
{{ define "content" -}}
|
||||
{{- /*gotype: dev.tandem.ws/tandem/camper/pkg/season.seasonL10nForm*/ -}}
|
||||
<form data-hx-put="/admin/seasons/{{ .Slug }}/{{ .Locale.Language }}">
|
||||
<h2>
|
||||
{{printf (pgettext "Translate Season to %s" "title") .Locale.Endonym }}
|
||||
</h2>
|
||||
{{ CSRFInput }}
|
||||
<fieldset>
|
||||
{{ with .Name -}}
|
||||
<fieldset>
|
||||
<legend>{{( pgettext "Name" "input")}}</legend>
|
||||
{{( gettext "Source:" )}} {{ .Source }}<br>
|
||||
<label>
|
||||
{{( pgettext "Translation:" "input" )}}
|
||||
<input type="text" name="{{ .Name }}" value="{{ .Val }}"
|
||||
required {{ template "error-attrs" . }}><br>
|
||||
</label>
|
||||
{{ template "error-message" . }}
|
||||
</fieldset>
|
||||
{{- end }}
|
||||
</fieldset>
|
||||
<footer>
|
||||
<button type="submit">{{( pgettext "Translate" "action" )}}</button>
|
||||
</footer>
|
||||
</form>
|
||||
{{- end }}
|
Loading…
Reference in New Issue