Skip to content

Commit b9e365d

Browse files
committed
Add order-api examples
1 parent f66ae59 commit b9e365d

File tree

10 files changed

+268
-0
lines changed

10 files changed

+268
-0
lines changed

packages/ui-extensions-react/src/surfaces/point-of-sale/hooks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export {
1414
useStatefulSubscribableLocale,
1515
destroyStatefulSubscribableLocale,
1616
} from './locale-api';
17+
export {useOrder} from './order-api';
1718

1819
export {
1920
useScannerDataSubscription,

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import {ReferenceEntityTemplateSchema} from '@shopify/generate-docs';
2+
import {generateCodeBlock} from '../helpers/generateCodeBlock';
23
import {ExtensionTargetType, TargetLink} from '../types/ExtensionTargetType';
34

5+
const generateCodeBlockForOrderApi = (title: string, fileName: string) =>
6+
generateCodeBlock(title, 'order-api', fileName);
7+
48
const data: ReferenceEntityTemplateSchema = {
59
name: 'Order API',
610
description: `
@@ -25,6 +29,35 @@ The Order API provides an extension with data about the current order.
2529
],
2630
category: 'APIs',
2731
related: [],
32+
examples: {
33+
description: 'Examples of using the Order API',
34+
examples: [
35+
{
36+
codeblock: generateCodeBlockForOrderApi(
37+
'Basic usage of the Order API with useOrder hook',
38+
'basic-usage',
39+
),
40+
},
41+
{
42+
codeblock: generateCodeBlockForOrderApi(
43+
'Display order details in a custom block',
44+
'display-in-block',
45+
),
46+
},
47+
{
48+
codeblock: generateCodeBlockForOrderApi(
49+
'Use Order API with navigation',
50+
'with-navigation',
51+
),
52+
},
53+
{
54+
codeblock: generateCodeBlockForOrderApi(
55+
'Simple useOrder hook example',
56+
'use-order-hook',
57+
),
58+
},
59+
],
60+
},
2861
};
2962

3063
export default data;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {
2+
extension,
3+
Screen,
4+
Navigator,
5+
ScrollView,
6+
Text,
7+
Section,
8+
} from '@shopify/ui-extensions/point-of-sale';
9+
10+
export default extension('pos.purchase.post.block.render', (root, api) => {
11+
// Access order information from the API
12+
const order = api.order;
13+
14+
const navigator = root.createComponent(Navigator, {});
15+
const screen = navigator.createComponent(Screen, {
16+
name: 'OrderDetails',
17+
title: 'Order Details',
18+
});
19+
const scrollView = screen.createComponent(ScrollView, {});
20+
const section = scrollView.createComponent(Section, {
21+
title: 'Order Information',
22+
});
23+
24+
section.createComponent(Text, {}, `Order ID: ${order.id}`);
25+
section.createComponent(Text, {}, `Order Name: ${order.name}`);
26+
27+
if (order.customerId) {
28+
section.createComponent(Text, {}, `Customer ID: ${order.customerId}`);
29+
}
30+
31+
root.mount();
32+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import React from 'react';
2+
import {
3+
reactExtension,
4+
Screen,
5+
Navigator,
6+
ScrollView,
7+
Text,
8+
Section,
9+
useOrder,
10+
} from '@shopify/ui-extensions-react/point-of-sale';
11+
12+
const SmartGridModal = () => {
13+
// Use the useOrder hook to access order information
14+
const order = useOrder();
15+
16+
return (
17+
<Navigator>
18+
<Screen name="OrderDetails" title="Order Details">
19+
<ScrollView>
20+
<Section title="Order Information">
21+
<Text>Order ID: {order.id}</Text>
22+
<Text>Order Name: {order.name}</Text>
23+
{order.customerId && <Text>Customer ID: {order.customerId}</Text>}
24+
</Section>
25+
</ScrollView>
26+
</Screen>
27+
</Navigator>
28+
);
29+
};
30+
31+
export default reactExtension('pos.purchase.post.block.render', () => (
32+
<SmartGridModal />
33+
));
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import {
2+
extension,
3+
POSBlock,
4+
POSBlockRow,
5+
} from '@shopify/ui-extensions/point-of-sale';
6+
7+
export default extension('pos.order-details.block.render', (root, api) => {
8+
const order = api.order;
9+
10+
const posBlock = root.createComponent(POSBlock, {});
11+
12+
posBlock.createComponent(POSBlockRow, {
13+
label: 'Order',
14+
value: order.name,
15+
});
16+
17+
posBlock.createComponent(POSBlockRow, {
18+
label: 'Order ID',
19+
value: order.id.toString(),
20+
});
21+
22+
if (order.customerId) {
23+
posBlock.createComponent(POSBlockRow, {
24+
label: 'Customer ID',
25+
value: order.customerId.toString(),
26+
});
27+
}
28+
29+
root.mount();
30+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from 'react';
2+
import {
3+
reactExtension,
4+
POSBlock,
5+
POSBlockRow,
6+
useOrder,
7+
} from '@shopify/ui-extensions-react/point-of-sale';
8+
9+
const OrderDetailsBlock = () => {
10+
// Use the useOrder hook directly
11+
const order = useOrder();
12+
13+
return (
14+
<POSBlock>
15+
<POSBlockRow label="Order" value={order.name} />
16+
<POSBlockRow label="Order ID" value={order.id.toString()} />
17+
{order.customerId && (
18+
<POSBlockRow label="Customer ID" value={order.customerId.toString()} />
19+
)}
20+
</POSBlock>
21+
);
22+
};
23+
24+
export default reactExtension('pos.order-details.block.render', () => (
25+
<OrderDetailsBlock />
26+
));
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import {extension, Text, Stack} from '@shopify/ui-extensions/point-of-sale';
2+
3+
export default extension('pos.order-details.block.render', (root, api) => {
4+
// Access order data directly from the API
5+
const order = api.order;
6+
7+
const stack = root.createComponent(Stack, {});
8+
9+
stack.createComponent(Text, {}, `Order #${order.name}`);
10+
stack.createComponent(Text, {}, `ID: ${order.id}`);
11+
12+
if (order.customerId) {
13+
stack.createComponent(Text, {}, `Customer ID: ${order.customerId}`);
14+
}
15+
16+
root.mount();
17+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React from 'react';
2+
import {
3+
reactExtension,
4+
Text,
5+
Stack,
6+
useOrder,
7+
} from '@shopify/ui-extensions-react/point-of-sale';
8+
9+
const OrderInfo = () => {
10+
// The useOrder hook provides direct access to order data
11+
const order = useOrder();
12+
13+
return (
14+
<Stack>
15+
<Text>Order #{order.name}</Text>
16+
<Text>ID: {order.id}</Text>
17+
{order.customerId && <Text>Customer ID: {order.customerId}</Text>}
18+
</Stack>
19+
);
20+
};
21+
22+
export default reactExtension('pos.order-details.block.render', () => (
23+
<OrderInfo />
24+
));
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import {
2+
extension,
3+
Navigator,
4+
Screen,
5+
Text,
6+
Button,
7+
} from '@shopify/ui-extensions/point-of-sale';
8+
9+
export default extension('pos.purchase.post.action.render', (root, api) => {
10+
const order = api.order;
11+
const navigation = api.navigation;
12+
13+
const navigator = root.createComponent(Navigator, {});
14+
const screen = navigator.createComponent(Screen, {
15+
name: 'OrderActions',
16+
title: 'Order Actions',
17+
});
18+
19+
screen.createComponent(Text, {}, `Processing order: ${order.name}`);
20+
21+
if (order.customerId) {
22+
screen.createComponent(Button, {
23+
title: 'View Customer',
24+
onPress: () => {
25+
navigation.navigate('CustomerDetails', {
26+
customerId: order.customerId,
27+
});
28+
},
29+
});
30+
}
31+
32+
root.mount();
33+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import React from 'react';
2+
import {
3+
reactExtension,
4+
Navigator,
5+
Screen,
6+
Text,
7+
Button,
8+
useOrder,
9+
useApi,
10+
} from '@shopify/ui-extensions-react/point-of-sale';
11+
12+
const OrderActionModal = () => {
13+
const order = useOrder();
14+
const {navigation} = useApi();
15+
16+
const handleViewCustomer = () => {
17+
if (order.customerId) {
18+
// Navigate to customer details
19+
navigation.navigate('CustomerDetails', {
20+
customerId: order.customerId,
21+
});
22+
}
23+
};
24+
25+
return (
26+
<Navigator>
27+
<Screen name="OrderActions" title="Order Actions">
28+
<Text>Processing order: {order.name}</Text>
29+
{order.customerId && (
30+
<Button title="View Customer" onPress={handleViewCustomer} />
31+
)}
32+
</Screen>
33+
</Navigator>
34+
);
35+
};
36+
37+
export default reactExtension('pos.purchase.post.action.render', () => (
38+
<OrderActionModal />
39+
));

0 commit comments

Comments
 (0)