- Add MailPreviewField (POST /mail/preview + /mail/test-send, empty sandbox iframe) - Register mailpreview as valueless/group-labelled; SettingsFormView FORM_VALUES/FORM_LOCALE - Smoke tests for srcdoc, sequence guard, 422 keep-last, and 429 Retry-After Plan 35-03, task 1 Co-authored-by: Cursor <cursoragent@cursor.com>
269 lines
9.2 KiB
Vue
269 lines
9.2 KiB
Vue
<script setup lang="ts">
|
|
import { computed, nextTick, provide, ref } from 'vue'
|
|
import { useRoute, useRouter } from 'vue-router'
|
|
import { ArrowLeft } from '@lucide/vue'
|
|
import { api } from '../api/client'
|
|
import type { AdminRecord, FormField, FormView } from '../api/types'
|
|
import { shortcutHint } from '../app/hotkeys'
|
|
import { currentLocale, t } from '../app/i18n'
|
|
import FormErrorBanner from '../components/form/FormErrorBanner.vue'
|
|
import FormGrid from '../components/form/FormGrid.vue'
|
|
import FormTabs from '../components/form/FormTabs.vue'
|
|
import { FORM_LOCALE, FORM_VALUES, SETTINGS_SECRETS } from '../components/form/formContext'
|
|
import {
|
|
DEFAULT_TAB,
|
|
contextAllows,
|
|
editablePayload,
|
|
fieldErrors,
|
|
fieldHasErrors,
|
|
focusField,
|
|
noticeLines,
|
|
panelDomId,
|
|
tabDomId,
|
|
tabOf,
|
|
type TabItem,
|
|
} from '../components/form/formState'
|
|
import Button from '../components/ui/Button.vue'
|
|
import { settingsEntry } from '../state/useSettings'
|
|
import { showToast } from '../state/useToasts'
|
|
|
|
// One settings page (D-21) through the same FormGrid and field registry as
|
|
// records: GET the schema and the values, PUT the values keyed by field name,
|
|
// map a 422 the same way. Settings forms have no create step.
|
|
// Phase 18: fields with a `tab` render the FormTabs strip as on a record form
|
|
// (UI-SPEC F4); the write-only password fields carry their stored, env or
|
|
// empty state from meta.secrets (F2) and are sent only when the
|
|
// administrator typed a value (a string) or removed the stored one (null);
|
|
// richeditor save notices from meta.notices render under their field (F3).
|
|
// Save and Save and close answer Ctrl/Cmd+S and Ctrl/Cmd+Enter (app/hotkeys.ts).
|
|
const route = useRoute()
|
|
const router = useRouter()
|
|
const code = String(route.params.code ?? '')
|
|
const ID_PREFIX = 'setting'
|
|
|
|
const schema = ref<FormView | null>(null)
|
|
const values = ref<AdminRecord>({})
|
|
const errors = ref<Record<string, string[]>>({})
|
|
const notices = ref<Record<string, string[]>>({})
|
|
const secrets = ref<Record<string, string>>({})
|
|
const loading = ref(true)
|
|
const failed = ref(false)
|
|
const busy = ref(false)
|
|
const activeTab = ref(DEFAULT_TAB)
|
|
|
|
provide(SETTINGS_SECRETS, computed(() => secrets.value))
|
|
// Phase 35: mailpreview on Branding reads the unsaved draft (UI-SPEC C8).
|
|
provide(FORM_VALUES, computed(() => values.value))
|
|
provide(FORM_LOCALE, computed(() => schema.value?.meta.locale || currentLocale.value))
|
|
|
|
const fields = computed(() => (schema.value?.fields ?? []).filter((field) => contextAllows(field, 'update')))
|
|
const fieldNames = computed(() => fields.value.map((field) => field.name))
|
|
const title = computed(() => settingsEntry(code)?.label || schema.value?.name || code)
|
|
const description = computed(() => settingsEntry(code)?.description ?? '')
|
|
|
|
const tabs = computed<TabItem[]>(() => {
|
|
if (!fields.value.some((field) => field.tab)) {
|
|
return []
|
|
}
|
|
const out: TabItem[] = []
|
|
for (const field of fields.value) {
|
|
const key = tabOf(field)
|
|
let tab = out.find((item) => item.key === key)
|
|
if (!tab) {
|
|
tab = { key, label: field.tab || t('backend::lang.form.tab_default'), errors: 0 }
|
|
if (key === DEFAULT_TAB) {
|
|
out.unshift(tab)
|
|
} else {
|
|
out.push(tab)
|
|
}
|
|
}
|
|
if (fieldHasErrors(errors.value, field.name)) {
|
|
tab.errors += 1
|
|
}
|
|
}
|
|
return out
|
|
})
|
|
|
|
const activeIndex = computed(() => Math.max(tabs.value.findIndex((tab) => tab.key === activeTab.value), 0))
|
|
|
|
const panelFields = computed(() => {
|
|
if (tabs.value.length === 0) {
|
|
return fields.value
|
|
}
|
|
const key = tabs.value[activeIndex.value]?.key ?? DEFAULT_TAB
|
|
return fields.value.filter((field) => tabOf(field) === key)
|
|
})
|
|
|
|
async function load(): Promise<void> {
|
|
const params = { path: { code } }
|
|
const [schemaResult, valuesResult] = await Promise.all([
|
|
api.GET('/settings/{code}/schema', { params }),
|
|
api.GET('/settings/{code}', { params }),
|
|
])
|
|
schema.value = schemaResult.data?.data ?? null
|
|
values.value = { ...(valuesResult.data?.data.data ?? {}) }
|
|
secrets.value = { ...(valuesResult.data?.data.meta?.secrets ?? {}) }
|
|
failed.value = !schemaResult.data || !valuesResult.data
|
|
loading.value = false
|
|
activeTab.value = tabs.value[0]?.key ?? DEFAULT_TAB
|
|
}
|
|
|
|
function update(name: string, value: unknown): void {
|
|
values.value = { ...values.value, [name]: value }
|
|
if (errors.value[name]) {
|
|
const next = { ...errors.value }
|
|
delete next[name]
|
|
errors.value = next
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The PUT body: every editable value, except a password field that was not
|
|
* typed into (undefined or an empty string keeps the stored value, so it is
|
|
* left out) while a pending clear sends null.
|
|
*/
|
|
function settingsPayload(editable: FormField[], current: AdminRecord): AdminRecord {
|
|
const out = editablePayload(editable, current)
|
|
for (const field of editable) {
|
|
if (field.type !== 'password') {
|
|
continue
|
|
}
|
|
const value = current[field.name]
|
|
if (value === null) {
|
|
out[field.name] = null
|
|
} else if (typeof value !== 'string' || value === '') {
|
|
delete out[field.name]
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
/** Saves the values; true when the PUT succeeded. */
|
|
async function save(): Promise<boolean> {
|
|
if (busy.value || !schema.value) {
|
|
return false
|
|
}
|
|
busy.value = true
|
|
try {
|
|
const result = await api.PUT('/settings/{code}', {
|
|
params: { path: { code } },
|
|
body: settingsPayload(fields.value, values.value),
|
|
})
|
|
if (result.data) {
|
|
errors.value = {}
|
|
values.value = { ...result.data.data.data }
|
|
secrets.value = { ...(result.data.data.meta?.secrets ?? {}) }
|
|
notices.value = noticeLines(result.data.data.meta?.notices)
|
|
showToast(t('backend::lang.settings.saved'))
|
|
return true
|
|
}
|
|
if (result.response.status === 422) {
|
|
errors.value = fieldErrors(result.error?.error.details)
|
|
const invalid = fields.value.filter((field) => errors.value[field.name])
|
|
const first = invalid.find((field) => tabOf(field) === activeTab.value) ?? invalid[0]
|
|
if (first && tabs.value.length > 0) {
|
|
activeTab.value = tabOf(first)
|
|
}
|
|
await nextTick()
|
|
if (first) {
|
|
focusField(ID_PREFIX, first.name)
|
|
}
|
|
return false
|
|
}
|
|
showToast(result.error?.error.message || t('backend::lang.form.error_generic'), 'danger')
|
|
return false
|
|
} catch {
|
|
showToast(t('backend::lang.form.error_generic'), 'danger')
|
|
return false
|
|
} finally {
|
|
busy.value = false
|
|
}
|
|
}
|
|
|
|
async function onSave(): Promise<void> {
|
|
await save()
|
|
}
|
|
|
|
/** Zapisz i zamknij: saves, then returns to the settings index (the Cancel target). */
|
|
async function onSaveAndClose(): Promise<void> {
|
|
if (await save()) {
|
|
await router.push('/settings')
|
|
}
|
|
}
|
|
|
|
void load()
|
|
</script>
|
|
|
|
<template>
|
|
<section class="flex w-full flex-col gap-5 pb-24">
|
|
<header class="flex items-center gap-4">
|
|
<Button
|
|
variant="outline"
|
|
data-action="back"
|
|
to="/settings"
|
|
:aria-label="t('backend::lang.settings.back')"
|
|
class="size-10! px-0!"
|
|
>
|
|
<ArrowLeft :size="18" aria-hidden="true" />
|
|
</Button>
|
|
<div class="flex min-w-0 flex-1 flex-col">
|
|
<h1 class="truncate text-[24px] font-bold tracking-[-0.02em]">{{ title }}</h1>
|
|
<p v-if="description" class="text-muted">{{ description }}</p>
|
|
</div>
|
|
<FormTabs v-if="tabs.length > 0" v-model="activeTab" :tabs="tabs" :id-prefix="ID_PREFIX" />
|
|
</header>
|
|
|
|
<p v-if="failed && !loading" role="alert" class="rounded-inner bg-danger-soft px-[18px] py-3.5 text-danger">
|
|
{{ t('backend::lang.form.load_failed') }}
|
|
</p>
|
|
|
|
<template v-else-if="schema">
|
|
<FormErrorBanner :errors="errors" :field-names="fieldNames" />
|
|
<form
|
|
:id="tabs.length > 0 ? panelDomId(ID_PREFIX, activeIndex) : undefined"
|
|
:role="tabs.length > 0 ? 'tabpanel' : undefined"
|
|
:aria-labelledby="tabs.length > 0 ? tabDomId(ID_PREFIX, activeIndex) : undefined"
|
|
class="rounded-card border border-border bg-surface p-7 shadow-card"
|
|
novalidate
|
|
@submit.prevent="onSave"
|
|
>
|
|
<FormGrid
|
|
:fields="panelFields"
|
|
:values="values"
|
|
:errors="errors"
|
|
:notices="notices"
|
|
:source="null"
|
|
:id-prefix="ID_PREFIX"
|
|
@update="update"
|
|
/>
|
|
</form>
|
|
</template>
|
|
|
|
<footer class="fixed right-0 bottom-0 left-[var(--shell-offset,0px)] z-30 flex items-center justify-end gap-2.5 border-t border-border bg-surface px-8 py-3.5">
|
|
<!-- No Esc on Cancel: a settings form has no unsaved-changes question,
|
|
so Esc would discard edits silently. -->
|
|
<Button variant="ghost" to="/settings">{{ t('backend::lang.form.cancel') }}</Button>
|
|
<Button
|
|
variant="outline"
|
|
data-action="save-close"
|
|
hotkey="mod+enter"
|
|
:title="shortcutHint(t('backend::lang.form.save_and_close'), 'mod+enter')"
|
|
:disabled="busy || loading || !schema"
|
|
@click="onSaveAndClose"
|
|
>
|
|
{{ t('backend::lang.form.save_and_close') }}
|
|
</Button>
|
|
<Button
|
|
variant="primary"
|
|
data-action="save"
|
|
hotkey="mod+s"
|
|
:title="shortcutHint(t('backend::lang.form.save'), 'mod+s')"
|
|
:disabled="busy || loading || !schema"
|
|
@click="onSave"
|
|
>
|
|
{{ busy ? t('backend::lang.form.saving') : t('backend::lang.form.save') }}
|
|
</Button>
|
|
</footer>
|
|
</section>
|
|
</template>
|