Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Base dispatching for events #34

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions examples/nextjs/components/Cart/Cart.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect } from 'react';
import React, { useState, useEffect, useContext } from 'react';
import Link from 'next/link';
import Image from 'next/image';
import { useCheckout, useCart } from '@nacelle/react-hooks';
Expand All @@ -8,6 +8,7 @@ import { formatCurrency } from '@nacelle/react-dev-utils';
import ItemQuantity from 'components/ItemQuantity';
import useDetectDevice from 'hooks/useDetectDevice';
import * as styles from './Cart.styles';
import { EventLogContext } from 'providers/EventDispatcher';

const checkoutCredentials = {
nacelleSpaceId: process.env.NACELLE_SPACE_ID,
Expand All @@ -21,9 +22,10 @@ const Cart = () => {
checkoutCredentials,
cart
);

const { dispatchEvent } = useContext(EventLogContext);
useEffect(() => {
if (checkoutData) {
dispatchEvent({ type: 'CHECKOUT_INIT', payload: checkoutData });
const { processCheckout } = checkoutData.data;
window.location = processCheckout.url;
}
Expand Down Expand Up @@ -75,6 +77,7 @@ const Cart = () => {

const CartItem = ({ item, cartActions, isMobile }) => {
const [itemQuantity, updateQuantity] = useState(item.quantity || 0);
const { dispatchEvent } = useContext(EventLogContext);

const formatPrice = formatCurrency(item.locale, item.priceCurrency);

Expand All @@ -96,7 +99,10 @@ const CartItem = ({ item, cartActions, isMobile }) => {
return updateQuantity(qty);
};

const removeItemFromCart = () => cartActions.removeFromCart(item);
const removeItemFromCart = () => {
dispatchEvent({ type: 'REMOVE_FROM_CART', payload: item });
return cartActions.removeFromCart(item);
};

return (
<div css={styles.cartItem}>
Expand Down
6 changes: 4 additions & 2 deletions examples/nextjs/components/ProductCard/ProductCard.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useState, useContext } from 'react';
import Link from 'next/link';
import Image from 'next/image';
import { useCart } from '@nacelle/react-hooks';
Expand All @@ -8,6 +8,7 @@ import { formatCurrency } from '@nacelle/react-dev-utils';
import ItemQuantity from 'components/ItemQuantity';
import useDetectDevice from 'hooks/useDetectDevice';
import * as styles from './ProductCard.styles';
import { EventLogContext } from 'providers/EventDispatcher';

const LinkPDP = ({ pdpLink, children }) => {
if (!pdpLink) {
Expand All @@ -34,6 +35,7 @@ const ProductCard = ({
const [quantity, setQuantity] = useState(0);
const [, { addToCart, toggleCart }] = useCart();
const device = useDetectDevice();
const { dispatchEvent } = useContext(EventLogContext);

const productLink = `/products/${product.handle}`;
const productVariant = product.variants[0];
Expand All @@ -44,8 +46,8 @@ const ProductCard = ({

const addItemToCart = () => {
const item = { ...product, variant: productVariant, quantity };

addToCart(item);
dispatchEvent({ type: 'ADD_TO_CART', payload: item });
return toggleCart();
};

Expand Down
13 changes: 8 additions & 5 deletions examples/nextjs/pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ import { CartProvider } from '@nacelle/react-hooks';
import Layout from 'components/Layout';
import $nacelle from 'services/nacelle.js';
import { ProductSearchProvider } from 'providers/ProductSearch';
import { EventDispatcherProvider } from 'providers/EventDispatcher';
import * as styles from 'styles/global.styles';

function MyApp({ Component, pageProps, space, products }) {
return (
<CartProvider useLocalStorage>
<Global styles={styles.global} />
<ProductSearchProvider products={products}>
<Layout space={space}>
<Component {...pageProps} />
</Layout>
</ProductSearchProvider>
<EventDispatcherProvider>
<ProductSearchProvider products={products}>
<Layout space={space}>
<Component {...pageProps} />
</Layout>
</ProductSearchProvider>
</EventDispatcherProvider>
</CartProvider>
);
}
Expand Down
8 changes: 7 additions & 1 deletion examples/nextjs/pages/pages/[handle].js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import React from 'react';
import React, { useEffect, useContext } from 'react';
import { useRouter } from 'next/router';
import $nacelle from 'services/nacelle.js';
import { dataToPaths } from 'utils';
import { EventLogContext } from 'providers/EventDispatcher';

const Page = ({ page }) => {
const router = useRouter();

const { dispatchEvent } = useContext(EventLogContext);
useEffect(() => {
dispatchEvent({ type: 'PAGE_VIEW' });
}, []);

// If the page is not yet generated, this will be displayed
// initially until getStaticProps() finishes running
if (router.isFallback) {
Expand Down
8 changes: 7 additions & 1 deletion examples/nextjs/pages/products/[handle].js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import React from 'react';
import React, { useEffect, useContext } from 'react';
import { useRouter } from 'next/router';

import $nacelle from 'services/nacelle';
import { dataToPaths } from 'utils';
import ProductCard from 'components/ProductCard';
import * as styles from 'styles/pages.styles';
import { EventLogContext } from 'providers/EventDispatcher';

const ProductDetail = ({ product }) => {
const router = useRouter();

const { dispatchEvent } = useContext(EventLogContext);
useEffect(() => {
dispatchEvent({ type: 'PRODUCT_VIEW', payload: product });
}, []);

// If the page is not yet generated, this will be displayed
// initially until getStaticProps() finishes running
if (router.isFallback) {
Expand Down
63 changes: 63 additions & 0 deletions examples/nextjs/providers/EventDispatcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React, { useEffect, useReducer } from 'react';

export const EventLogContext = React.createContext();

const eventReducer = (state, event) => {
if (
event.type === 'PAGE_VIEW' ||
event.type === 'PRODUCT_VIEW' ||
event.type === 'ADD_TO_CART' ||
event.type === 'REMOVE_FROM_CART' ||
event.type === 'CHECKOUT_INIT'
) {
return [...state, event];
}
return state;
};

export const EventDispatcherProvider = ({ children }) => {
const [eventLog, dispatchEvent] = useReducer(eventReducer, []);

useEffect(() => {
if (eventLog.length > 0 && typeof window !== 'undefined') {
const event = eventLog.pop();
switch (event.type) {
case 'PAGE_VIEW':
console.log('PAGE VIEW EVENT FIRED');
break;
case 'PRODUCT_VIEW':
console.log(
'PRODUCT_VIEW EVENT FIRED',
JSON.stringify(event.payload, null, 2)
);
break;
case 'ADD_TO_CART':
console.log(
'ADD_TO_CART EVENT FIRED',
JSON.stringify(event.payload, null, 2)
);
break;
case 'REMOVE_FROM_CART':
console.log(
'REMOVE_FROM_CART EVENT FIRED',
JSON.stringify(event.payload, null, 2)
);
break;
case 'CHECKOUT_INIT':
console.log(
'CHECKOUT_INIT EVENT FIRED',
JSON.stringify(event.payload, null, 2)
);
break;
default:
break;
}
}
}, [eventLog]);

return (
<EventLogContext.Provider value={{ dispatchEvent }}>
{children}
</EventLogContext.Provider>
);
};