feat(mail): one admin record per mail template with a locale switcher
Gap G-35-1 (supersedes D-05), tracer slice: - postcard: migration 202611130001_mail_template_base_code (base_code column and index); SyncIndex writes base_code; EnsureLocaleRows creates the <template>-<locale> rows of locales that ship no file; TemplateBase and Catalog.TemplateFor; send, preview and validation resolve a missing <template>-<locale> to the locale's customization, else the base's customization, else the base file. Names with their own file send as before. - cabana: mail locales are the translator's plus the TranslationWriter's enabled locales; sync ensures the locale rows and re-syncs when the locale set changes; record lookups admit fallback-locale rows; the record title is the base code; RecordMeta.LocaleVariants (RecordLocaleVariant) on the show and update responses. - admin: LocaleVariantSwitcher in the update form header, from meta.localeVariants, behind the unsaved-changes confirm. - tests: TestMailOverrideSmoke/locale_fallback, TestMailScreensSmoke/locales, LocaleVariantSwitcher.test.ts.
This commit is contained in:
@@ -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"
|
||||
|
||||
14
admin/src/api/schema.d.ts
vendored
14
admin/src/api/schema.d.ts
vendored
@@ -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
|
||||
|
||||
68
admin/src/components/form/LocaleVariantSwitcher.vue
Normal file
68
admin/src/components/form/LocaleVariantSwitcher.vue
Normal file
@@ -0,0 +1,68 @@
|
||||
<script setup lang="ts">
|
||||
// The locale switcher of a record with locale versions (meta.localeVariants:
|
||||
// the mail templates screen). Each version is its own record id; choosing one
|
||||
// opens it on the same route, behind the form's unsaved-changes confirm. When
|
||||
// that navigation is refused the selection goes back to the open version.
|
||||
// Labels and state words come translated from the server.
|
||||
import { computed, ref, watch } from 'vue'
|
||||
import { isNavigationFailure, useRoute, useRouter } from 'vue-router'
|
||||
import type { RecordMeta } from '../../api/types'
|
||||
import { t } from '../../app/i18n'
|
||||
|
||||
type LocaleVariant = NonNullable<RecordMeta['localeVariants']>[number]
|
||||
|
||||
const props = defineProps<{ variants: LocaleVariant[]; controlId?: string }>()
|
||||
|
||||
const route = useRoute()
|
||||
const router = useRouter()
|
||||
|
||||
const current = computed(() => props.variants.find((variant) => variant.current)?.id ?? props.variants[0]?.id ?? 0)
|
||||
const selected = ref(current.value)
|
||||
watch(current, (id) => {
|
||||
selected.value = id
|
||||
})
|
||||
|
||||
const selectId = computed(() => props.controlId ?? 'locale-variant')
|
||||
|
||||
function optionText(variant: LocaleVariant): string {
|
||||
const states = variant.states ?? []
|
||||
return states.length > 0 ? `${variant.label} — ${states.join(', ')}` : variant.label
|
||||
}
|
||||
|
||||
async function onChange(event: Event): Promise<void> {
|
||||
const select = event.target as HTMLSelectElement
|
||||
const id = Number(select.value)
|
||||
if (!Number.isFinite(id) || id === current.value) {
|
||||
return
|
||||
}
|
||||
selected.value = id
|
||||
let failed = false
|
||||
try {
|
||||
const result = await router.push({ name: route.name ?? undefined, params: { ...route.params, id: String(id) }, query: route.query })
|
||||
failed = isNavigationFailure(result)
|
||||
} catch {
|
||||
failed = true
|
||||
}
|
||||
if (failed) {
|
||||
selected.value = current.value
|
||||
select.value = String(current.value)
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="flex flex-wrap items-center gap-2">
|
||||
<label :for="selectId" class="text-[13px] text-muted">{{ t('backend::lang.form.locale_variant') }}</label>
|
||||
<select
|
||||
:id="selectId"
|
||||
data-locale-variant
|
||||
class="rounded-control border border-border-strong bg-surface px-2 py-1 text-[13px]"
|
||||
:value="String(selected)"
|
||||
@change="onChange"
|
||||
>
|
||||
<option v-for="variant in variants" :key="variant.id" :value="String(variant.id)" :data-locale="variant.locale">
|
||||
{{ optionText(variant) }}
|
||||
</option>
|
||||
</select>
|
||||
</div>
|
||||
</template>
|
||||
@@ -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<RecordAction[]>([])
|
||||
/** 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<NonNullable<RecordMeta['localeVariants']>>([])
|
||||
const acting = ref(false)
|
||||
const partialReload = ref(0)
|
||||
const errors = ref<Record<string, string[]>>({})
|
||||
@@ -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()
|
||||
<div class="flex min-w-0 flex-1 flex-col">
|
||||
<h1 class="truncate text-[24px] font-bold tracking-[-0.02em]">{{ loading ? '' : title }}</h1>
|
||||
<p v-if="subtitle" class="text-muted">{{ subtitle }}</p>
|
||||
<LocaleVariantSwitcher
|
||||
v-if="!loading && localeVariants.length > 1"
|
||||
class="mt-1.5"
|
||||
:variants="localeVariants"
|
||||
:control-id="`${ID_PREFIX}-locale-variant`"
|
||||
/>
|
||||
</div>
|
||||
<FormTabs v-if="tabs.length > 0" v-model="activeTab" :tabs="tabs" :id-prefix="ID_PREFIX" />
|
||||
</header>
|
||||
|
||||
@@ -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<HTMLSelectElement>('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',
|
||||
|
||||
75
admin/tests/form/LocaleVariantSwitcher.test.ts
Normal file
75
admin/tests/form/LocaleVariantSwitcher.test.ts
Normal file
@@ -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<HTMLSelectElement>('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<HTMLSelectElement>('select')
|
||||
await select.setValue('8')
|
||||
await flushPromises()
|
||||
expect(router.currentRoute.value.params.id).toBe('7')
|
||||
expect(select.element.value).toBe('7')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user