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

feat: headless logout #186

Merged
merged 2 commits into from
Dec 16, 2024
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
25 changes: 22 additions & 3 deletions apps/storefront/src/components/HeadlessController.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useContext, useEffect, useRef } from 'react';
import { B2BEvent } from '@b3/hooks';
import { useB3Lang } from '@b3/lang';
import Cookies from 'js-cookie';

Expand All @@ -20,6 +21,8 @@ import {
import { setB2BToken } from '@/store/slices/company';
import { QuoteItem } from '@/types/quotes';
import CallbackManager from '@/utils/b3CallbackManager';
import b2bLogger from '@/utils/b3Logger';
import { logoutSession } from '@/utils/b3logout';
import { LineItems } from '@/utils/b3Product/b3Product';
import createShoppingList from '@/utils/b3ShoppingList/b3ShoppingList';
import { getCurrentCustomerInfo } from '@/utils/loginInfo';
Expand Down Expand Up @@ -62,21 +65,23 @@ export default function HeadlessController({ setOpenPage }: HeadlessControllerPr
const storeDispatch = useAppDispatch();
const b3Lang = useB3Lang();

const {
state: { registerEnabled, productQuoteEnabled, cartQuoteEnabled, shoppingListEnabled },
} = useContext(GlobalContext);
const { state: globalState } = useContext(GlobalContext);
const isB2BUser = useAppSelector(isB2BUserSelector);
const salesRepCompanyId = useAppSelector(({ b2bFeatures }) => b2bFeatures.masqueradeCompany.id);
const customer = useAppSelector(({ company }) => company.customer);
const role = useAppSelector(({ company }) => company.customer.role);
const productList = useAppSelector(formattedQuoteDraftListSelector);
const isAgenting = useAppSelector(({ b2bFeatures }) => b2bFeatures.masqueradeCompany.isAgenting);
const B2BToken = useAppSelector(({ company }) => company.tokens.B2BToken);

const {
state: { addQuoteBtn, shoppingListBtn, addToAllQuoteBtn },
} = useContext(CustomStyleContext);
const { addToQuote: addProductsFromCart } = addProductsFromCartToQuote(setOpenPage);

const { registerEnabled, productQuoteEnabled, cartQuoteEnabled, shoppingListEnabled } =
globalState;

const saveFn = () => {
setOpenPage({
isOpen: true,
Expand Down Expand Up @@ -124,6 +129,7 @@ export default function HeadlessController({ setOpenPage }: HeadlessControllerPr
...window.b2b,
callbacks: Manager,
utils: {
getRoutes: () => [],
openPage: (page) =>
setTimeout(() => {
if (page === 'CLOSE') {
Expand Down Expand Up @@ -186,6 +192,19 @@ export default function HeadlessController({ setOpenPage }: HeadlessControllerPr
storeDispatch(setB2BToken(b2bStorefrontJWTToken));
await getCurrentCustomerInfo(b2bStorefrontJWTToken);
},
logout: async () => {
try {
if (isAgenting) {
await endMasquerade();
}
} catch (e) {
b2bLogger.error(e);
} finally {
window.sessionStorage.clear();
logoutSession();
window.b2b.callbacks.dispatchEvent(B2BEvent.OnLogout);
}
},
},
shoppingList: {
itemFromCurrentPage: [],
Expand Down
5 changes: 4 additions & 1 deletion apps/storefront/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ declare interface CustomFieldStringItems {

type ChannelPlatform =
| 'bigcommerce'
// cSpell:ignore acquia
| 'acquia'
| 'bloomreach'
| 'catalyst'
Expand Down Expand Up @@ -36,6 +37,7 @@ declare interface Window {
callbacks: import('@/utils/b3CallbackManager').default;
utils: {
openPage: (page: import('./constants').HeadlessRoute) => void;
getRoutes: () => import('@/shared/routes').RouteItem[];
quote: {
addProductFromPage: (item: import('@/utils').LineItems) => void;
addProductsFromCart: () => Promise<void>;
Expand All @@ -47,7 +49,7 @@ declare interface Window {
getButtonInfoAddAllFromCartToQuote: () => import('@/shared/customStyleButton/context/config').BtnProperties;
};
user: {
getProfile: () => Record<string, string | number>;
getProfile: () => Record<string, any>;
getMasqueradeState: () => Promise<{
current_company_id: number;
companies: CustomFieldStringItems[];
Expand All @@ -57,6 +59,7 @@ declare interface Window {
endMasquerade: () => void;
graphqlBCProxy: typeof import('@/shared/service/request/b3Fetch').default.graphqlBCProxy;
loginWithB2BStorefrontToken: (b2bStorefrontJWTToken: string) => Promise<void>;
logout: () => Promise<void>;
};
shoppingList: {
itemFromCurrentPage: import('@/components').ProductMappedAttributes;
Expand Down
Loading