Replace L10nInput with I18nInput in season

This commit is contained in:
jordi fita mas 2024-01-12 20:26:45 +01:00
parent 734178657d
commit 58c3b607a1
6 changed files with 104 additions and 189 deletions

View File

@ -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) _, err := tx.Exec(ctx, "select translate_legal_text($1, $2, $3, $4, $5)", companyID, slug, langTag, name, content)
return err 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
}

View File

@ -56,7 +56,7 @@ func (h *AdminHandler) Handler(user *auth.User, company *auth.Company, conn *dat
case "new": case "new":
switch r.Method { switch r.Method {
case http.MethodGet: case http.MethodGet:
f := newSeasonForm() f := newSeasonForm(company)
f.MustRender(w, r, user, company) f.MustRender(w, r, user, company)
default: default:
httplib.MethodNotAllowed(w, r, http.MethodGet) 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) http.NotFound(w, r)
return return
} }
f := newSeasonForm() f := newSeasonForm(company)
if err := f.FillFromDatabase(r.Context(), conn, head); err != nil { if err := f.FillFromDatabase(r.Context(), conn, head); err != nil {
if database.ErrorIsNotFound(err) { if database.ErrorIsNotFound(err) {
http.NotFound(w, r) http.NotFound(w, r)
@ -90,10 +90,9 @@ func (h *AdminHandler) Handler(user *auth.User, company *auth.Company, conn *dat
} }
panic(err) 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 "": case "":
switch r.Method { switch r.Method {
case http.MethodGet: 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) httplib.MethodNotAllowed(w, r, http.MethodGet, http.MethodPut)
} }
default: default:
loc, ok := h.locales.Get(langTag) http.NotFound(w, r)
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)
}
} }
} }
} }
@ -170,19 +153,9 @@ func collectSeasonEntries(ctx context.Context, company *auth.Company, conn *data
, season.name , season.name
, to_color(color)::text , to_color(color)::text
, active , 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 from season
join company using (company_id) where season.company_id = $1
, language order by position, name`, company.ID)
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)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -191,17 +164,9 @@ func collectSeasonEntries(ctx context.Context, company *auth.Company, conn *data
var seasons []*seasonEntry var seasons []*seasonEntry
for rows.Next() { for rows.Next() {
entry := &seasonEntry{} entry := &seasonEntry{}
var translations database.RecordArray if err = rows.Scan(&entry.Slug, &entry.URL, &entry.Name, &entry.Color, &entry.Active); err != nil {
if err = rows.Scan(&entry.Slug, &entry.URL, &entry.Name, &entry.Color, &entry.Active, &translations); err != nil {
return nil, err 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) seasons = append(seasons, entry)
} }
@ -209,13 +174,12 @@ func collectSeasonEntries(ctx context.Context, company *auth.Company, conn *data
} }
type seasonEntry struct { type seasonEntry struct {
ID int ID int
Slug string Slug string
URL string URL string
Name string Name string
Color string Color string
Active bool Active bool
Translations []*locale.Translation
} }
var longMonthNames = []string{ 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") 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 { if err := f.Parse(r); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
return return
@ -345,39 +309,67 @@ func processSeasonForm(w http.ResponseWriter, r *http.Request, user *auth.User,
f.MustRender(w, r, user, company) f.MustRender(w, r, user, company)
return 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) 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) { func addSeason(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn) {
f := newSeasonForm() f := newSeasonForm(company)
processSeasonForm(w, r, user, company, conn, f, func() { processSeasonForm(w, r, user, company, conn, f, func(ctx context.Context, tx *database.Tx) error {
conn.MustExec(r.Context(), "select add_season($1, $2, $3)", company.ID, f.Name, f.Color) 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) { 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() { processSeasonForm(w, r, user, company, conn, f, func(ctx context.Context, tx *database.Tx) error {
conn.MustExec(r.Context(), "select edit_season($1, $2, $3, $4)", f.Slug, f.Name, f.Color, f.Active) 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 { type seasonForm struct {
Slug string DefaultLang string
Active *form.Checkbox Slug string
Name *form.Input Active *form.Checkbox
Color *form.Input Name form.I18nInput
Color *form.Input
} }
func newSeasonForm() *seasonForm { func newSeasonForm(company *auth.Company) *seasonForm {
return &seasonForm{ return &seasonForm{
DefaultLang: company.DefaultLanguage.String(),
Active: &form.Checkbox{ Active: &form.Checkbox{
Name: "active", Name: "active",
Checked: true, Checked: true,
}, },
Name: &form.Input{ Name: form.NewI18nInput(company.Locales, "label"),
Name: "label",
},
Color: &form.Input{ Color: &form.Input{
Name: "season", Name: "season",
}, },
@ -386,8 +378,26 @@ func newSeasonForm() *seasonForm {
func (f *seasonForm) FillFromDatabase(ctx context.Context, conn *database.Conn, slug string) error { func (f *seasonForm) FillFromDatabase(ctx context.Context, conn *database.Conn, slug string) error {
f.Slug = slug f.Slug = slug
row := conn.QueryRow(ctx, "select name, to_color(color)::text, active from season where slug = $1", slug) var name database.RecordArray
return row.Scan(&f.Name.Val, &f.Color.Val, &f.Active.Checked) 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 { 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) { func (f *seasonForm) Valid(ctx context.Context, conn *database.Conn, l *locale.Locale) (bool, error) {
v := form.NewValidator(l) 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 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 { 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 return false, err

View File

@ -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
}

View File

@ -28,7 +28,7 @@
{{ end }} {{ end }}
</h2> </h2>
{{ CSRFInput }} {{ CSRFInput }}
<fieldset> <fieldset {{template "init-lang" . }}>
{{ if .Slug }} {{ if .Slug }}
{{ with .Active -}} {{ with .Active -}}
<label> <label>
@ -42,12 +42,17 @@
<input type="hidden" name="{{ .Active.Name }}" value="true"> <input type="hidden" name="{{ .Active.Name }}" value="true">
{{ end }} {{ end }}
{{ with .Name -}} {{ with .Name -}}
<label> <fieldset>
{{( pgettext "Name" "input")}}<br> <legend>{{( pgettext "Name" "input")}}</legend>
<input type="text" name="{{ .Name }}" value="{{ .Val }}" {{ template "lang-selector" . }}
required {{ template "error-attrs" . }}><br> {{ range $lang, $input := . -}}
</label> <label x-cloak x-show="lang === '{{ $lang }}'"><span>{{ $lang }}</span><br>
{{ template "error-message" . }} <input type="text" name="{{ $input.Name }}" value="{{ $input.Val }}"
{{ template "error-attrs" $input }}><br>
</label>
{{- end }}
{{ template "error-message" . }}
</fieldset>
{{- end }} {{- end }}
{{ with .Color -}} {{ with .Color -}}
<label> <label>

View File

@ -24,7 +24,6 @@
<tr> <tr>
<th scope="col">{{( pgettext "Color" "header" )}}</th> <th scope="col">{{( pgettext "Color" "header" )}}</th>
<th scope="col">{{( pgettext "Name" "header" )}}</th> <th scope="col">{{( pgettext "Name" "header" )}}</th>
<th scope="col">{{( pgettext "Translations" "header" )}}</th>
<th scope="col">{{( pgettext "Active" "season" )}}</th> <th scope="col">{{( pgettext "Active" "season" )}}</th>
</tr> </tr>
</thead> </thead>
@ -39,13 +38,6 @@
</svg> </svg>
</td> </td>
<td><a href="{{ .URL }}">{{ .Name }}</a></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> <td>{{ if .Active }}{{( gettext "Yes" )}}{{ else }}{{( gettext "No" )}}{{ end }}</td>
</tr> </tr>
{{- end }} {{- end }}

View File

@ -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 }}