Add golang template to initialize x-data for translation in admin

Had to add the DefaultLang to legal and location forms in order to use
the same template for all.
This commit is contained in:
jordi fita mas 2024-01-12 19:51:12 +01:00
parent b6cd2f7693
commit 2b702d6632
10 changed files with 36 additions and 32 deletions

View File

@ -46,7 +46,7 @@ func (h *AdminHandler) Handler(user *auth.User, company *auth.Company, conn *dat
} }
default: default:
f := newLegalForm(company) f := newLegalForm(company)
if err := f.FillFromDatabase(r.Context(), conn, head); err != nil { if err := f.FillFromDatabase(r.Context(), conn, company, head); err != nil {
if database.ErrorIsNotFound(err) { if database.ErrorIsNotFound(err) {
http.NotFound(w, r) http.NotFound(w, r)
return return
@ -134,7 +134,7 @@ func addLegal(w http.ResponseWriter, r *http.Request, user *auth.User, company *
_, err := tx.Exec(ctx, ` _, err := tx.Exec(ctx, `
insert into legal_text (company_id, slug, name, content) insert into legal_text (company_id, slug, name, content)
values ($1, $2, $3, xmlparse(content $4)) values ($1, $2, $3, xmlparse(content $4))
`, company.ID, f.Slug.Val, f.Name[company.DefaultLanguage.String()].Val, f.Content[company.DefaultLanguage.String()]) `, company.ID, f.Slug.Val, f.Name[f.DefaultLang].Val, f.Content[f.DefaultLang])
if err != nil { if err != nil {
return err return err
} }
@ -155,7 +155,7 @@ func editLegal(w http.ResponseWriter, r *http.Request, user *auth.User, company
, content = xmlparse(content $4) , content = xmlparse(content $4)
where company_id = $1 where company_id = $1
and slug = $2 and slug = $2
`, company.ID, f.Slug.Val, f.Name[company.DefaultLanguage.String()].Val, f.Content[company.DefaultLanguage.String()]) `, company.ID, f.Slug.Val, f.Name[f.DefaultLang].Val, f.Content[f.DefaultLang])
if err != nil { if err != nil {
return err return err
} }
@ -191,16 +191,16 @@ func processLegalForm(w http.ResponseWriter, r *http.Request, user *auth.User, c
} }
type legalForm struct { type legalForm struct {
company *auth.Company DefaultLang string
URL string URL string
Slug *form.Input Slug *form.Input
Name form.I18nInput Name form.I18nInput
Content form.I18nInput Content form.I18nInput
} }
func newLegalForm(company *auth.Company) *legalForm { func newLegalForm(company *auth.Company) *legalForm {
f := &legalForm{ f := &legalForm{
company: company, DefaultLang: company.DefaultLanguage.String(),
Slug: &form.Input{ Slug: &form.Input{
Name: "slug", Name: "slug",
}, },
@ -210,7 +210,7 @@ func newLegalForm(company *auth.Company) *legalForm {
return f return f
} }
func (f *legalForm) FillFromDatabase(ctx context.Context, conn *database.Conn, slug string) error { func (f *legalForm) FillFromDatabase(ctx context.Context, conn *database.Conn, company *auth.Company, slug string) error {
var name database.RecordArray var name database.RecordArray
var content database.RecordArray var content database.RecordArray
row := conn.QueryRow(ctx, ` row := conn.QueryRow(ctx, `
@ -227,8 +227,8 @@ func (f *legalForm) FillFromDatabase(ctx context.Context, conn *database.Conn, s
group by text.slug group by text.slug
, text.name , text.name
, text.content::text , text.content::text
`, pgx.QueryResultFormats{pgx.BinaryFormatCode}, f.company.ID, slug) `, pgx.QueryResultFormats{pgx.BinaryFormatCode}, company.ID, slug)
if err := row.Scan(&f.URL, &f.Slug.Val, &f.Name[f.company.DefaultLanguage.String()].Val, &f.Content[f.company.DefaultLanguage.String()].Val, &name, &content); err != nil { if err := row.Scan(&f.URL, &f.Slug.Val, &f.Name[f.DefaultLang].Val, &f.Content[f.DefaultLang].Val, &name, &content); err != nil {
return err return err
} }
if err := f.Name.FillArray(name); err != nil { if err := f.Name.FillArray(name); err != nil {
@ -252,8 +252,8 @@ func (f *legalForm) Parse(r *http.Request) error {
func (f *legalForm) Valid(l *locale.Locale) bool { func (f *legalForm) Valid(l *locale.Locale) bool {
v := form.NewValidator(l) v := form.NewValidator(l)
if v.CheckRequired(f.Name[f.company.DefaultLanguage.String()], l.GettextNoop("Name can not be empty.")) { if v.CheckRequired(f.Name[f.DefaultLang], l.GettextNoop("Name can not be empty.")) {
v.CheckMinLength(f.Name[f.company.DefaultLanguage.String()], 1, l.GettextNoop("Name must have at least one letter.")) v.CheckMinLength(f.Name[f.DefaultLang], 1, l.GettextNoop("Name must have at least one letter."))
} }
return v.AllOK return v.AllOK
} }

View File

@ -30,7 +30,7 @@ func (h *AdminHandler) Handler(user *auth.User, company *auth.Company, conn *dat
case "": case "":
switch r.Method { switch r.Method {
case http.MethodGet: case http.MethodGet:
f := newLocationForm(company.Locales) f := newLocationForm(company)
if err := f.FillFromDatabase(r.Context(), company, conn); err != nil { if err := f.FillFromDatabase(r.Context(), company, conn); err != nil {
if !database.ErrorIsNotFound(err) { if !database.ErrorIsNotFound(err) {
panic(err) panic(err)
@ -49,7 +49,7 @@ func (h *AdminHandler) Handler(user *auth.User, company *auth.Company, conn *dat
} }
func updateLocationSettings(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn) { func updateLocationSettings(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn) {
f := newLocationForm(company.Locales) f := newLocationForm(company)
if ok, err := form.Handle(f, w, r, user); err != nil { if ok, err := form.Handle(f, w, r, user); err != nil {
return return
} else if !ok { } else if !ok {
@ -58,15 +58,15 @@ func updateLocationSettings(w http.ResponseWriter, r *http.Request, user *auth.U
} }
tx := conn.MustBegin(r.Context()) tx := conn.MustBegin(r.Context())
defer tx.Rollback(r.Context()) defer tx.Rollback(r.Context())
var defaultLang = company.DefaultLanguage.String() if err := tx.SetupLocation(r.Context(), company.ID, f.Directions[f.DefaultLang].Val, f.MapEmbed.Val, f.OpeningDates[f.DefaultLang].Val); err != nil {
if err := tx.SetupLocation(r.Context(), company.ID, f.Directions[defaultLang].Val, f.MapEmbed.Val, f.OpeningDates[defaultLang].Val); err != nil {
panic(err) panic(err)
} }
for lang := range company.Locales { for lang := range company.Locales {
if lang == company.DefaultLanguage { l := lang.String()
if l == f.DefaultLang {
continue continue
} }
if err := tx.TranslateLocation(r.Context(), company.ID, lang, f.Directions[lang.String()].Val, f.OpeningDates[lang.String()].Val); err != nil { if err := tx.TranslateLocation(r.Context(), company.ID, lang, f.Directions[l].Val, f.OpeningDates[l].Val); err != nil {
panic(err) panic(err)
} }
} }
@ -75,18 +75,20 @@ func updateLocationSettings(w http.ResponseWriter, r *http.Request, user *auth.U
} }
type locationForm struct { type locationForm struct {
DefaultLang string
Directions form.I18nInput Directions form.I18nInput
MapEmbed *form.Input MapEmbed *form.Input
OpeningDates form.I18nInput OpeningDates form.I18nInput
} }
func newLocationForm(locales locale.Locales) *locationForm { func newLocationForm(company *auth.Company) *locationForm {
return &locationForm{ return &locationForm{
Directions: form.NewI18nInput(locales, "directions"), DefaultLang: company.DefaultLanguage.String(),
Directions: form.NewI18nInput(company.Locales, "directions"),
MapEmbed: &form.Input{ MapEmbed: &form.Input{
Name: "map_embed", Name: "map_embed",
}, },
OpeningDates: form.NewI18nInput(locales, "opening_dates"), OpeningDates: form.NewI18nInput(company.Locales, "opening_dates"),
} }
} }
@ -106,9 +108,9 @@ func (f *locationForm) FillFromDatabase(ctx context.Context, company *auth.Compa
, location.map_embed::text , location.map_embed::text
, location.opening_dates::text , location.opening_dates::text
`, pgx.QueryResultFormats{pgx.BinaryFormatCode}, company.ID).Scan( `, pgx.QueryResultFormats{pgx.BinaryFormatCode}, company.ID).Scan(
&f.Directions[company.DefaultLanguage.String()].Val, &f.Directions[f.DefaultLang].Val,
&f.MapEmbed.Val, &f.MapEmbed.Val,
&f.OpeningDates[company.DefaultLanguage.String()].Val, &f.OpeningDates[f.DefaultLang].Val,
&directions, &directions,
&openingDates, &openingDates,
) )

View File

@ -28,7 +28,7 @@
{{ end }} {{ end }}
</h2> </h2>
{{ CSRFInput }} {{ CSRFInput }}
<fieldset x-data="{ lang: '{{ .DefaultLang }}' }"> <fieldset {{ template "init-lang" . }}>
{{ with .Media -}} {{ with .Media -}}
{{ template "media-picker" . }} {{ template "media-picker" . }}
{{- end }} {{- end }}

View File

@ -28,7 +28,7 @@
{{ end }} {{ end }}
</h2> </h2>
{{ CSRFInput }} {{ CSRFInput }}
<fieldset x-data="{ lang: '{{ .DefaultLang }}' }"> <fieldset {{ template "init-lang" . }}>
{{ with $field := .Icon -}} {{ with $field := .Icon -}}
<fieldset class="icon-input"> <fieldset class="icon-input">
<legend>{{( pgettext "Icon" "input")}}</legend> <legend>{{( pgettext "Icon" "input")}}</legend>

View File

@ -28,7 +28,7 @@
{{ end }} {{ end }}
</h2> </h2>
{{ CSRFInput }} {{ CSRFInput }}
<fieldset x-data="{ lang: '{{ .DefaultLang }}' }"> <fieldset {{ template "init-lang" . }}>
{{ with .Name -}} {{ with .Name -}}
<fieldset> <fieldset>
<legend>{{( pgettext "Name" "input")}}</legend> <legend>{{( pgettext "Name" "input")}}</legend>

View File

@ -28,7 +28,7 @@
{{ end }} {{ end }}
</h2> </h2>
{{ CSRFInput }} {{ CSRFInput }}
<fieldset x-data="{ lang: '{{ .DefaultLang }}' }"> <fieldset {{ template "init-lang" . }}>
{{ if .Slug }} {{ if .Slug }}
{{ with .Active -}} {{ with .Active -}}
<label> <label>

View File

@ -28,7 +28,7 @@
{{ end }} {{ end }}
</h2> </h2>
{{ CSRFInput }} {{ CSRFInput }}
<fieldset x-data="{ lang: '{{ .DefaultLang }}' }"> <fieldset {{ template "init-lang" . }}>
{{ with .Media -}} {{ with .Media -}}
{{ template "media-picker" . }} {{ template "media-picker" . }}
{{- end }} {{- end }}

View File

@ -42,6 +42,8 @@
</fieldset> </fieldset>
{{- end }} {{- end }}
{{ define "init-lang" }}x-data="{ lang: '{{ .DefaultLang }}' }"{{ end }}
{{ define "lang-selector" -}} {{ define "lang-selector" -}}
<div class="lang-selector" role="toolbar"> <div class="lang-selector" role="toolbar">
{{ range $lang, $input := . -}} {{ range $lang, $input := . -}}

View File

@ -28,7 +28,7 @@
{{ end }} {{ end }}
</h2> </h2>
{{ CSRFInput }} {{ CSRFInput }}
<fieldset x-data="{ lang: 'ca' }"> <fieldset {{ template "init-lang" . }}>
{{ if .URL }} {{ if .URL }}
<input type="hidden" name="{{ .Slug.Name }}" value="{{ .Slug.Val }}"> <input type="hidden" name="{{ .Slug.Name }}" value="{{ .Slug.Val }}">
{{ else }} {{ else }}

View File

@ -11,7 +11,7 @@
<form data-hx-put="/admin/location"> <form data-hx-put="/admin/location">
<h2>{{( pgettext "Location Settings" "title" )}}</h2> <h2>{{( pgettext "Location Settings" "title" )}}</h2>
{{ CSRFInput }} {{ CSRFInput }}
<fieldset x-data="{ lang: 'ca' }"> <fieldset {{ template "init-lang" . }}>
{{ with .Directions -}} {{ with .Directions -}}
<fieldset> <fieldset>
<legend>{{( pgettext "Directions" "input" )}}</legend> <legend>{{( pgettext "Directions" "input" )}}</legend>