Skip to content

Commit 9475b2d

Browse files
Merge pull request #3641 from Shopify/jh_discount-function-settings-observables
Update API and add observables for discount function settings
2 parents 49f99e5 + 9440bde commit 9475b2d

File tree

5 files changed

+97
-12
lines changed

5 files changed

+97
-12
lines changed

.changeset/wet-cycles-punch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@shopify/ui-extensions': minor
3+
---
4+
5+
Added subscribable discounts api. Update the type for `data.id` to string to fix a previously incorrect type.

packages/ui-extensions/src/shared.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,3 +952,38 @@ export interface ReadonlySignalLike<T> {
952952
*/
953953
subscribe(fn: (value: T) => void): () => void;
954954
}
955+
956+
/**
957+
* A result type that indicates the success or failure of an operation.
958+
*/
959+
type Result<T> =
960+
| {success: true; value: T}
961+
| {success: false; errors: ValidationError[]};
962+
963+
/**
964+
* A validation error object that is returned when an operation fails.
965+
*/
966+
interface ValidationError {
967+
type: 'error';
968+
/**
969+
* A message describing the error.
970+
*/
971+
message: string;
972+
/**
973+
* A code identifier for the error.
974+
*/
975+
code: string;
976+
/**
977+
* Field-level validation issues
978+
*/
979+
issues?: {
980+
message: string;
981+
path: string[];
982+
}[];
983+
}
984+
985+
/**
986+
* A function that updates a signal and returns a result indicating success or failure.
987+
* The function is typically used alongisde a ReadonlySignalLike object.
988+
*/
989+
export type UpdateSignalFunction<T> = (value: T) => Result<T>;

packages/ui-extensions/src/surfaces/admin/api/discount-function-settings/discount-function-settings.doc.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ const data: ReferenceEntityTemplateSchema = {
1818
'The object exposed to the extension that contains the discount function settings.',
1919
type: 'DiscountFunctionSettingsData',
2020
},
21+
{
22+
title: 'discounts',
23+
description:
24+
'The reactive API for managing discount function configuration.',
25+
type: 'DiscountsApi',
26+
},
2127
],
2228
category: 'API',
2329
subCategory: 'Target APIs',

packages/ui-extensions/src/surfaces/admin/api/discount-function-settings/discount-function-settings.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type {BlockExtensionApi} from '../block/block';
22
import type {ExtensionTarget as AnyExtensionTarget} from '../../extension-targets';
33

44
import {ApplyMetafieldChange} from './metafields';
5-
import {DiscountFunctionSettingsData} from './launch-options';
5+
import {DiscountFunctionSettingsData, DiscountsApi} from './launch-options';
66

77
export interface DiscountFunctionSettingsApi<
88
ExtensionTarget extends AnyExtensionTarget,
@@ -12,4 +12,5 @@ export interface DiscountFunctionSettingsApi<
1212
*/
1313
applyMetafieldChange: ApplyMetafieldChange;
1414
data: DiscountFunctionSettingsData;
15+
discounts: DiscountsApi;
1516
}
Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import type {
2+
ReadonlySignalLike,
3+
UpdateSignalFunction,
4+
} from '../../../../shared';
5+
16
interface Metafield {
27
description?: string;
38
id: string;
@@ -7,23 +12,56 @@ interface Metafield {
712
type: string;
813
}
914

10-
export enum DiscountClass {
11-
Product = 'PRODUCT',
12-
Order = 'ORDER',
13-
Shipping = 'SHIPPING',
14-
}
15+
type DiscountClass = 'product' | 'order' | 'shipping';
16+
17+
type DiscountMethod = 'automatic' | 'code';
18+
19+
type PurchaseType = 'one_time_purchase' | 'subscription' | 'both';
1520

16-
interface Discount {
21+
/**
22+
* The object that exposes the validation with its settings.
23+
*/
24+
export interface DiscountFunctionSettingsData {
1725
/**
18-
* the discount's gid
26+
* The unique identifier for the discount.
1927
*/
2028
id: string;
29+
/**
30+
* The discount metafields.
31+
*/
32+
metafields: Metafield[];
2133
}
2234

2335
/**
24-
* The object that exposes the validation with its settings.
36+
* Reactive Api for managing discount function configuration.
2537
*/
26-
export interface DiscountFunctionSettingsData {
27-
id: Discount;
28-
metafields: Metafield[];
38+
export interface DiscountsApi {
39+
/**
40+
* A signal that contains the discount classes.
41+
*/
42+
discountClasses: ReadonlySignalLike<DiscountClass[]>;
43+
/**
44+
* A function that updates the discount classes.
45+
*/
46+
updateDiscountClasses: UpdateSignalFunction<DiscountClass[]>;
47+
/**
48+
* A signal that contains the discount method.
49+
*/
50+
discountMethod: ReadonlySignalLike<DiscountMethod>;
51+
/**
52+
* A signal that contains the purchase type.
53+
*/
54+
purchaseType: ReadonlySignalLike<PurchaseType>;
55+
/**
56+
* A function that updates the purchase type.
57+
*/
58+
updatePurchaseType: UpdateSignalFunction<PurchaseType>;
59+
/**
60+
* A signal that contains the recurring cycle limit for subscriptions purchases.
61+
*/
62+
recurringCycleLimit: ReadonlySignalLike<number | null | undefined>;
63+
/**
64+
* A function that updates the recurring cycle limit for subscriptions purchases.
65+
*/
66+
updateRecurringCycleLimit: UpdateSignalFunction<number | null | undefined>;
2967
}

0 commit comments

Comments
 (0)