From 5207e14ed57e831f7af83ed119cf653063da12e8 Mon Sep 17 00:00:00 2001 From: bc-marco <109162781+bc-marco@users.noreply.github.com> Date: Fri, 12 Apr 2024 14:59:36 -0600 Subject: [PATCH] fix: logs out properly from stencil store (#1001) * fix: properly logs out from stencil * fix: update test --- apps/storefront/src/pages/login/Login.tsx | 47 ++++++++----------- .../components/quickOrder/QuickOrder.test.tsx | 4 +- 2 files changed, 23 insertions(+), 28 deletions(-) diff --git a/apps/storefront/src/pages/login/Login.tsx b/apps/storefront/src/pages/login/Login.tsx index c764620a..21f0768b 100644 --- a/apps/storefront/src/pages/login/Login.tsx +++ b/apps/storefront/src/pages/login/Login.tsx @@ -5,12 +5,12 @@ import { useEffect, useState, } from 'react' -import { useLocation, useNavigate } from 'react-router-dom' +import { useNavigate, useSearchParams } from 'react-router-dom' import { useB3Lang } from '@b3/lang' import { Alert, Box, ImageListItem } from '@mui/material' import { B3Card, B3Sping } from '@/components' -import { OpenPageState,useMobile } from '@/hooks' +import { OpenPageState, useMobile } from '@/hooks' import { CustomStyleContext } from '@/shared/customStyleButtton' import { defaultCreateAccountPanel } from '@/shared/customStyleButtton/context/config' import { GlobaledContext } from '@/shared/global' @@ -32,12 +32,7 @@ import { } from '@/utils' import LoginWidget from './component/LoginWidget' -import { - getLoginFlag, - loginCheckout, - LoginConfig, - LoginInfoInit, -} from './config' +import { loginCheckout, LoginConfig, LoginInfoInit } from './config' import LoginForm from './LoginForm' import LoginPanel from './LoginPanel' import { LoginContainer, LoginImage } from './styled' @@ -63,37 +58,34 @@ interface RegisteredProps { type AlertColor = 'success' | 'info' | 'warning' | 'error' export default function Login(props: RegisteredProps) { + const { setOpenPage } = props + const isLoggedIn = useAppSelector(isLoggedInSelector) + const salesRepCompanyId = useAppSelector( + ({ b2bFeatures }) => b2bFeatures.masqueradeCompany.id + ) + const isAgenting = useAppSelector( + ({ b2bFeatures }) => b2bFeatures.masqueradeCompany.isAgenting + ) const [isLoading, setLoading] = useState(true) const [isMobile] = useMobile() - const { setOpenPage } = props - const [showTipInfo, setShowTipInfo] = useState(true) const [flag, setLoginFlag] = useState('') + const [loginInfo, setLoginInfo] = useState(null) const [loginAccount, setLoginAccount] = useState({ emailAddress: '', password: '', }) - const location = useLocation() - - const [loginInfo, setLoginInfo] = useState(null) - const navigate = useNavigate() - const b3Lang = useB3Lang() + const [searchParams, setSearchParams] = useSearchParams() const { state: { isCheckout, logo, B3UserId, registerEnabled }, dispatch, } = useContext(GlobaledContext) - const salesRepCompanyId = useAppSelector( - ({ b2bFeatures }) => b2bFeatures.masqueradeCompany.id - ) - const isAgenting = useAppSelector( - ({ b2bFeatures }) => b2bFeatures.masqueradeCompany.isAgenting - ) const { state: { loginPageButton, @@ -135,10 +127,8 @@ export default function Login(props: RegisteredProps) { displayStoreLogo: displayStoreLogo || false, } - const { search } = location - - const loginFlag = getLoginFlag(search, 'loginFlag') - const showTipInfo = getLoginFlag(search, 'showTip') !== 'false' + const loginFlag = searchParams.get('loginFlag') + const showTipInfo = searchParams.get('showTip') !== 'false' setShowTipInfo(showTipInfo) @@ -147,7 +137,7 @@ export default function Login(props: RegisteredProps) { if (loginFlag === '7') { snackbar.error(b3Lang('login.loginText.invoiceErrorTip')) } - if (loginFlag === '3' && !isLoggedIn) { + if (loginFlag === '3' && isLoggedIn) { const { result } = (await bcLogoutLogin()).data.logout if (result !== 'success') return @@ -161,7 +151,6 @@ export default function Login(props: RegisteredProps) { logoutSession() setLoading(false) - window.location.href = '/#/login' window.location.reload() return } @@ -245,6 +234,10 @@ export default function Login(props: RegisteredProps) { const handleLoginSubmit = async (data: LoginConfig) => { setLoading(true) setLoginAccount(data) + setSearchParams((prevURLSearchParams) => { + prevURLSearchParams.delete('loginFlag') + return prevURLSearchParams + }) if (isCheckout) { try { diff --git a/apps/storefront/tests/components/quickOrder/QuickOrder.test.tsx b/apps/storefront/tests/components/quickOrder/QuickOrder.test.tsx index 1d1c32b5..ae5a804f 100644 --- a/apps/storefront/tests/components/quickOrder/QuickOrder.test.tsx +++ b/apps/storefront/tests/components/quickOrder/QuickOrder.test.tsx @@ -3,6 +3,8 @@ import { beforeEach, describe, it, vi } from 'vitest' import { ThemeFrame } from '@/components' import Quickorder from '@/pages/quickorder/Quickorder' import { GlobalProvider } from '@/shared/global' +import b2bFeatures from '@/store/slices/b2bFeatures' +import company from '@/store/slices/company' import global from '@/store/slices/global' import storeConfigs from '@/store/slices/storeConfigs' import theme from '@/store/slices/theme' @@ -38,7 +40,7 @@ describe('Quickorder component', () => { , { - reducer: { global, theme, storeConfigs }, + reducer: { global, theme, storeConfigs, company, b2bFeatures }, } ) })