Replace L10nInput with I18nInput in service

locale.Translation and form.L10nInput are no longer used.

The translation type in Postgres is now also useless, and i believe it
was never used, but i keep it because I already have a tag and i can not
just remove it, meaning that dropping it is more trouble that worth it.
This commit is contained in:
jordi fita mas 2024-01-12 21:06:12 +01:00
parent 58c3b607a1
commit 4e126237a0
8 changed files with 116 additions and 232 deletions

View File

@ -131,3 +131,17 @@ func (tx *Tx) TranslateSeason(ctx context.Context, slug string, langTag language
_, err := tx.Exec(ctx, "select translate_season($1, $2, $3)", slug, langTag, name)
return err
}
func (tx *Tx) AddService(ctx context.Context, companyID int, icon string, name string, description string) (int, error) {
return tx.GetInt(ctx, "select add_service($1, $2, $3, $4)", companyID, icon, name, description)
}
func (tx *Tx) EditService(ctx context.Context, id int, icon string, name string, description string) error {
_, err := tx.Exec(ctx, "select edit_service($1, $2, $3, $4)", id, icon, name, description)
return err
}
func (tx *Tx) TranslateService(ctx context.Context, id int, langTag language.Tag, name string, description string) error {
_, err := tx.Exec(ctx, "select translate_service($1, $2, $3, $4)", id, langTag, name, description)
return err
}

View File

@ -42,20 +42,6 @@ func (input *Input) Int() int {
return i
}
func (input *Input) L10nInput() *L10nInput {
return &L10nInput{
Input: Input{
Name: input.Name,
},
Source: input.Val,
}
}
type L10nInput struct {
Input
Source string
}
type I18nInput map[string]*Input
func NewI18nInput(locales locale.Locales, name string) I18nInput {

View File

@ -1,12 +0,0 @@
/*
* SPDX-FileCopyrightText: 2023 jordi fita mas <jfita@peritasoft.com>
* SPDX-License-Identifier: AGPL-3.0-only
*/
package locale
type Translation struct {
URL string
Endonym string
Missing bool
}

View File

@ -53,7 +53,7 @@ func (h *AdminHandler) Handler(user *auth.User, company *auth.Company, conn *dat
case "new":
switch r.Method {
case http.MethodGet:
f := newServiceForm(r.Context(), conn)
f := newServiceForm(r.Context(), conn, company)
f.MustRender(w, r, user, company)
default:
httplib.MethodNotAllowed(w, r, http.MethodGet)
@ -66,7 +66,7 @@ func (h *AdminHandler) Handler(user *auth.User, company *auth.Company, conn *dat
http.NotFound(w, r)
return
}
f := newServiceForm(r.Context(), conn)
f := newServiceForm(r.Context(), conn, company)
if err := f.FillFromDatabase(r.Context(), conn, id); err != nil {
if database.ErrorIsNotFound(err) {
http.NotFound(w, r)
@ -75,10 +75,8 @@ 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:
@ -91,23 +89,7 @@ func (h *AdminHandler) Handler(user *auth.User, company *auth.Company, conn *dat
httplib.MethodNotAllowed(w, r, http.MethodGet, http.MethodPut, http.MethodDelete)
}
default:
loc, ok := h.locales.Get(langTag)
if !ok {
http.NotFound(w, r)
return
}
l10n := newServiceL10nForm(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:
editServiceL10n(w, r, user, company, conn, l10n)
default:
httplib.MethodNotAllowed(w, r, http.MethodGet, http.MethodPut)
}
http.NotFound(w, r)
}
}
})
@ -136,10 +118,9 @@ func (page *servicesIndex) MustRender(w http.ResponseWriter, r *http.Request, us
}
type serviceEntry struct {
URL string
Icon string
Name string
Translations []*locale.Translation
URL string
Icon string
Name string
}
func collectServiceEntries(ctx context.Context, company *auth.Company, conn *database.Conn) ([]*serviceEntry, error) {
@ -147,18 +128,10 @@ func collectServiceEntries(ctx context.Context, company *auth.Company, conn *dat
select '/admin/services/' || service_id
, icon_name
, service.name
, array_agg((lang_tag, endonym, not exists (select 1 from service_i18n as i18n where i18n.service_id = service.service_id and i18n.lang_tag = language.lang_tag))::translation order by endonym)
from service
join company using (company_id)
, language
where lang_tag <> default_lang_tag
and language.selectable
and service.company_id = $1
group by service_id
, icon_name
, service.name
where service.company_id = $1
order by service.name
`, pgx.QueryResultFormats{pgx.BinaryFormatCode}, company.ID)
`, company.ID)
if err != nil {
return nil, err
}
@ -167,17 +140,9 @@ func collectServiceEntries(ctx context.Context, company *auth.Company, conn *dat
var services []*serviceEntry
for rows.Next() {
entry := &serviceEntry{}
var translations database.RecordArray
if err = rows.Scan(&entry.URL, &entry.Icon, &entry.Name, &translations); err != nil {
if err = rows.Scan(&entry.URL, &entry.Icon, &entry.Name); 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),
})
}
services = append(services, entry)
}
@ -185,25 +150,61 @@ func collectServiceEntries(ctx context.Context, company *auth.Company, conn *dat
}
func addService(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn) {
f := newServiceForm(r.Context(), conn)
f := newServiceForm(r.Context(), conn, company)
processServiceForm(w, r, user, company, conn, f, func(ctx context.Context, tx *database.Tx) error {
var err error
if f.ID, err = tx.AddService(ctx, company.ID, f.Icon.String(), f.Name[f.DefaultLang].Val, f.Description[f.DefaultLang].Val); err != nil {
return err
}
return translateService(ctx, tx, company, f)
})
}
func processServiceForm(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn, f *serviceForm, act func(context.Context, *database.Tx) error) {
if ok, err := form.Handle(f, w, r, user); err != nil {
return
} else if !ok {
f.MustRender(w, r, user, company)
return
}
conn.MustExec(r.Context(), "select add_service($1, $2, $3, $4)", company.ID, f.Icon, f.Name, f.Description)
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/services", http.StatusSeeOther)
}
func translateService(ctx context.Context, tx *database.Tx, company *auth.Company, f *serviceForm) error {
for lang := range company.Locales {
l := lang.String()
if l == f.DefaultLang {
continue
}
if err := tx.TranslateService(ctx, f.ID, lang, f.Name[l].Val, f.Description[l].Val); err != nil {
return err
}
}
return nil
}
func editService(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn, f *serviceForm) {
processServiceForm(w, r, user, company, conn, f, func(ctx context.Context, tx *database.Tx) error {
if err := tx.EditService(ctx, f.ID, f.Icon.String(), f.Name[f.DefaultLang].Val, f.Description[f.DefaultLang].Val); err != nil {
return err
}
return translateService(ctx, tx, company, f)
})
if ok, err := form.Handle(f, w, r, user); err != nil {
return
} else if !ok {
f.MustRender(w, r, user, company)
return
}
conn.MustExec(r.Context(), "select edit_service($1, $2, $3, $4)", f.ID, f.Icon, f.Name, f.Description)
httplib.Redirect(w, r, "/admin/services", http.StatusSeeOther)
}
@ -217,37 +218,52 @@ func deleteService(w http.ResponseWriter, r *http.Request, user *auth.User, conn
}
type serviceForm struct {
DefaultLang string
ID int
Icon *form.Select
Name *form.Input
Description *form.Input
Name form.I18nInput
Description form.I18nInput
}
func newServiceForm(ctx context.Context, conn *database.Conn) *serviceForm {
func newServiceForm(ctx context.Context, conn *database.Conn, company *auth.Company) *serviceForm {
return &serviceForm{
DefaultLang: company.DefaultLanguage.String(),
Icon: &form.Select{
Name: "icon",
Options: form.MustGetOptions(ctx, conn, "select icon_name, icon_name from icon order by 1"),
},
Name: &form.Input{
Name: "name",
},
Description: &form.Input{
Name: "description",
},
Name: form.NewI18nInput(company.Locales, "name"),
Description: form.NewI18nInput(company.Locales, "description"),
}
}
func (f *serviceForm) FillFromDatabase(ctx context.Context, conn *database.Conn, id int) error {
f.ID = id
var name database.RecordArray
var description database.RecordArray
row := conn.QueryRow(ctx, `
select array[icon_name]
, name
, description
, service.name
, service.description::text
, array_agg((lang_tag, i18n.name))
, array_agg((lang_tag, i18n.description::text))
from service
left join service_i18n as i18n using (service_id)
where service_id = $1
`, id)
return row.Scan(&f.Icon.Selected, &f.Name.Val, &f.Description.Val)
group by icon_name
, service.name
, service.description::text
`, pgx.QueryResultFormats{pgx.BinaryFormatCode}, id)
if err := row.Scan(&f.Icon.Selected, &f.Name[f.DefaultLang].Val, &f.Description[f.DefaultLang].Val, &name, &description); err != nil {
return err
}
if err := f.Name.FillArray(name); err != nil {
return err
}
if err := f.Description.FillArray(description); err != nil {
return err
}
return nil
}
func (f *serviceForm) Parse(r *http.Request) error {
@ -263,7 +279,7 @@ func (f *serviceForm) Parse(r *http.Request) error {
func (f *serviceForm) Valid(l *locale.Locale) bool {
v := form.NewValidator(l)
v.CheckSelectedOptions(f.Icon, l.GettextNoop("Selected icon is not valid."))
v.CheckRequired(f.Name, l.GettextNoop("Name can not be empty."))
v.CheckRequired(f.Name[f.DefaultLang], l.GettextNoop("Name can not be empty."))
return v.AllOK
}

View File

@ -1,75 +0,0 @@
/*
* SPDX-FileCopyrightText: 2023 jordi fita mas <jfita@peritasoft.com>
* SPDX-License-Identifier: AGPL-3.0-only
*/
package services
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 serviceL10nForm struct {
Locale *locale.Locale
ID int
Name *form.L10nInput
Description *form.L10nInput
}
func newServiceL10nForm(f *serviceForm, loc *locale.Locale) *serviceL10nForm {
return &serviceL10nForm{
Locale: loc,
ID: f.ID,
Name: f.Name.L10nInput(),
Description: f.Description.L10nInput(),
}
}
func (l10n *serviceL10nForm) FillFromDatabase(ctx context.Context, conn *database.Conn) error {
row := conn.QueryRow(ctx, `
select coalesce(i18n.name, '') as l10n_name
, coalesce(i18n.description, '') as l10n_description
from service
left join service_i18n as i18n on service.service_id = i18n.service_id and i18n.lang_tag = $1
where service.service_id = $2
`, l10n.Locale.Language, l10n.ID)
return row.Scan(&l10n.Name.Val, &l10n.Description.Val)
}
func (l10n *serviceL10nForm) MustRender(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company) {
template.MustRenderAdmin(w, r, user, company, "services/l10n.gohtml", l10n)
}
func editServiceL10n(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn, l10n *serviceL10nForm) {
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_service($1, $2, $3, $4)", l10n.ID, l10n.Locale.Language, l10n.Name, l10n.Description)
httplib.Redirect(w, r, "/admin/services", http.StatusSeeOther)
}
func (l10n *serviceL10nForm) Parse(r *http.Request) error {
if err := r.ParseForm(); err != nil {
return err
}
l10n.Name.FillValue(r)
l10n.Description.FillValue(r)
return nil
}
func (l10n *serviceL10nForm) 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 }}
</h2>
{{ CSRFInput }}
<fieldset>
<fieldset {{ template "init-lang" . }}>
{{ with $field := .Icon -}}
<fieldset class="icon-input">
<legend>{{( pgettext "Icon" "input")}}</legend>
@ -48,19 +48,30 @@
</fieldset>
{{- 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 }}"
required {{ template "error-attrs" $input }}><br>
</label>
{{- end }}
{{ template "error-message" . }}
</fieldset>
{{- end }}
{{ with .Description -}}
<label>
{{( pgettext "Description" "input")}}<br>
<textarea class="html" name="{{ .Name }}" {{ template "error-attrs" . }}>{{ .Val }}</textarea><br>
</label>
{{ template "error-message" . }}
<fieldset>
<legend>{{( pgettext "Description" "input")}}</legend>
{{ template "lang-selector" . }}
{{ range $lang, $input := . -}}
<label x-cloak x-show="lang === '{{ $lang }}'"><span>{{ $lang }}</span><br>
<textarea class="html"
name="{{ $input.Name }}" {{ template "error-attrs" $input }}>{{ $input.Val }}</textarea><br>
</label>
{{- end }}
{{ template "error-message" . }}
</fieldset>
{{- end }}
</fieldset>
<footer>

View File

@ -60,7 +60,6 @@
<thead>
<tr>
<th scope="col">{{( pgettext "Service" "header" )}}</th>
<th scope="col">{{( pgettext "Translations" "header" )}}</th>
<th scope="col">{{( pgettext "Actions" "header" )}}</th>
</tr>
</thead>
@ -69,13 +68,6 @@
{{ range .Services -}}
<tr>
<td class="icon_{{ .Icon }}"><a href="{{ .URL }}">{{ .Name }}</a></td>
<td>
{{ range .Translations }}
<a
{{ if .Missing }}class="missing-translation"{{ end }}
href="{{ .URL }}">{{ .Endonym }}</a>
{{ end }}
</td>
<td>
<form data-hx-delete="{{ .URL }}"
data-hx-confirm="{{ $confirm }}"

View File

@ -1,48 +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/services.serviceL10nForm*/ -}}
{{printf (pgettext "Translate Service to %s" "title") .Locale.Endonym }}
{{- end }}
{{ define "content" -}}
{{- /*gotype: dev.tandem.ws/tandem/camper/pkg/services.serviceL10nForm*/ -}}
<form data-hx-put="/admin/services/{{ .ID }}/{{ .Locale.Language }}">
<h2>
{{printf (pgettext "Translate Service 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 }}
{{ with .Description -}}
<fieldset>
<legend{{( pgettext "Description" "input")}}></legend>
{{( gettext "Source:" )}}<br>
{{ .Source | raw }}<br>
<label>
{{( pgettext "Translation:" "input" )}}
<textarea class="html"
name="{{ .Name }}" {{ template "error-attrs" . }}>{{ .Val }}</textarea><br>
</label>
{{ template "error-message" . }}
</fieldset>
{{- end }}
</fieldset>
<footer>
<button type="submit">{{( pgettext "Translate" "action" )}}</button>
</footer>
</form>
{{- end }}