diff --git a/.changeset/slimy-knives-decide.md b/.changeset/slimy-knives-decide.md new file mode 100644 index 000000000..9149be0d7 --- /dev/null +++ b/.changeset/slimy-knives-decide.md @@ -0,0 +1,5 @@ +--- +'@shopify/ui-extensions': minor +--- + +Add discount function settings api diff --git a/packages/ui-extensions/src/surfaces/admin/api.ts b/packages/ui-extensions/src/surfaces/admin/api.ts index 995aeb523..0fca4d204 100644 --- a/packages/ui-extensions/src/surfaces/admin/api.ts +++ b/packages/ui-extensions/src/surfaces/admin/api.ts @@ -9,3 +9,4 @@ export type {ProductDetailsConfigurationApi} from './api/product-configuration/p export type {ProductVariantDetailsConfigurationApi} from './api/product-configuration/product-variant-details-configuration'; export type {OrderRoutingRuleApi} from './api/order-routing-rule/order-routing-rule'; export type {ValidationSettingsApi} from './api/checkout-rules/validation-settings'; +export type {DiscountFunctionSettingsApi} from './api/discount-function-settings/discount-function-settings'; diff --git a/packages/ui-extensions/src/surfaces/admin/api/checkout-rules/launch-options.ts b/packages/ui-extensions/src/surfaces/admin/api/checkout-rules/launch-options.ts index 13ceb98fb..700f72a92 100644 --- a/packages/ui-extensions/src/surfaces/admin/api/checkout-rules/launch-options.ts +++ b/packages/ui-extensions/src/surfaces/admin/api/checkout-rules/launch-options.ts @@ -6,7 +6,6 @@ interface Metafield { value: string; type: string; } - interface Validation { /** * the validation's gid when active in a shop diff --git a/packages/ui-extensions/src/surfaces/admin/api/checkout-rules/metafields.ts b/packages/ui-extensions/src/surfaces/admin/api/checkout-rules/metafields.ts index d96e6693c..a93e9be79 100644 --- a/packages/ui-extensions/src/surfaces/admin/api/checkout-rules/metafields.ts +++ b/packages/ui-extensions/src/surfaces/admin/api/checkout-rules/metafields.ts @@ -1,49 +1,4 @@ -const supportedDefinitionTypes = [ - 'boolean', - 'collection_reference', - 'color', - 'date', - 'date_time', - 'dimension', - 'file_reference', - 'json', - 'metaobject_reference', - 'mixed_reference', - 'money', - 'multi_line_text_field', - 'number_decimal', - 'number_integer', - 'page_reference', - 'product_reference', - 'rating', - 'rich_text_field', - 'single_line_text_field', - 'product_taxonomy_value_reference', - 'url', - 'variant_reference', - 'volume', - 'weight', - 'list.collection_reference', - 'list.color', - 'list.date', - 'list.date_time', - 'list.dimension', - 'list.file_reference', - 'list.metaobject_reference', - 'list.mixed_reference', - 'list.number_decimal', - 'list.number_integer', - 'list.page_reference', - 'list.product_reference', - 'list.rating', - 'list.single_line_text_field', - 'list.url', - 'list.variant_reference', - 'list.volume', - 'list.weight', -] as const; - -export type SupportedDefinitionType = (typeof supportedDefinitionTypes)[number]; +import {SupportedDefinitionType} from '../shared'; interface MetafieldUpdateChange { type: 'updateMetafield'; diff --git a/packages/ui-extensions/src/surfaces/admin/api/discount-function-settings/discount-function-settings.doc.ts b/packages/ui-extensions/src/surfaces/admin/api/discount-function-settings/discount-function-settings.doc.ts new file mode 100644 index 000000000..4c2c8ec44 --- /dev/null +++ b/packages/ui-extensions/src/surfaces/admin/api/discount-function-settings/discount-function-settings.doc.ts @@ -0,0 +1,27 @@ +import {ReferenceEntityTemplateSchema} from '@shopify/generate-docs'; + +const data: ReferenceEntityTemplateSchema = { + name: 'Discount Function Settings API', + description: + 'This API is available to Discount Function Settings extensions. Refer to the [tutorial](/docs/apps/build/discounts/build-ui-extension) for more information. Note that the [`FunctionSettings`](/docs/api/admin-extensions/components/forms/functionsettings) component is required to build Discount Function Settings extensions.', + isVisualComponent: false, + type: 'API', + definitions: [ + { + title: 'applyMetafieldChange', + description: 'Applies a change to the discount function settings.', + type: 'ApplyMetafieldChange', + }, + { + title: 'data', + description: + 'The object exposed to the extension that contains the discount function settings.', + type: 'DiscountFunctionSettingsData', + }, + ], + category: 'API', + subCategory: 'Target APIs', + related: [], +}; + +export default data; diff --git a/packages/ui-extensions/src/surfaces/admin/api/discount-function-settings/discount-function-settings.ts b/packages/ui-extensions/src/surfaces/admin/api/discount-function-settings/discount-function-settings.ts new file mode 100644 index 000000000..0df7f6e79 --- /dev/null +++ b/packages/ui-extensions/src/surfaces/admin/api/discount-function-settings/discount-function-settings.ts @@ -0,0 +1,15 @@ +import type {StandardApi} from '../standard/standard'; +import type {ExtensionTarget as AnyExtensionTarget} from '../../extension-targets'; + +import {ApplyMetafieldChange} from './metafields'; +import {DiscountFunctionSettingsData} from './launch-options'; + +export interface DiscountFunctionSettingsApi< + ExtensionTarget extends AnyExtensionTarget, +> extends StandardApi { + /** + * Applies a change to the discount function settings. + */ + applyMetafieldChange: ApplyMetafieldChange; + data: DiscountFunctionSettingsData; +} diff --git a/packages/ui-extensions/src/surfaces/admin/api/discount-function-settings/launch-options.ts b/packages/ui-extensions/src/surfaces/admin/api/discount-function-settings/launch-options.ts new file mode 100644 index 000000000..0a2806e2a --- /dev/null +++ b/packages/ui-extensions/src/surfaces/admin/api/discount-function-settings/launch-options.ts @@ -0,0 +1,29 @@ +interface Metafield { + description?: string; + id: string; + namespace: string; + key: string; + value: string; + type: string; +} + +export enum DiscountClass { + Product = 'PRODUCT', + Order = 'ORDER', + Shipping = 'SHIPPING', +} + +interface Discount { + /** + * the discount's gid + */ + id: string; +} + +/** + * The object that exposes the validation with its settings. + */ +export interface DiscountFunctionSettingsData { + id: Discount; + metafields: Metafield[]; +} diff --git a/packages/ui-extensions/src/surfaces/admin/api/discount-function-settings/metafields.ts b/packages/ui-extensions/src/surfaces/admin/api/discount-function-settings/metafields.ts new file mode 100644 index 000000000..a93e9be79 --- /dev/null +++ b/packages/ui-extensions/src/surfaces/admin/api/discount-function-settings/metafields.ts @@ -0,0 +1,31 @@ +import {SupportedDefinitionType} from '../shared'; + +interface MetafieldUpdateChange { + type: 'updateMetafield'; + key: string; + namespace?: string; + value: string | number; + valueType?: SupportedDefinitionType; +} + +interface MetafieldRemoveChange { + type: 'removeMetafield'; + key: string; + namespace: string; +} + +type MetafieldChange = MetafieldUpdateChange | MetafieldRemoveChange; +interface MetafieldChangeResultError { + type: 'error'; + message: string; +} +interface MetafieldChangeSuccess { + type: 'success'; +} +type MetafieldChangeResult = + | MetafieldChangeSuccess + | MetafieldChangeResultError; + +export type ApplyMetafieldChange = ( + change: MetafieldChange, +) => Promise; diff --git a/packages/ui-extensions/src/surfaces/admin/api/shared.ts b/packages/ui-extensions/src/surfaces/admin/api/shared.ts index 62de98989..b4c1a6d1c 100644 --- a/packages/ui-extensions/src/surfaces/admin/api/shared.ts +++ b/packages/ui-extensions/src/surfaces/admin/api/shared.ts @@ -4,3 +4,47 @@ export interface Data { */ selected: {id: string}[]; } + +export type SupportedDefinitionType = + | 'boolean' + | 'collection_reference' + | 'color' + | 'date' + | 'date_time' + | 'dimension' + | 'file_reference' + | 'json' + | 'metaobject_reference' + | 'mixed_reference' + | 'money' + | 'multi_line_text_field' + | 'number_decimal' + | 'number_integer' + | 'page_reference' + | 'product_reference' + | 'rating' + | 'rich_text_field' + | 'single_line_text_field' + | 'product_taxonomy_value_reference' + | 'url' + | 'variant_reference' + | 'volume' + | 'weight' + | 'list.collection_reference' + | 'list.color' + | 'list.date' + | 'list.date_time' + | 'list.dimension' + | 'list.file_reference' + | 'list.metaobject_reference' + | 'list.mixed_reference' + | 'list.number_decimal' + | 'list.number_integer' + | 'list.page_reference' + | 'list.product_reference' + | 'list.rating' + | 'list.single_line_text_field' + | 'list.url' + | 'list.variant_reference' + | 'list.volume' + | 'list.weight'; diff --git a/packages/ui-extensions/src/surfaces/admin/extension-targets.ts b/packages/ui-extensions/src/surfaces/admin/extension-targets.ts index fc4589851..594144185 100644 --- a/packages/ui-extensions/src/surfaces/admin/extension-targets.ts +++ b/packages/ui-extensions/src/surfaces/admin/extension-targets.ts @@ -11,6 +11,7 @@ import type { ProductVariantDetailsConfigurationApi, OrderRoutingRuleApi, ValidationSettingsApi, + DiscountFunctionSettingsApi, } from './api'; import {AnyComponentBuilder} from '../../shared'; import {PurchaseOptionsCardConfigurationApi} from './api/purchase-options-card-action/purchase-options-card-action'; @@ -100,7 +101,7 @@ export interface ExtensionTargets { * See the [list of available components](/docs/api/admin-extensions/components). */ 'admin.discount-details.function-settings.render': RenderExtension< - BlockExtensionApi<'admin.discount-details.function-settings.render'>, + DiscountFunctionSettingsApi<'admin.discount-details.function-settings.render'>, AllComponents >;