diff --git a/admin/openapi/admin.json b/admin/openapi/admin.json index f5374bd2..c4226263 100644 --- a/admin/openapi/admin.json +++ b/admin/openapi/admin.json @@ -2885,6 +2885,36 @@ ], "type": "object" }, + "cabana.RecordLocaleVariant": { + "properties": { + "current": { + "type": "boolean" + }, + "id": { + "type": "integer" + }, + "label": { + "type": "string" + }, + "locale": { + "type": "string" + }, + "states": { + "items": { + "type": "string" + }, + "type": "array" + } + }, + "required": [ + "current", + "id", + "label", + "locale", + "states" + ], + "type": "object" + }, "cabana.RecordMeta": { "properties": { "actions": { @@ -2906,6 +2936,13 @@ }, "type": "object" }, + "localeVariants": { + "description": "LocaleVariants are the locale versions of the record for the update\nform's locale switcher, in display order. Only the show and update\nresponses of a controller whose records have locale versions (the mail\ntemplates screen) carry it; it is omitted otherwise.", + "items": { + "$ref": "#/components/schemas/cabana.RecordLocaleVariant" + }, + "type": "array" + }, "notices": { "items": { "$ref": "#/components/schemas/cabana.FieldNotice" diff --git a/admin/src/api/schema.d.ts b/admin/src/api/schema.d.ts index 297a3e67..0e22b24c 100644 --- a/admin/src/api/schema.d.ts +++ b/admin/src/api/schema.d.ts @@ -6998,6 +6998,13 @@ export interface components { data: components["schemas"]["cabana.AdminRecord"]; meta: components["schemas"]["cabana.RecordMeta"]; }; + "cabana.RecordLocaleVariant": { + current: boolean; + id: number; + label: string; + locale: string; + states: string[]; + }; "cabana.RecordMeta": { actions?: components["schemas"]["cabana.RecordAction"][]; /** @@ -7010,6 +7017,13 @@ export interface components { labels: { [key: string]: components["schemas"]["cabana.RelationOption"][]; }; + /** + * @description LocaleVariants are the locale versions of the record for the update + * form's locale switcher, in display order. Only the show and update + * responses of a controller whose records have locale versions (the mail + * templates screen) carry it; it is omitted otherwise. + */ + localeVariants?: components["schemas"]["cabana.RecordLocaleVariant"][]; notices?: components["schemas"]["cabana.FieldNotice"][]; /** * @description PermissionLocks are, per permissioneditor field, the permissions this diff --git a/admin/src/components/form/LocaleVariantSwitcher.vue b/admin/src/components/form/LocaleVariantSwitcher.vue new file mode 100644 index 00000000..1db5114d --- /dev/null +++ b/admin/src/components/form/LocaleVariantSwitcher.vue @@ -0,0 +1,68 @@ + + + diff --git a/admin/src/views/FormView.vue b/admin/src/views/FormView.vue index 4e8b0335..e12a4f69 100644 --- a/admin/src/views/FormView.vue +++ b/admin/src/views/FormView.vue @@ -22,6 +22,7 @@ import { mapWinterUrl } from '../app/winterUrl' import FormErrorBanner from '../components/form/FormErrorBanner.vue' import FormGrid from '../components/form/FormGrid.vue' import FormTabs from '../components/form/FormTabs.vue' +import LocaleVariantSwitcher from '../components/form/LocaleVariantSwitcher.vue' import { DEPENDENT_CACHE, FORM_ASSETS, @@ -105,6 +106,8 @@ const recordTitle = ref('') const actions = ref([]) /** When false, Delete is hidden; absent means true (existing controllers). */ const deletable = ref(true) +/** The record's locale versions (meta.localeVariants); the switcher shows from two. */ +const localeVariants = ref>([]) const acting = ref(false) const partialReload = ref(0) const errors = ref>({}) @@ -266,6 +269,9 @@ function adopt(record: RecordEnvelope | undefined): void { recordTitle.value = record.meta.title ?? '' actions.value = record.meta.actions ?? [] deletable.value = record.meta.deletable !== false + if (record.meta.localeVariants) { + localeVariants.value = record.meta.localeVariants + } } saved.value = snapshot(editablePayload(fields.value, values.value, mode, permissionLocks.value)) } @@ -705,6 +711,12 @@ void load()

{{ loading ? '' : title }}

{{ subtitle }}

+
diff --git a/admin/tests/form/FormViewMail.test.ts b/admin/tests/form/FormViewMail.test.ts index 61c5277f..0a6d3f9f 100644 --- a/admin/tests/form/FormViewMail.test.ts +++ b/admin/tests/form/FormViewMail.test.ts @@ -107,6 +107,26 @@ describe('FormView mail footer actions and meta.deletable', () => { expect(absent.wrapper.find('[data-action="delete"]').exists()).toBe(true) }) + it('shows the locale switcher from meta.localeVariants when there are two or more', async () => { + const two = await mountApp( + '/summercms/cabana/mailtemplates/7', + routes({ + [`GET ${RECORD}`]: mailRecord({ + localeVariants: [ + { id: 7, locale: '', label: 'Default', states: [], current: true }, + { id: 9, locale: 'de', label: 'de', states: ['Customized'], current: false }, + ], + }), + }), + ) + const select = two.wrapper.find('header select[data-locale-variant]') + expect(select.exists()).toBe(true) + expect(select.findAll('option').map((option) => option.text())).toEqual(['Default', 'de — Customized']) + + const none = await mountApp('/summercms/cabana/mailtemplates/7', routes()) + expect(none.wrapper.find('[data-locale-variant]').exists()).toBe(false) + }) + it('toasts 403 and 409 paths from record actions', async () => { const forbidden = await mountApp( '/summercms/cabana/mailtemplates/7', diff --git a/admin/tests/form/LocaleVariantSwitcher.test.ts b/admin/tests/form/LocaleVariantSwitcher.test.ts new file mode 100644 index 00000000..5937630c --- /dev/null +++ b/admin/tests/form/LocaleVariantSwitcher.test.ts @@ -0,0 +1,75 @@ +import { afterEach, beforeEach, describe, expect, it } from 'vitest' +import { enableAutoUnmount, flushPromises, mount } from '@vue/test-utils' +import { defineComponent, h } from 'vue' +import { createMemoryHistory, createRouter } from 'vue-router' +import LocaleVariantSwitcher from '../../src/components/form/LocaleVariantSwitcher.vue' +import { setBundle } from '../../src/app/i18n' +import { resetState } from '../helpers' + +const variants = [ + { id: 7, locale: '', label: 'Default', states: [], current: true }, + { id: 9, locale: 'de', label: 'de', states: ['Customized', 'File changed'], current: false }, + { id: 8, locale: 'en', label: 'en', states: [], current: false }, +] + +const Stub = defineComponent({ render: () => h('div') }) + +async function mountSwitcher(allow: () => boolean) { + const router = createRouter({ + history: createMemoryHistory(), + routes: [{ path: '/:vendor/:plugin/:controller/:id', name: 'update', component: Stub }], + }) + router.beforeEach((_to, from) => (from.matched.length === 0 ? true : allow())) + await router.push('/summercms/cabana/mailtemplates/7') + const wrapper = mount(LocaleVariantSwitcher, { + props: { variants }, + global: { plugins: [router] }, + attachTo: document.body, + }) + return { wrapper, router } +} + +beforeEach(() => { + resetState() + setBundle({ 'backend::lang.form.locale_variant': { other: 'Language' } }, 'en') +}) + +afterEach(() => { + document.body.innerHTML = '' +}) + +enableAutoUnmount(afterEach) + +describe('LocaleVariantSwitcher', () => { + it('is a labelled select of the variants with their states, the current one selected', async () => { + const { wrapper } = await mountSwitcher(() => true) + const select = wrapper.find('select[data-locale-variant]') + expect(select.exists()).toBe(true) + const label = wrapper.find('label') + expect(label.text()).toBe('Language') + expect(label.attributes('for')).toBe(select.attributes('id')) + expect(wrapper.findAll('option').map((option) => option.text())).toEqual([ + 'Default', + 'de — Customized, File changed', + 'en', + ]) + expect(select.element.value).toBe('7') + }) + + it('opens the chosen version on the same route', async () => { + const { wrapper, router } = await mountSwitcher(() => true) + await wrapper.find('select').setValue('9') + await flushPromises() + expect(router.currentRoute.value.params.id).toBe('9') + expect(router.currentRoute.value.path).toBe('/summercms/cabana/mailtemplates/9') + }) + + it('puts the selection back when the navigation is refused', async () => { + const { wrapper, router } = await mountSwitcher(() => false) + const select = wrapper.find('select') + await select.setValue('8') + await flushPromises() + expect(router.currentRoute.value.params.id).toBe('7') + expect(select.element.value).toBe('7') + }) +}) diff --git a/docs/backend/forms.md b/docs/backend/forms.md index 33b02856..038a11ba 100644 --- a/docs/backend/forms.md +++ b/docs/backend/forms.md @@ -31,7 +31,7 @@ On a create form Save follows `redirect` and Save and close follows `redirectClo An optional `recordActions` key lists the record actions the form offers, by the names the controller registers through `pact.HasAdminRecordActions`. When the form has a `preview` block, those actions appear on the [preview screen](#preview-screen). When it has no preview, the update form footer shows them after Delete (outline buttons; a `danger` action uses the danger confirm). The show response of a record carries `meta.actions`: the declared actions the administrator may run and that apply to the record in its current state. See [Record actions](admin-controllers.md#record-actions). -`meta.deletable` on the show response hides Delete when it is `false`. When the key is absent, Delete behaves as before (`true`), so existing controllers are unchanged. After every successful save and record action, `type: partial` fields reload so status hints and diffs stay current. +`meta.deletable` on the show response hides Delete when it is `false`. When the key is absent, Delete behaves as before (`true`), so existing controllers are unchanged. `meta.localeVariants` on the show and update responses lists the locale versions of a record whose versions are separate rows (the mail templates screen, `cabana.RecordMeta.LocaleVariants`): each with its row `id`, `locale`, translated `label` and `states`, and `current` for the open record. With two or more, the update form shows a Language select under the title; choosing a version opens that record on the same route, behind the unsaved-changes confirm. Other controllers send no key. After every successful save and record action, `type: partial` fields reload so status hints and diffs stay current. ## fields.yaml diff --git a/docs/services/mail.md b/docs/services/mail.md index 5480a12b..02ab836a 100644 --- a/docs/services/mail.md +++ b/docs/services/mail.md @@ -139,7 +139,7 @@ fmt.Println(err != nil, len(driver.Messages())) // true 1 ``` -postcard does not pick a locale. For a per-language template, register one name per language (`acme.blog::mail.welcome_pl`) and pass the full name. +postcard does not pick a locale: the caller passes the full name. A per-language version of a template is a file named after the template, a dash and the locale (`welcome-en.htm`, sent as `acme.blog::mail.welcome-en`). A name with its own file always renders that file. A name the catalog does not hold, of the form `