Skip to content

Commit 0d85224

Browse files
Update API and add observables
1 parent 49f99e5 commit 0d85224

File tree

4 files changed

+46
-13
lines changed

4 files changed

+46
-13
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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,3 +952,19 @@ export interface ReadonlySignalLike<T> {
952952
*/
953953
subscribe(fn: (value: T) => void): () => void;
954954
}
955+
956+
type Result<T> =
957+
| {success: true; value: T}
958+
| {success: false; errors: ValidationError[]};
959+
960+
interface ValidationError {
961+
type: 'error';
962+
message: string;
963+
code: string;
964+
issues?: {
965+
message: string;
966+
path: string[];
967+
}[];
968+
}
969+
970+
export type UpdateSignalFunction<T> = (value: T) => Result<T>;

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
}
Lines changed: 20 additions & 12 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,26 @@ 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';
1516

16-
interface Discount {
17-
/**
18-
* the discount's gid
19-
*/
20-
id: string;
21-
}
17+
type DiscountMethod = 'automatic' | 'code';
18+
19+
type PurchaseType = 'one_time_purchase' | 'subscription' | 'both';
2220

2321
/**
2422
* The object that exposes the validation with its settings.
2523
*/
2624
export interface DiscountFunctionSettingsData {
27-
id: Discount;
25+
id: string;
2826
metafields: Metafield[];
2927
}
28+
29+
export interface SubscribableDiscountFields {
30+
discountClasses: ReadonlySignalLike<DiscountClass[]>;
31+
updateDiscountClasses: UpdateSignalFunction<DiscountClass[]>;
32+
discountMethod: ReadonlySignalLike<DiscountMethod>;
33+
purchaseType: ReadonlySignalLike<PurchaseType>;
34+
updatePurchaseType: UpdateSignalFunction<PurchaseType>;
35+
recurringCycleLimit: ReadonlySignalLike<number | null | undefined>;
36+
updateRecurringCycleLimit: UpdateSignalFunction<number | null | undefined>;
37+
}

0 commit comments

Comments
 (0)