@magento/venia-sample-eventing 0.0.1-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/intercept.js +18 -0
  2. package/main.js +69 -0
  3. package/package.json +24 -0
package/intercept.js ADDED
@@ -0,0 +1,18 @@
1
+ module.exports = targets => {
2
+ const { talons } = targets.of('@magento/peregrine');
3
+ const { specialFeatures } = targets.of('@magento/pwa-buildpack');
4
+
5
+ specialFeatures.tap(flags => {
6
+ /**
7
+ * Wee need to activate esModules, cssModules and GQL Queries to allow build pack to load our extension
8
+ * {@link https://magento.github.io/pwa-studio/pwa-buildpack/reference/configure-webpack/#special-flags}.
9
+ */
10
+ flags[targets.name] = {
11
+ esModules: true
12
+ };
13
+ });
14
+
15
+ talons.tap(({ App }) => {
16
+ App.useApp.wrapWith('@magento/venia-sample-eventing');
17
+ });
18
+ };
package/main.js ADDED
@@ -0,0 +1,69 @@
1
+ import { useEffect } from 'react';
2
+ import { useEventingContext } from '@magento/peregrine/lib/context/eventing';
3
+
4
+ const formatCartProducts = items => {
5
+ const productList = [];
6
+ items.forEach(item => {
7
+ productList.push({
8
+ SKU: item.product?.sku || null,
9
+ name: item.product?.name || null,
10
+ priceTotal: item.prices?.row_total?.value || null,
11
+ quantity: item?.quantity || null,
12
+ discountAmount: item.prices?.total_item_discount?.value || null,
13
+ currencyCode: item.prices?.price?.currency || null,
14
+ selectedOptions: item?.configurable_options
15
+ ? item.configurable_options.reduce((result, item) => {
16
+ const option = {
17
+ attribute: item?.option_label || null,
18
+ value: item?.value_label || null
19
+ };
20
+ return [...result, option];
21
+ }, [])
22
+ : null
23
+ });
24
+ });
25
+ return productList;
26
+ };
27
+
28
+ export default original => props => {
29
+ const [observable, { dispatch }] = useEventingContext();
30
+
31
+ useEffect(() => {
32
+ const sub = observable.subscribe(async event => {
33
+ switch (event.type) {
34
+ case 'CART_PAGE_VIEW':
35
+ console.log('Logging event:', {
36
+ type: event.type,
37
+ ...event.payload,
38
+ products: formatCartProducts(event.payload.products)
39
+ });
40
+ break;
41
+ case 'CHECKOUT_PAGE_VIEW':
42
+ console.log('Logging event:', {
43
+ type: event.type,
44
+ ...event.payload,
45
+ products: formatCartProducts(event.payload.products)
46
+ });
47
+ break;
48
+ case 'ORDER_CONFIRMATION_PAGE_VIEW':
49
+ console.log('Logging event:', {
50
+ type: event.type,
51
+ ...event.payload,
52
+ products: formatCartProducts(event.payload.products)
53
+ });
54
+ break;
55
+ default:
56
+ console.log('Logging event:', event);
57
+ break;
58
+ }
59
+ });
60
+
61
+ dispatch('hello world');
62
+
63
+ return () => {
64
+ sub.unsubscribe();
65
+ };
66
+ }, [dispatch, observable]);
67
+
68
+ return original(props);
69
+ };
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@magento/venia-sample-eventing",
3
+ "version": "0.0.1-alpha.1",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "description": "Demonstrates how to read and write PWA Studio events.",
8
+ "main": "./main.js",
9
+ "scripts": {
10
+ "clean": " "
11
+ },
12
+ "repository": "github:magento/pwa-studio",
13
+ "license": "(OSL-3.0 OR AFL-3.0)",
14
+ "peerDependencies": {
15
+ "@magento/peregrine": "~12.3.0",
16
+ "@magento/pwa-buildpack": "~11.2.0",
17
+ "react": "~17.0.1"
18
+ },
19
+ "pwa-studio": {
20
+ "targets": {
21
+ "intercept": "./intercept"
22
+ }
23
+ }
24
+ }