Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useNavigationCurrentEntry customer account #1526

Merged
merged 6 commits into from
Nov 28, 2023
Merged

Conversation

oluwatimio
Copy link
Contributor

@oluwatimio oluwatimio commented Nov 13, 2023

Background

Partially Fixes https://github.com/Shopify/core-issues/issues/62603

Solution

🎩

Tophat https://github.com/Shopify/customer-account-web/pull/3400

Checklist

  • I have 🎩'd these changes
  • I have updated relevant documentation

import {useApi} from './api';
import {useEffect, useReducer} from 'react';

export function useLiveFullPageNavigation<
Copy link
Contributor Author

@oluwatimio oluwatimio Nov 13, 2023

Choose a reason for hiding this comment

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

Extension points without full page target cant use this hook

Copy link
Member

Choose a reason for hiding this comment

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

Can we throw an error if it's used in an unsupported extension point?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just did! If its a standard navigation, we wont have the current entry and the navigation events so throwing an error in that case

import {useApi} from './api';
import {useEffect, useReducer} from 'react';

export function useLiveFullPageNavigation<
Copy link
Member

Choose a reason for hiding this comment

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

Can we add tests for this? 🤔

Copy link
Member

Choose a reason for hiding this comment

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

I know there's gonna be some mocking required, but we can ensure that the state is synchronized with the currententrychange event.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated the tests

This comment has been minimized.

@robin-drexler robin-drexler mentioned this pull request Nov 20, 2023
2 tasks
import {useApi} from './api';
import {useEffect, useReducer} from 'react';

export function useLiveFullPageNavigation<
Copy link
Member

Choose a reason for hiding this comment

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

I think we never use the word live anywhere for subscriptions.

Can we rename this hook to maybe useNavigationCurrentEntry? I feel like useCurrentEntry could also work, but might be unclear on it's own? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I prefer useNavigationCurrentEntry its more descriptive that it belongs to the navigation api

Copy link
Member

Choose a reason for hiding this comment

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

This would require some coordination, but can we move this hook into navigation after this PR is merged?

#1548

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good!

useEffect(() => {
if (!currentEntry || !removeEventListener || !addEventListener) {
throw new Error(
'useLiveFullPageNavigation must be used in a full page extension only',
Copy link
Member

Choose a reason for hiding this comment

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

Let's include the actual target name so it's easier to google?


destroyAll();

expect(mock.removeEventListener).toHaveBeenCalledWith(
Copy link
Member

Choose a reason for hiding this comment

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

mock isn't being reset after/before each test, right?

So if you added another test that asserted on mock.removeEventListener to have been called this would always be true even if it was never called inside the test, right?

Could we maybe put the mock into a createMock function that we call in each test? This way, it'd always be a new instance.

const {currentEntry, removeEventListener, addEventListener} =
useApi<Target>().navigation;

const [entry, update] = useReducer(() => currentEntry, currentEntry);
Copy link
Member

Choose a reason for hiding this comment

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

Is this the implementation from the RFC comment? :D

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Its pretty close yeah! haha

@oluwatimio oluwatimio changed the title UseLiveFullPageNavigation customer account useNavigationCurrentEntry customer account Nov 22, 2023
@oluwatimio oluwatimio merged commit 22ac2d9 into unstable Nov 28, 2023
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants