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

add authenticationState api to customer account ui extension #2037

Merged
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
6 changes: 6 additions & 0 deletions .changeset/new-pugs-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@shopify/ui-extensions': patch
'@shopify/ui-extensions-react': patch
---

add authenticationState api to customer account ui extension
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type {
AuthenticationState,
RenderOrderStatusExtensionTarget,
} from '@shopify/ui-extensions/customer-account';

import {useSubscription} from './subscription';
import {useApi} from './api';

/**
* Returns authentication state of Order status page.
*/
export function useAuthenticationState<
Target extends RenderOrderStatusExtensionTarget = RenderOrderStatusExtensionTarget,
>(): AuthenticationState {
return useSubscription(useApi<Target>().authenticationState);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ export {
useAuthenticatedAccountCustomer,
useAuthenticatedAccountPurchasingCompany,
} from './authenticated-account';

export {useAuthenticationState} from './authentication-state';
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {useAuthenticationState} from '../authentication-state';
import type {Extension} from '@shopify/ui-extensions/customer-account';

import {mount, createMockStatefulRemoteSubscribable} from './mount';

describe('useAuthenticationState Hooks', () => {
it('returns authenticationState', () => {
const authenticationStateSubscribable =
createMockStatefulRemoteSubscribable(
'fully_authenticated',
) as Extension['authenticationState'];

const {value} = mount.hook(useAuthenticationState, {
extensionApi: {
extension: {
target: 'customer-account.order-status.block.render' as const,
},
authenticationState: authenticationStateSubscribable,
},
});
expect(value).toBeDefined();
expect(value).toBe('fully_authenticated');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export type {
OrderStatusPurchasingCompany,
OrderStatusBuyerIdentity,
CheckoutToken,
AuthenticationState,
} from './api/order-status/order-status';
export type {
Attribute,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ export interface Docs_OrderStatus_ShopApi
export interface Docs_OrderStatus_RequireLoginApi
extends Pick<OrderStatusApi<any>, 'requireLogin'> {}

export interface Docs_OrderStatus_AuthenticationStateApi
extends Pick<OrderStatusApi<any>, 'authenticationState'> {}

export interface Docs_OrderStatus_CartLinesApi
extends Pick<OrderStatusApi<any>, 'lines'> {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ export interface OrderStatusLocalization {
market: StatefulRemoteSubscribable<Market | undefined>;
}

export type AuthenticationState = 'fully_authenticated' | 'pre_authenticated';

export interface OrderStatusApi<Target extends ExtensionTarget> {
/**
* Methods for interacting with [Web Pixels](https://shopify.dev/docs/apps/marketing), such as emitting an event.
Expand Down Expand Up @@ -352,6 +354,11 @@ export interface OrderStatusApi<Target extends ExtensionTarget> {
* The requireLogin() method triggers login if the customer is viewing pre-authenticated Order status page.
*/
requireLogin: () => Promise<void>;

/**
* The authentication state of Order status page.
*/
authenticationState: StatefulRemoteSubscribable<AuthenticationState>;
}

export interface OrderStatusBuyerIdentity {
Expand Down
Loading