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:
parent
b6cd2f7693
commit
2b702d6632
|
@ -46,7 +46,7 @@ func (h *AdminHandler) Handler(user *auth.User, company *auth.Company, conn *dat
|
|||
}
|
||||
default:
|
||||
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) {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
|
@ -134,7 +134,7 @@ func addLegal(w http.ResponseWriter, r *http.Request, user *auth.User, company *
|
|||
_, err := tx.Exec(ctx, `
|
||||
insert into legal_text (company_id, slug, name, content)
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
|
@ -155,7 +155,7 @@ func editLegal(w http.ResponseWriter, r *http.Request, user *auth.User, company
|
|||
, content = xmlparse(content $4)
|
||||
where company_id = $1
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
|
@ -191,16 +191,16 @@ func processLegalForm(w http.ResponseWriter, r *http.Request, user *auth.User, c
|
|||
}
|
||||
|
||||
type legalForm struct {
|
||||
company *auth.Company
|
||||
URL string
|
||||
Slug *form.Input
|
||||
Name form.I18nInput
|
||||
Content form.I18nInput
|
||||
DefaultLang string
|
||||
URL string
|
||||
Slug *form.Input
|
||||
Name form.I18nInput
|
||||
Content form.I18nInput
|
||||
}
|
||||
|
||||
func newLegalForm(company *auth.Company) *legalForm {
|
||||
f := &legalForm{
|
||||
company: company,
|
||||
DefaultLang: company.DefaultLanguage.String(),
|
||||
Slug: &form.Input{
|
||||
Name: "slug",
|
||||
},
|
||||
|
@ -210,7 +210,7 @@ func newLegalForm(company *auth.Company) *legalForm {
|
|||
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 content database.RecordArray
|
||||
row := conn.QueryRow(ctx, `
|
||||
|
@ -227,8 +227,8 @@ func (f *legalForm) FillFromDatabase(ctx context.Context, conn *database.Conn, s
|
|||
group by text.slug
|
||||
, text.name
|
||||
, text.content::text
|
||||
`, pgx.QueryResultFormats{pgx.BinaryFormatCode}, f.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 {
|
||||
`, pgx.QueryResultFormats{pgx.BinaryFormatCode}, company.ID, slug)
|
||||
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
|
||||
}
|
||||
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 {
|
||||
v := form.NewValidator(l)
|
||||
if v.CheckRequired(f.Name[f.company.DefaultLanguage.String()], 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."))
|
||||
if v.CheckRequired(f.Name[f.DefaultLang], l.GettextNoop("Name can not be empty.")) {
|
||||
v.CheckMinLength(f.Name[f.DefaultLang], 1, l.GettextNoop("Name must have at least one letter."))
|
||||
}
|
||||
return v.AllOK
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ func (h *AdminHandler) Handler(user *auth.User, company *auth.Company, conn *dat
|
|||
case "":
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
f := newLocationForm(company.Locales)
|
||||
f := newLocationForm(company)
|
||||
if err := f.FillFromDatabase(r.Context(), company, conn); err != nil {
|
||||
if !database.ErrorIsNotFound(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) {
|
||||
f := newLocationForm(company.Locales)
|
||||
f := newLocationForm(company)
|
||||
if ok, err := form.Handle(f, w, r, user); err != nil {
|
||||
return
|
||||
} else if !ok {
|
||||
|
@ -58,15 +58,15 @@ func updateLocationSettings(w http.ResponseWriter, r *http.Request, user *auth.U
|
|||
}
|
||||
tx := conn.MustBegin(r.Context())
|
||||
defer tx.Rollback(r.Context())
|
||||
var defaultLang = company.DefaultLanguage.String()
|
||||
if err := tx.SetupLocation(r.Context(), company.ID, f.Directions[defaultLang].Val, f.MapEmbed.Val, f.OpeningDates[defaultLang].Val); err != nil {
|
||||
if err := tx.SetupLocation(r.Context(), company.ID, f.Directions[f.DefaultLang].Val, f.MapEmbed.Val, f.OpeningDates[f.DefaultLang].Val); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
for lang := range company.Locales {
|
||||
if lang == company.DefaultLanguage {
|
||||
l := lang.String()
|
||||
if l == f.DefaultLang {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
@ -75,18 +75,20 @@ func updateLocationSettings(w http.ResponseWriter, r *http.Request, user *auth.U
|
|||
}
|
||||
|
||||
type locationForm struct {
|
||||
DefaultLang string
|
||||
Directions form.I18nInput
|
||||
MapEmbed *form.Input
|
||||
OpeningDates form.I18nInput
|
||||
}
|
||||
|
||||
func newLocationForm(locales locale.Locales) *locationForm {
|
||||
func newLocationForm(company *auth.Company) *locationForm {
|
||||
return &locationForm{
|
||||
Directions: form.NewI18nInput(locales, "directions"),
|
||||
DefaultLang: company.DefaultLanguage.String(),
|
||||
Directions: form.NewI18nInput(company.Locales, "directions"),
|
||||
MapEmbed: &form.Input{
|
||||
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.opening_dates::text
|
||||
`, pgx.QueryResultFormats{pgx.BinaryFormatCode}, company.ID).Scan(
|
||||
&f.Directions[company.DefaultLanguage.String()].Val,
|
||||
&f.Directions[f.DefaultLang].Val,
|
||||
&f.MapEmbed.Val,
|
||||
&f.OpeningDates[company.DefaultLanguage.String()].Val,
|
||||
&f.OpeningDates[f.DefaultLang].Val,
|
||||
&directions,
|
||||
&openingDates,
|
||||
)
|
||||
|
|
|
@ -28,7 +28,7 @@
|
|||
{{ end }}
|
||||
</h2>
|
||||
{{ CSRFInput }}
|
||||
<fieldset x-data="{ lang: '{{ .DefaultLang }}' }">
|
||||
<fieldset {{ template "init-lang" . }}>
|
||||
{{ with .Media -}}
|
||||
{{ template "media-picker" . }}
|
||||
{{- end }}
|
||||
|
|
|
@ -28,7 +28,7 @@
|
|||
{{ end }}
|
||||
</h2>
|
||||
{{ CSRFInput }}
|
||||
<fieldset x-data="{ lang: '{{ .DefaultLang }}' }">
|
||||
<fieldset {{ template "init-lang" . }}>
|
||||
{{ with $field := .Icon -}}
|
||||
<fieldset class="icon-input">
|
||||
<legend>{{( pgettext "Icon" "input")}}</legend>
|
||||
|
|
|
@ -28,7 +28,7 @@
|
|||
{{ end }}
|
||||
</h2>
|
||||
{{ CSRFInput }}
|
||||
<fieldset x-data="{ lang: '{{ .DefaultLang }}' }">
|
||||
<fieldset {{ template "init-lang" . }}>
|
||||
{{ with .Name -}}
|
||||
<fieldset>
|
||||
<legend>{{( pgettext "Name" "input")}}</legend>
|
||||
|
|
|
@ -28,7 +28,7 @@
|
|||
{{ end }}
|
||||
</h2>
|
||||
{{ CSRFInput }}
|
||||
<fieldset x-data="{ lang: '{{ .DefaultLang }}' }">
|
||||
<fieldset {{ template "init-lang" . }}>
|
||||
{{ if .Slug }}
|
||||
{{ with .Active -}}
|
||||
<label>
|
||||
|
|
|
@ -28,7 +28,7 @@
|
|||
{{ end }}
|
||||
</h2>
|
||||
{{ CSRFInput }}
|
||||
<fieldset x-data="{ lang: '{{ .DefaultLang }}' }">
|
||||
<fieldset {{ template "init-lang" . }}>
|
||||
{{ with .Media -}}
|
||||
{{ template "media-picker" . }}
|
||||
{{- end }}
|
||||
|
|
|
@ -42,6 +42,8 @@
|
|||
</fieldset>
|
||||
{{- end }}
|
||||
|
||||
{{ define "init-lang" }}x-data="{ lang: '{{ .DefaultLang }}' }"{{ end }}
|
||||
|
||||
{{ define "lang-selector" -}}
|
||||
<div class="lang-selector" role="toolbar">
|
||||
{{ range $lang, $input := . -}}
|
||||
|
|
|
@ -28,7 +28,7 @@
|
|||
{{ end }}
|
||||
</h2>
|
||||
{{ CSRFInput }}
|
||||
<fieldset x-data="{ lang: 'ca' }">
|
||||
<fieldset {{ template "init-lang" . }}>
|
||||
{{ if .URL }}
|
||||
<input type="hidden" name="{{ .Slug.Name }}" value="{{ .Slug.Val }}">
|
||||
{{ else }}
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
<form data-hx-put="/admin/location">
|
||||
<h2>{{( pgettext "Location Settings" "title" )}}</h2>
|
||||
{{ CSRFInput }}
|
||||
<fieldset x-data="{ lang: 'ca' }">
|
||||
<fieldset {{ template "init-lang" . }}>
|
||||
{{ with .Directions -}}
|
||||
<fieldset>
|
||||
<legend>{{( pgettext "Directions" "input" )}}</legend>
|
||||
|
|
Loading…
Reference in New Issue