Skip to content

Commit

Permalink
fix: logs out properly from stencil store (#1001)
Browse files Browse the repository at this point in the history
* fix: properly logs out from stencil

* fix: update test
  • Loading branch information
bc-marco authored Apr 12, 2024
1 parent ba88085 commit 5207e14
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 28 deletions.
47 changes: 20 additions & 27 deletions apps/storefront/src/pages/login/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
Expand All @@ -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<boolean>(true)
const [flag, setLoginFlag] = useState<string>('')
const [loginInfo, setLoginInfo] = useState<LoginInfoInit | null>(null)
const [loginAccount, setLoginAccount] = useState<LoginConfig>({
emailAddress: '',
password: '',
})
const location = useLocation()

const [loginInfo, setLoginInfo] = useState<LoginInfoInit | null>(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,
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand All @@ -161,7 +151,6 @@ export default function Login(props: RegisteredProps) {

logoutSession()
setLoading(false)
window.location.href = '/#/login'
window.location.reload()
return
}
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -38,7 +40,7 @@ describe('Quickorder component', () => {
</ThemeFrame>
</GlobalProvider>,
{
reducer: { global, theme, storeConfigs },
reducer: { global, theme, storeConfigs, company, b2bFeatures },
}
)
})
Expand Down

0 comments on commit 5207e14

Please sign in to comment.