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

Base dispatching for events #34

wants to merge 3 commits into from

Conversation

KrisQ
Copy link
Contributor

@KrisQ KrisQ commented Mar 1, 2021

Story: DRAMS-299

Type of Change

  • Bug fix
  • Non-breaking feature
  • Breaking feature
  • Chore (docs, refactor, etc)

What is being changed and why?

  • Add event dispatcher to Nacelle x NextJS

How to Test

@KrisQ KrisQ requested a review from NWRichmond March 1, 2021 17:23
@vercel
Copy link

vercel bot commented Mar 1, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployments, click below or on the icon next to each commit.

nacelle-nextjs-example – ./examples/nextjs

🔍 Inspect: https://vercel.com/nacelle/nacelle-nextjs-example/6R7MJHtAd8S3xudZuBnqwhLmrRDf
✅ Preview: https://nacelle-nextjs-example-git-drams-299-nacelle.vercel.app

nacelle-gatsby-example – ./examples/gatsby

🔍 Inspect: https://vercel.com/nacelle/nacelle-gatsby-example/AbRuVZaSrCoN9FD7JYjx8mVNsrf4
✅ Preview: Failed

[Deployment for 7a17789 failed]

gamma-nova-jewelry – ./examples/nextjs

🔍 Inspect: https://vercel.com/nacelle/gamma-nova-jewelry/yUWrdmviW6YV89evwV87PqXSKKAQ
✅ Preview: Failed

[Deployment for 7a17789 failed]

Copy link
Contributor

@NWRichmond NWRichmond left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice use of a reducer!

There are a few changes that will need to be made to conform with the Rules of Hooks, and some miscellaneous things that I've noted in the comments. Here is a diff of the changes required for examples/nextjs/pages/products/[handle].js:

Screen Shot 2021-03-02 at 9 19 49 AM

Do you have the ESLint VSCode extension? If so, it should be pointing out some of the ESLint errors, like this:

Screen Shot 2021-03-02 at 9 18 15 AM

It would be great if you could also set up more events (in addition to PRODUCT_VIEW) so that each event shown in the reducer gets triggered.

Lastly, if you could please add some screenshots demonstrating the functionality, or better yet, steps to follow to test the functionality, that would be very helpful.

const [eventLog, dispatchEvent] = useReducer(eventReducer, []);

useEffect(() => {
if (eventLog.length > 0 && process.browser) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

process.browser is deprecated in Next.js - instead, use typeof window !== 'undefined'

@@ -15,6 +15,11 @@ const ProductDetail = ({ product }) => {
return <div>Loading...</div>;
}

const { dispatchEvent } = useContext(EventLogContext);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. EventLogContext is undefined and needs to be imported
  2. useContext must go above the if block, since hooks can't be called conditionally.

@@ -15,6 +15,11 @@ const ProductDetail = ({ product }) => {
return <div>Loading...</div>;
}

const { dispatchEvent } = useContext(EventLogContext);
useEffect(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useEffect must go above the if block, since hooks can't be called conditionally.

Comment on lines 5 to 16
const eventReducer = (state, action) => {
if (
action.type === 'PAGE_VIEW' ||
action.type === 'PRODUCT_VIEW' ||
action.type === 'ADD_TO_CART' ||
action.type === 'REMOVE_FROM_CART' ||
action.type === 'CHECKOUT_INIT'
) {
return [...state, action];
}
return state;
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

console.log('PAGE VIEW EVENT FIRED');
break;
case 'PRODUCT_VIEW':
console.log('PRODUCT_VIEW EVENT FIRED', event.payload);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's probably good to JSON.stringify(event.payload, null, 2) so that it doesn't show [object Object]

@KrisQ
Copy link
Contributor Author

KrisQ commented Mar 2, 2021

Nice use of a reducer!

There are a few changes that will need to be made to conform with the Rules of Hooks, and some miscellaneous things that I've noted in the comments. Here is a diff of the changes required for examples/nextjs/pages/products/[handle].js:

Screen Shot 2021-03-02 at 9 19 49 AM

Do you have the ESLint VSCode extension? If so, it should be pointing out some of the ESLint errors, like this:

Screen Shot 2021-03-02 at 9 18 15 AM

It would be great if you could also set up more events (in addition to PRODUCT_VIEW) so that each event shown in the reducer gets triggered.

Lastly, if you could please add some screenshots demonstrating the functionality, or better yet, steps to follow to test the functionality, that would be very helpful.

Thanks for taking a look Nick I fixed all this! I'm happy to know I wasn't way off with this way of doing things

Copy link
Contributor

@NWRichmond NWRichmond left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once you fix the import error, the CI should run successfully for the Next.js example site.

@@ -1,11 +1,17 @@
import React from 'react';
import React, { useContext } from 'react';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useEffect also needs to be imported:

Screen Shot 2021-03-02 at 1 05 14 PM

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Strange it was working when I tested it with logs...

Should be good now thank you!

Copy link
Contributor

@NWRichmond NWRichmond left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nicely done!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants