Skip to content

Commit c15146a

Browse files
authored
Merge pull request #3080 from Shopify/vic/editable-cart-api-backfill
Backfill editable changes for CartApi
2 parents cd29ed5 + dd22acc commit c15146a

File tree

7 files changed

+65
-65
lines changed

7 files changed

+65
-65
lines changed

.changeset/gold-balloons-tan.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@shopify/ui-extensions': patch
3+
---
4+
5+
Remove CartNotEditableError for POS CartApi

packages/ui-extensions/docs/surfaces/point-of-sale/reference/apis/cart-api.doc.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ The Cart API enables UI Extensions to manage and interact with POS cart contents
4545
'subscribable',
4646
),
4747
},
48+
{
49+
codeblock: generateCodeBlockForCartApi(
50+
'Check editable state of the cart',
51+
'check-cart-editable',
52+
),
53+
},
4854
{
4955
codeblock: generateCodeBlockForCartApi(
5056
'Apply a cart level discount',
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {Cart, Tile, extension} from '@shopify/ui-extensions/point-of-sale';
2+
3+
export default extension('pos.home.tile.render', (root, api) => {
4+
const tile = root.createComponent(Tile, {
5+
title: 'My App',
6+
enabled: api.cart.subscribable.initial.editable ?? true,
7+
});
8+
9+
api.cart.subscribable.subscribe((newCart: Cart) => {
10+
tile.updateProps({
11+
enabled: newCart.editable ?? true
12+
});
13+
});
14+
15+
root.append(tile);
16+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React from 'react';
2+
import {
3+
reactExtension,
4+
Tile,
5+
useApi,
6+
useCartEditable,
7+
} from '@shopify/ui-extensions-react/point-of-sale';
8+
9+
const SmartGridTile = () => {
10+
const editable = useCartEditable();
11+
12+
return <Tile title="My App" enabled={editable} />;
13+
};
14+
15+
export default reactExtension('pos.home.tile.render', () => <SmartGridTile />);

packages/ui-extensions/docs/surfaces/point-of-sale/staticPages/pages/versions.doc.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ const data: LandingTemplateSchema = {
4545
- Added optional \`taxLines\` property to [ShippingLine](/docs/api/pos-ui-extensions/targets/post-transaction/pos-transaction-complete-event-observe#transactioncompletedata-propertydetail-transaction) interface.
4646
- Added optional \`onBlur\` handler to [SearchBar](/docs/api/pos-ui-extensions/components/searchbar) component.
4747
- Added optional \`tone\` property to [Icon](/docs/api/pos-ui-extensions/components/icon) component and expanded \`name\` and \`size\` options.
48+
- Added optional \`editable\` property to \`Cart\` interface.
49+
- Added \`useCartEditable\` hook to access the cart's editable state.
4850
4951
### Developer Preview
5052

packages/ui-extensions/src/surfaces/point-of-sale/render/api/cart-api/cart-api.ts

Lines changed: 21 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,17 @@ export interface CartApiContent {
3030
subscribable: RemoteSubscribable<Cart>;
3131

3232
/** Bulk update the cart
33+
*
3334
* @param cartState the cart state to set
3435
* @returns the updated cart
35-
*
36-
* @throws CartNotEditableError if the cart is not currently editable.
3736
*/
3837
bulkCartUpdate(cartState: CartUpdateInput): Promise<Cart>;
3938

4039
/** Apply a cart level discount
40+
*
4141
* @param type the type of discount applied (example: 'Percentage')
4242
* @param title the title attributed with the discount
4343
* @param amount the percentage or fixed monetary amount deducted with the discount. Pass in `undefined` if using discount codes.
44-
*
45-
* @throws CartNotEditableError if the cart is not currently editable.
4644
*/
4745
applyCartDiscount(
4846
type: CartDiscountType,
@@ -52,98 +50,83 @@ export interface CartApiContent {
5250

5351
/**
5452
* Add a code discount to the cart
55-
* @param code the code for the discount to add to the cart
5653
*
57-
* @throws CartNotEditableError if the cart is not currently editable.
54+
* @param code the code for the discount to add to the cart
5855
*/
5956
addCartCodeDiscount(code: string): Promise<void>;
6057

6158
/**
6259
* Remove the cart discount
63-
*
64-
* @throws CartNotEditableError if the cart is not currently editable.
6560
*/
6661
removeCartDiscount(): Promise<void>;
6762

6863
/**
6964
* Remove all cart and line item discounts
70-
* @param disableAutomaticDiscounts Whether or not automatic discounts should be enabled after removing the discounts.
7165
*
72-
* @throws CartNotEditableError if the cart is not currently editable.
66+
* @param disableAutomaticDiscounts Whether or not automatic discounts should be enabled after removing the discounts.
7367
*/
7468
removeAllDiscounts(disableAutomaticDiscounts: boolean): Promise<void>;
7569

7670
/**
7771
* Clear the cart
78-
*
79-
* @throws CartNotEditableError if the cart is not currently editable.
8072
*/
8173
clearCart(): Promise<void>;
8274

8375
/**
8476
* Set the customer in the cart
85-
* @param customer the customer object to add to the cart
8677
*
87-
* @throws CartNotEditableError if the cart is not currently editable.
78+
* @param customer the customer object to add to the cart
8879
*/
8980
setCustomer(customer: Customer): Promise<void>;
9081

9182
/**
9283
* Remove the current customer from the cart
93-
*
94-
* @throws CartNotEditableError if the cart is not currently editable.
9584
*/
9685
removeCustomer(): Promise<void>;
9786

9887
/**
9988
* Add a custom sale to the cart
89+
*
10090
* @param customSale the custom sale object to add to the cart
10191
* @returns {string} the uuid of the line item added
102-
*
103-
* @throws CartNotEditableError if the cart is not currently editable.
10492
*/
10593
addCustomSale(customSale: CustomSale): Promise<string>;
10694

10795
/**
10896
* Add a line item by variant ID to the cart
97+
*
10998
* @param variantId the product variant's numeric ID to add to the cart
11099
* @param quantity the number of this variant to add to the cart
111100
* @returns {string} the uuid of the line item added
112-
*
113-
* @throws CartNotEditableError if the cart is not currently editable.
114101
*/
115102
addLineItem(variantId: number, quantity: number): Promise<string>;
116103

117104
/**
118105
* Remove the line item at this uuid from the cart
119-
* @param uuid the uuid of the line item that should be removed
120106
*
121-
* @throws CartNotEditableError if the cart is not currently editable.
107+
* @param uuid the uuid of the line item that should be removed
122108
*/
123109
removeLineItem(uuid: string): Promise<void>;
124110

125111
/**
126112
* Adds custom properties to the cart
127-
* @param properties the custom key to value object to attribute to the cart
128113
*
129-
* @throws CartNotEditableError if the cart is not currently editable.
114+
* @param properties the custom key to value object to attribute to the cart
130115
*/
131116
addCartProperties(properties: Record<string, string>): Promise<void>;
132117

133118
/**
134119
* Removes the specified cart properties
135-
* @param keys the collection of keys to be removed from the cart properties
136120
*
137-
* @throws CartNotEditableError if the cart is not currently editable.
121+
* @param keys the collection of keys to be removed from the cart properties
138122
*/
139123
removeCartProperties(keys: string[]): Promise<void>;
140124

141125
/**
142126
* Adds custom properties to the specified line item
127+
*
143128
* @param uuid the uuid of the line item to which the properties should be stringd
144129
* @param properties the custom key to value object to attribute to the line item
145-
*
146-
* @throws CartNotEditableError if the cart is not currently editable.
147130
*/
148131
addLineItemProperties(
149132
uuid: string,
@@ -152,31 +135,28 @@ export interface CartApiContent {
152135

153136
/**
154137
* Adds custom properties to multiple line items at the same time.
155-
* @param lineItemProperties the collection of custom line item properties to apply to their respective line items.
156138
*
157-
* @throws CartNotEditableError if the cart is not currently editable.
139+
* @param lineItemProperties the collection of custom line item properties to apply to their respective line items.
158140
*/
159141
bulkAddLineItemProperties(
160142
lineItemProperties: SetLineItemPropertiesInput[],
161143
): Promise<void>;
162144

163145
/**
164146
* Removes the specified line item properties
147+
*
165148
* @param uuid the uuid of the line item to which the properties should be removed
166149
* @param keys the collection of keys to be removed from the line item properties
167-
*
168-
* @throws CartNotEditableError if the cart is not currently editable.
169150
*/
170151
removeLineItemProperties(uuid: string, keys: string[]): Promise<void>;
171152

172153
/**
173154
* Add a discount on a line item to the cart
155+
*
174156
* @param uuid the uuid of the line item that should receive a discount
175157
* @param type the type of discount applied (example: 'Percentage')
176158
* @param title the title attributed with the discount
177159
* @param amount the percentage or fixed monetary amount deducted with the discout
178-
*
179-
* @throws CartNotEditableError if the cart is not currently editable.
180160
*/
181161
setLineItemDiscount(
182162
uuid: string,
@@ -187,28 +167,25 @@ export interface CartApiContent {
187167

188168
/**
189169
* Set line item discounts to multiple line items at the same time.
190-
* @param lineItemDiscounts a map of discounts to add. They key is the uuid of the line item you want to add the discount to. The value is the discount input.
191170
*
192-
* @throws CartNotEditableError if the cart is not currently editable.
171+
* @param lineItemDiscounts a map of discounts to add. They key is the uuid of the line item you want to add the discount to. The value is the discount input.
193172
*/
194173
bulkSetLineItemDiscounts(
195174
lineItemDiscounts: SetLineItemDiscountInput[],
196175
): Promise<void>;
197176

198177
/**
199178
* Sets an attributed staff to all line items in the cart.
200-
* @param staffId the ID of the staff. Providing undefined will clear the attributed staff from all line items.
201179
*
202-
* @throws CartNotEditableError if the cart is not currently editable.
180+
* @param staffId the ID of the staff. Providing undefined will clear the attributed staff from all line items.
203181
*/
204182
setAttributedStaff(staffId: number | undefined): Promise<void>;
205183

206184
/**
207185
* Sets an attributed staff to a specific line items in the cart.
186+
*
208187
* @param staffId the ID of the staff. Providing undefined will clear the attributed staff on the line item.
209188
* @param lineItemUuid the UUID of the line item.
210-
*
211-
* @throws CartNotEditableError if the cart is not currently editable.
212189
*/
213190
setAttributedStaffToLineItem(
214191
staffId: number | undefined,
@@ -217,33 +194,29 @@ export interface CartApiContent {
217194

218195
/**
219196
* Remove all discounts from a line item
220-
* @param uuid the uuid of the line item whose discounts should be removed
221197
*
222-
* @throws CartNotEditableError if the cart is not currently editable.
198+
* @param uuid the uuid of the line item whose discounts should be removed
223199
*/
224200
removeLineItemDiscount(uuid: string): Promise<void>;
225201

226202
/**
227203
* Add an address to the customer (Customer must be present)
228-
* @param address the address object to add to the customer in cart
229204
*
230-
* @throws CartNotEditableError if the cart is not currently editable.
205+
* @param address the address object to add to the customer in cart
231206
*/
232207
addAddress(address: Address): Promise<void>;
233208

234209
/**
235210
* Delete an address from the customer (Customer must be present)
236-
* @param addressId the address ID to delete
237211
*
238-
* @throws CartNotEditableError if the cart is not currently editable.
212+
* @param addressId the address ID to delete
239213
*/
240214
deleteAddress(addressId: number): Promise<void>;
241215

242216
/**
243217
* Update the default address for the customer (Customer must be present)
244-
* @param addressId the address ID to set as the default address
245218
*
246-
* @throws CartNotEditableError if the cart is not currently editable.
219+
* @param addressId the address ID to set as the default address
247220
*/
248221
updateDefaultAddress(addressId: number): Promise<void>;
249222
}

packages/ui-extensions/src/surfaces/point-of-sale/render/api/cart-api/errors.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)