feat(19.7-01): admin SPA account contributions and prompted record actions
- AccountContributions.vue on My account: one card per contributed section
with status and outline buttons; confirm or danger actions go through the
shared ConfirmDialog; a returned URL opens in a new tab (noopener) and stays
as a visible link; 409 reloads with the stale toast
- ConfirmRequest.prompt and useConfirm value; ConfirmDialog renders one
labelled text input (maxlength, autocomplete off, aria-describedby comment,
autofocus), confirm disabled until a value is typed, Enter confirms
- RecordActions posts {payload} for a prompted action and {} otherwise; a 422
on payload shows the server's detail as a danger toast
- types.ts exports RecordActionPrompt, AccountContribution,
AccountContributionAction, AccountActionResult
- vitest smokes AccountContributions.test.ts and RecordActionsPrompt.test.ts
This commit is contained in:
@@ -16,6 +16,8 @@ import { useConfirm } from '../ui/confirm'
|
||||
// disabled meanwhile. Labels, confirm texts and messages come from the server
|
||||
// and are rendered as text. A `danger` action (UI-SPEC F5) turns the dialog's
|
||||
// confirm button into the danger variant; the footer button stays outline.
|
||||
// An action with a `prompt` asks for one text value in the dialog and posts
|
||||
// it as the body key `payload`; every other action posts an empty body.
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
source: ControllerParams
|
||||
@@ -39,6 +41,16 @@ const emit = defineEmits<{
|
||||
const confirm = useConfirm()
|
||||
const running = ref(false)
|
||||
|
||||
/** The server's first message on the payload field of a 422, if any. */
|
||||
function payloadDetail(error: unknown): string {
|
||||
const details = (error as { error?: { details?: Record<string, unknown> } } | undefined)?.error?.details
|
||||
const payload = details?.payload
|
||||
if (Array.isArray(payload) && typeof payload[0] === 'string') {
|
||||
return payload[0]
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
async function run(action: RecordAction): Promise<void> {
|
||||
if (running.value || props.disabled) {
|
||||
return
|
||||
@@ -46,23 +58,41 @@ async function run(action: RecordAction): Promise<void> {
|
||||
let status = 0
|
||||
let ok = false
|
||||
let text = ''
|
||||
let detail = ''
|
||||
const confirmed = await confirm.ask(
|
||||
{
|
||||
message: action.confirm || t('backend::lang.form.action_confirm', { action: action.label }),
|
||||
confirmLabel: action.label,
|
||||
danger: action.danger,
|
||||
prompt: action.prompt
|
||||
? {
|
||||
label: action.prompt.label,
|
||||
placeholder: action.prompt.placeholder,
|
||||
comment: action.prompt.comment,
|
||||
maxLength: action.prompt.max_length,
|
||||
}
|
||||
: undefined,
|
||||
},
|
||||
async () => {
|
||||
running.value = true
|
||||
emit('busy', true)
|
||||
try {
|
||||
const result = await api.POST('/{vendor}/{plugin}/{controller}/{id}/actions/{action}', {
|
||||
params: { path: { ...props.source, id: props.recordId, action: action.name } },
|
||||
body: {},
|
||||
})
|
||||
const params = { path: { ...props.source, id: props.recordId, action: action.name } }
|
||||
// A prompted action posts its value as `payload`; every other action
|
||||
// keeps the empty body the record action route expects.
|
||||
const result = action.prompt
|
||||
? await api.POST('/{vendor}/{plugin}/{controller}/{id}/actions/{action}', {
|
||||
params,
|
||||
body: { payload: confirm.value.value.trim() },
|
||||
})
|
||||
: await api.POST('/{vendor}/{plugin}/{controller}/{id}/actions/{action}', {
|
||||
params,
|
||||
body: {},
|
||||
})
|
||||
status = result.response.status
|
||||
ok = !!result.data
|
||||
text = result.data?.data.message ?? result.error?.error.message ?? ''
|
||||
detail = payloadDetail(result.error)
|
||||
} catch {
|
||||
status = 0
|
||||
} finally {
|
||||
@@ -91,6 +121,10 @@ async function run(action: RecordAction): Promise<void> {
|
||||
showToast(text || t('backend::lang.list.action_forbidden'), 'danger')
|
||||
return
|
||||
}
|
||||
if (status === 422 && detail) {
|
||||
showToast(detail, 'danger')
|
||||
return
|
||||
}
|
||||
showToast(text || t('backend::lang.extension.action_failed'), 'danger')
|
||||
}
|
||||
</script>
|
||||
@@ -114,6 +148,9 @@ async function run(action: RecordAction): Promise<void> {
|
||||
:confirm-label="confirm.request.value?.confirmLabel"
|
||||
:danger="confirm.request.value?.danger"
|
||||
:busy="confirm.busy.value"
|
||||
:prompt="confirm.request.value?.prompt ?? null"
|
||||
:value="confirm.value.value"
|
||||
@update:value="confirm.setValue"
|
||||
@confirm="confirm.confirm"
|
||||
@cancel="confirm.cancel"
|
||||
/>
|
||||
|
||||
Reference in New Issue
Block a user