Skip to content

Commit

Permalink
Merge pull request #5013 from Shopify/update_mapping_from_context_to_…
Browse files Browse the repository at this point in the history
…targets

Update context to target transform to match link targets
  • Loading branch information
alfonso-noriega authored Dec 4, 2024
2 parents 084f555 + 2fa6126 commit c15413e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ handle = "admin-link-title"
[[extensions.targeting]]
text = "admin link label"
url = "https://google.es"
target = "admin.collection.item.link"
target = "admin.collection-details.action.link"
`)
})

Expand Down Expand Up @@ -72,7 +72,7 @@ handle = "bulk-action-title"
[[extensions.targeting]]
text = "bulk action label"
url = "app://action/product?product_id=123#hash"
target = "admin.product.selection.link"
target = "admin.product-index.selection-action.link"
`)
})
test('correctly builds a toml string for bulk_action extension with no path in an embedded app', () => {
Expand Down Expand Up @@ -107,7 +107,7 @@ handle = "bulk-action-title"
[[extensions.targeting]]
text = "bulk action label"
url = "app://"
target = "admin.product.selection.link"
target = "admin.product-index.selection-action.link"
`)
})
test('correctly builds a toml string for bulk_action extension with no path but search query in an embedded app', () => {
Expand Down Expand Up @@ -142,7 +142,7 @@ handle = "bulk-action-title"
[[extensions.targeting]]
text = "bulk action label"
url = "app://?foo=bar"
target = "admin.product.selection.link"
target = "admin.product-index.selection-action.link"
`)
})
})
14 changes: 12 additions & 2 deletions packages/app/src/cli/services/admin-link/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('admin link utils', () => {
const target = contextToTarget(context)

// Then
expect(target).toEqual('admin.collection.item.link')
expect(target).toEqual('admin.collection-details.action.link')
})
test('correctly parses from context `ORDERS#INDEX` to target', () => {
// Given
Expand All @@ -20,6 +20,16 @@ describe('admin link utils', () => {
const target = contextToTarget(context)

// Then
expect(target).toEqual('admin.order.index.link')
expect(target).toEqual('admin.order-index.action.link')
})
test('correctly parses from context `CUSTOMERS#ACTION` to target', () => {
// Given
const context = 'CUSTOMERS#ACTION'

// When
const target = contextToTarget(context)

// Then
expect(target).toEqual('admin.customer-index.selection-action.link')
})
})
23 changes: 16 additions & 7 deletions packages/app/src/cli/services/admin-link/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,36 @@ export const contextToTarget = (context: string) => {
throw new Error('Invalid context')
}
const domain = 'admin'
const subDomain = typeToSubDomain(splitContext[0] || '')
const entity = locationToEntity(splitContext[1] || '')
const subDomain = typeToSubDomain(splitContext[0] ?? '')
const entity = locationToEntity(splitContext[1] ?? '')
const action = 'link'

return [domain, subDomain, entity, action].join('.')
if (entity === 'selection') {
return [domain, `${subDomain}-index`, `${entity}-action`, action].join('.')
} else {
return [domain, `${subDomain}-${entity}`, 'action', action].join('.')
}
}

const locationToEntity = (location: string) => {
switch (location.toLocaleLowerCase()) {
case 'show':
return 'item'
return 'details'
case 'index':
return 'index'
case 'action':
return 'selection'
case 'fulfilled_card':
return 'fulfilled_card'
return 'fulfilled-card'
default:
throw new Error(`Invalid context location: ${location}`)
}
}
const typeToSubDomain = (word: string) => {
return word.toLocaleLowerCase().replace(new RegExp(`(s)$`), '')
const typeToSubDomain = (type: string) => {
switch (type.toLocaleLowerCase()) {
case 'variants':
return 'product-variant'
default:
return type.toLocaleLowerCase().replace(new RegExp(`(s)$`), '')
}
}

0 comments on commit c15413e

Please sign in to comment.