Skip to content

Commit bf5f2f0

Browse files
Update API and add observables
1 parent ebfcf76 commit bf5f2f0

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ 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 {
6+
DiscountFunctionSettingsData,
7+
SubscribableDiscountFields,
8+
} from './launch-options';
69

710
export interface DiscountFunctionSettingsApi<
811
ExtensionTarget extends AnyExtensionTarget,
@@ -12,4 +15,5 @@ export interface DiscountFunctionSettingsApi<
1215
*/
1316
applyMetafieldChange: ApplyMetafieldChange;
1417
data: DiscountFunctionSettingsData;
18+
discounts: SubscribableDiscountFields;
1519
}

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

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,69 @@ interface Metafield {
77
type: string;
88
}
99

10-
export enum DiscountClass {
10+
enum DiscountClass {
1111
Product = 'PRODUCT',
1212
Order = 'ORDER',
1313
Shipping = 'SHIPPING',
1414
}
1515

16-
interface Discount {
17-
/**
18-
* the discount's gid
19-
*/
20-
id: string;
16+
enum DiscountMethod {
17+
Automatic = 'Automatic',
18+
Code = 'Code',
19+
}
20+
21+
enum PurchaseType {
22+
OneTimePurchase = 'ONE_TIME_PURCHASE',
23+
Subscription = 'SUBSCRIPTION',
24+
Both = 'BOTH',
25+
}
26+
27+
type Result<T> =
28+
| {success: true; value: T}
29+
| {success: false; error: ValidationError};
30+
31+
interface ValidationError {
32+
type: 'error';
33+
message: string;
34+
code: string;
35+
issues?: {
36+
message: string;
37+
path: string[];
38+
}[];
2139
}
2240

2341
/**
2442
* The object that exposes the validation with its settings.
2543
*/
2644
export interface DiscountFunctionSettingsData {
27-
id: Discount;
45+
id: string;
46+
discountClasses: DiscountClass[];
2847
metafields: Metafield[];
2948
}
49+
50+
/**
51+
* Represents a reactive signal interface that provides both immediate value access and subscription-based updates. Enables real-time synchronization with changing data through the observer pattern.
52+
*/
53+
export interface ReadonlySignalLike<T> {
54+
/**
55+
* The current value of the field.
56+
*/
57+
readonly value: T;
58+
/**
59+
* Subscribes to field changes and calls the provided function whenever the field updates. Returns an unsubscribe function to clean up the subscription.
60+
*/
61+
subscribe(fn: (value: T) => void): () => void;
62+
}
63+
64+
type UpdateFunction<T> = (value: T) => Result<T>;
65+
66+
export interface SubscribableDiscountFields {
67+
discountClasses: ReadonlySignalLike<DiscountClass[]>;
68+
updateDiscountClasses: UpdateFunction<DiscountClass[]>;
69+
method: ReadonlySignalLike<DiscountMethod>;
70+
updateMethod: UpdateFunction<DiscountMethod>;
71+
purchaseType: ReadonlySignalLike<PurchaseType>;
72+
updatePurchaseType: UpdateFunction<PurchaseType>;
73+
recurringCycleLimit: ReadonlySignalLike<number | null | undefined>;
74+
updateRecurringCycleLimit: UpdateFunction<number | null | undefined>;
75+
}

0 commit comments

Comments
 (0)