Skip to content

Commit

Permalink
Add defaults to variants, make openlink variants an option and open …
Browse files Browse the repository at this point in the history
…record in create mode for missing recordIDs
  • Loading branch information
KinyaElGrande committed Mar 15, 2024
1 parent 5b7fde4 commit 0a9bb6a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ export default {
target: param.name,
type: arg.type || this.paramTypes[func.ref][param.name][0],
valueType: this.getValueType(arg, arg.type || this.paramTypes[func.ref][param.name][0], input),
value: arg.value || null,
value: arg.value || input.default || null,
expr: arg.expr || arg.source || null,
required: param.required || false,
input,
Expand Down
22 changes: 14 additions & 8 deletions lib/vue/src/components/prompts/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ const variants = [
{ value: 'dark', text: 'Dark' },
]

const linkClickVariants = [
{ value: 'newTab', text: 'Open link in a new tab' },
{ value: 'sameTab', text: 'Open link in the same tab' },
{ value: 'modal', text: 'Open in a modal' },
]

export const prompts = Object.freeze([
{
ref: 'redirect',
Expand All @@ -19,7 +25,7 @@ export const prompts = Object.freeze([
{ name: 'owner', types: ['User', 'ID'], required: false },
{ name: 'url', types: ['String'], required: true },
{ name: 'delay', types: ['Integer'], meta: { description: 'Redirection delay in seconds' } },
{ name: 'openInNewTab', types: ['Boolean'] }
{ name: 'openLink', types: ['String'], meta: { visual: { input: { type: 'select', properties: { options: linkClickVariants }, default: 'sameTab' } } } },
],
},
{
Expand All @@ -31,7 +37,7 @@ export const prompts = Object.freeze([
{ name: 'params', types: ['KV'] },
{ name: 'query', types: ['KV'] },
{ name: 'delay', types: ['Integer'], meta: { description: 'Redirection delay in seconds' } },
{ name: 'openInNewTab', types: ['Boolean'] }
{ name: 'openLink', types: ['String'], meta: { visual: { input: { type: 'select', properties: { options: linkClickVariants }, default: 'sameTab' } } } },
],
},
{
Expand All @@ -47,7 +53,7 @@ export const prompts = Object.freeze([
{ name: 'record', types: ['ID', 'ComposeRecord'] },
{ name: 'edit', types: ['Boolean'] },
{ name: 'delay', types: ['Integer'], meta: { description: 'Redirection delay in seconds' } },
{ name: 'openInNewTab', types: ['Boolean'] }
{ name: 'openLink', types: ['String'], meta: { visual: { input: { type: 'select', properties: { options: linkClickVariants }, default: 'sameTab' } } } },
],
},
{
Expand All @@ -61,7 +67,7 @@ export const prompts = Object.freeze([
{ name: 'owner', types: ['User', 'ID'], required: false },
{ name: 'title', types: ['String'] },
{ name: 'message', types: ['String'], required: true },
{ name: 'variant', types: ['String'], meta: { visual: { input: { type: 'select', properties: { options: variants } } } } },
{ name: 'variant', types: ['String'], meta: { visual: { input: { type: 'select', properties: { options: variants }, default: 'primary' } } } },
{ name: 'timeout', types: ['Integer'], meta: { description: 'How long do we show the notification in seconds' } },
],
},
Expand All @@ -73,7 +79,7 @@ export const prompts = Object.freeze([
{ name: 'title', types: ['String'] },
{ name: 'message', types: ['String'], required: true },
{ name: 'buttonLabel', types: ['String'] },
{ name: 'buttonVariant', types: ['String'], meta: { visual: { input: { type: 'select', properties: { options: variants } } } } },
{ name: 'buttonVariant', types: ['String'], meta: { visual: { input: { type: 'select', properties: { options: variants }, default: 'primary' } } } },
{ name: 'buttonValue', types: ['Any'] },
],
},
Expand All @@ -85,10 +91,10 @@ export const prompts = Object.freeze([
{ name: 'title', types: ['String'] },
{ name: 'message', types: ['String'], required: true },
{ name: 'confirmButtonLabel', types: ['String'] },
{ name: 'confirmButtonVariant', types: ['String'], meta: { visual: { input: { type: 'select', properties: { options: variants } } } } },
{ name: 'confirmButtonVariant', types: ['String'], meta: { visual: { input: { type: 'select', properties: { options: variants }, default: 'primary' } } } },
{ name: 'confirmButtonValue', types: ['Any'] },
{ name: 'rejectButtonLabel', types: ['String'] },
{ name: 'rejectButtonVariant', types: ['String'], meta: { visual: { input: { type: 'select', properties: { options: variants } } } } },
{ name: 'rejectButtonVariant', types: ['String'], meta: { visual: { input: { type: 'select', properties: { options: variants }, default: 'danger' } } } },
{ name: 'rejectButtonValue', types: ['Any'] },
],
results: [
Expand Down Expand Up @@ -150,7 +156,7 @@ export const prompts = Object.freeze([
parameters: [
{ name: 'owner', types: ['User', 'ID'], required: false },
{ name: 'title', types: ['String'] },
{ name: 'variant', types: ['String'], meta: { visual: { input: { type: 'select', properties: { options: variants } } } } },
{ name: 'variant', types: ['String'], meta: { visual: { input: { type: 'select', properties: { options: variants }, default: 'primary' } } } },
{ name: 'message', types: ['String'], required: true },
{ name: 'label', types: ['String'] },
{
Expand Down
49 changes: 29 additions & 20 deletions lib/vue/src/components/prompts/kinds/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,18 @@ const definitions: Record<string, PromptDefinition> = {
handler: function (v): void {
const url = pVal(v, 'url')
const delay = (pVal(v, 'delay') || 0) as number
const openInNewTab = pVal(v, 'openInNewTab')
const openLink = pVal(v, 'openLink')

if (url !== undefined) {
console.debug('redirect to %s via prompt in %d sec', url, delay)
setTimeout(() => {
if (openInNewTab) {
if (openLink === 'newTab') {
// @ts-ignore
window.open(url, '_blank')
} else {
// @ts-ignore
window.location = url
}

// @ts-ignore
window.location = url
}, delay * 1000)
}
},
Expand All @@ -83,20 +84,20 @@ const definitions: Record<string, PromptDefinition> = {
const params = pVal(v, 'params')
const query = pVal(v, 'query')
const delay = (pVal(v, 'delay') || 0) as number
const openInNewTab = pVal(v, 'openInNewTab')
const openLink = pVal(v, 'openLink')
if (name !== undefined) {
console.debug('reroute to %s via prompt in %d sec', name, delay, { params, query })
setTimeout(() => {
const routeParams = { name, params, query }
if (openInNewTab) {
if (openLink === 'newTab') {
// @ts-ignore
const url = this.$router.resolve(routeParams).href
// @ts-ignore
window.open(url, '_blank')
} else {
// @ts-ignore
this.$router.push(routeParams)
}

// @ts-ignore
this.$router.push(routeParams)
}, delay * 1000)
}
},
Expand All @@ -109,7 +110,7 @@ const definitions: Record<string, PromptDefinition> = {
const record = pVal(v, 'record')
const edit = !!pVal(v, 'edit')
const delay = (pVal(v, 'delay') || 0) as number
const openInNewTab = pVal(v, 'openInNewTab')
const openLink = pVal(v, 'openLink')

let namespaceID = ''
let slug = ''
Expand Down Expand Up @@ -138,7 +139,9 @@ const definitions: Record<string, PromptDefinition> = {
// @ts-ignore
const { set: nn } = await this.$ComposeAPI.namespaceList({ slug: namespace as string })
if (!nn || nn.length !== 1) {
throw new Error('namespace not resolved')
// @ts-ignore
this.toastDanger('namespace not resolved', 'prompt error')
return
}

namespaceID = nn[0].namespaceID
Expand All @@ -154,7 +157,9 @@ const definitions: Record<string, PromptDefinition> = {
// @ts-ignore
const { set: mm } = await this.$ComposeAPI.moduleList({ handle: module as string, namespaceID })
if (!mm || mm.length !== 1) {
throw new Error('module not resolved')
// @ts-ignore
this.toastDanger('module not resolved', 'prompt error')
return
}

moduleID = mm[0].moduleID
Expand All @@ -171,18 +176,16 @@ const definitions: Record<string, PromptDefinition> = {
// @ts-ignore
const { set: pp } = await this.$ComposeAPI.pageList({ moduleID, namespaceID })
if (!pp || pp.length !== 1) {
throw new Error('record page not resolved')
// @ts-ignore
this.toastDanger('record page not resolved', 'prompt error')
return
}
pageID = pp[0].pageID

// @ts-ignore
if (this.$root.$options.name === 'compose') {
if (!edit && !recordID) {
throw new Error('invalid record page prompt configuration')
}

let name = 'page.record'
if (edit) {
if (edit || recordID === '') {
name += recordID ? '.edit' : '.create'
}

Expand All @@ -196,7 +199,13 @@ const definitions: Record<string, PromptDefinition> = {
window.location.reload()
} else {
const routeParams = { name, params: { recordID, pageID, slug } }
if (openInNewTab) {
if (openLink === 'modal') {
// @ts-ignore
this.$root.$emit('show-record-modal', {
recordID,
recordPageID: pageID,
})
} else if (openLink === 'newTab') {
// @ts-ignore
const url = this.$router.resolve(routeParams).href
// @ts-ignore
Expand Down

0 comments on commit 0a9bb6a

Please sign in to comment.