Skip to content

Commit

Permalink
feat: add global customer info
Browse files Browse the repository at this point in the history
  • Loading branch information
kris liu authored and b3aton committed Sep 2, 2022
1 parent d4d2f30 commit 7d3fa07
Show file tree
Hide file tree
Showing 8 changed files with 328 additions and 229 deletions.
4 changes: 3 additions & 1 deletion apps/storefront/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"lint": "eslint \"**/*.{ts,tsx}\"",
"preview": "vite preview",
"test": "vitest",
"coverage": "vitest --coverage"
"coverage": "vitest --coverage",
"start": "http-server ./dist --cors"
},
"dependencies": {
"@b3/global-b3": "*",
Expand All @@ -23,6 +24,7 @@
"@rollup/plugin-graphql": "^1.1.0",
"@types/babel__core": "^7.1.19",
"date-fns": "^2.28.0",
"http-server": "^14.1.1",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"react-hook-form": "^7.33.1",
Expand Down
81 changes: 19 additions & 62 deletions apps/storefront/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,10 @@ import {
import globalB3 from '@b3/global-b3'

import {
getCustomerInfo,
} from '@/shared/service/bc'

import {
getB2BCompanyUserInfo,
} from '@/shared/service/b2b'

import {
B3SStorage,
// B3SStorage,
getChannelId,
loginInfo,
getCurrentCustomerInfo,
} from '@/utils'

import {
Expand Down Expand Up @@ -92,8 +87,8 @@ export default function App() {
const {
state: {
isB2BUser,
isLogin,
customerId,
BcToken,
},
dispatch,
} = useContext(GlobaledContext)
Expand All @@ -116,55 +111,6 @@ export default function App() {
}
}, [isOpen])

const getCurrentCustomerInfo = async () => {
try {
const {
data: {
customer: {
entityId: customerId,
phone: phoneNumber,
firstName,
lastName,
email: emailAddress = '',
},
},
} = await getCustomerInfo()

const {
companyUserInfo: {
userType,
userInfo: {
role,
},
},
} = await getB2BCompanyUserInfo(emailAddress)

if (customerId) {
B3SStorage.set('emailAddress', emailAddress)

dispatch({
type: 'common',
payload: {
isB2BUser: userType === 3,
role,
isLogin: true,
customerId,
customer: {
phoneNumber,
firstName,
lastName,
emailAddress,
},
emailAddress,
},
})
}
} catch (error) {
// eslint-disable-next-line no-console
console.log(error)
}
}

useEffect(() => {
const {
pathname,
Expand Down Expand Up @@ -201,7 +147,18 @@ export default function App() {
})
}

getCurrentCustomerInfo()
const init = async () => {
// bc token
if (!BcToken) {
await getChannelId()
await loginInfo()
}
if (!customerId) {
getCurrentCustomerInfo(dispatch)
}
}

init()
}, [])

const createConvertB2BNavNode = () => {
Expand All @@ -216,7 +173,7 @@ export default function App() {
}

useEffect(() => {
if (isLogin && !isB2BUser && customerId) {
if (!isB2BUser && customerId) {
// already exist
if (document.querySelector('.navUser-item.navUser-convert-b2b')) {
return
Expand All @@ -238,7 +195,7 @@ export default function App() {
} else {
document.querySelector('.navUser-item.navUser-convert-b2b')?.remove()
}
}, [isB2BUser, isLogin, customerId])
}, [isB2BUser, customerId])

return (
<HashRouter>
Expand Down
89 changes: 4 additions & 85 deletions apps/storefront/src/pages/login/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,17 @@ import {

import {
getB2BRegisterLogo,
getBCToken,
getBCForcePasswordReset,
getB2BLoginPageConfig,
getBCStoreChannelId,
getB2BCompanyUserInfo,
} from '@/shared/service/b2b'

import {
bcLogin,
bcLogoutLogin,
getCustomerInfo,
} from '@/shared/service/bc'

import {
B3SStorage,
getCurrentCustomerInfo,
} from '@/utils'

import {
Expand All @@ -55,11 +51,8 @@ import {
getLogo,
LoginInfoInit,
LoginConfig,
getloginTokenInfo,
loginCheckout,
getLoginFlag,
getBCChannelId,
ChannelstoreSites,
} from './config'

import LoginWidget from './component/LoginWidget'
Expand Down Expand Up @@ -88,7 +81,6 @@ export default function Login() {

const [logo, setLogo] = useState('')
const [flag, setLoginFlag] = useState<string>('')
const [channelId, setChannelId] = useState<number>(1)
const [loginAccount, setLoginAccount] = useState<LoginConfig>({
emailAddress: '',
})
Expand All @@ -103,7 +95,6 @@ export default function Login() {
const {
state: {
isCheckout,
BcToken,
},
dispatch,
} = useContext(GlobaledContext)
Expand Down Expand Up @@ -132,11 +123,6 @@ export default function Login() {
},
} = await getB2BLoginPageConfig()

const {
storeBasicInfo,
}: any = await getBCStoreChannelId()

const getChannelId = getBCChannelId((storeBasicInfo as ChannelstoreSites)?.storeSites || [])
const Info = {
loginTitle: pageTitle,
loginBtn: signInButtonText,
Expand All @@ -161,21 +147,6 @@ export default function Login() {
if (loginFlag) setLoginFlag(loginFlag)

if (loginFlag === '3') {
if (!BcToken) {
const loginTokenInfo = getloginTokenInfo(channelId)
const {
data: {
token,
},
} = await getBCToken(loginTokenInfo)
B3SStorage.set('BcToken', token)
dispatch({
type: 'common',
payload: {
BcToken: token,
},
})
}
await bcLogoutLogin()

dispatch({
Expand All @@ -186,7 +157,7 @@ export default function Login() {
})
}

setChannelId(getChannelId)
// setChannelId(getChannelId)
setLoginInfo(Info)
setLogo(registerLogo)
setLoading(false)
Expand Down Expand Up @@ -255,21 +226,6 @@ export default function Login() {
}
} else {
try {
const loginTokenInfo = getloginTokenInfo(channelId)
const {
data: {
token,
},
} = await getBCToken(loginTokenInfo)
dispatch({
type: 'common',
payload: {
BcToken: token,
},
})
B3SStorage.set('BcToken', token)
B3SStorage.set('emailAddress', data.emailAddress)

const getBCFieldsValue = {
email: data.emailAddress,
pass: data.password,
Expand All @@ -281,46 +237,9 @@ export default function Login() {
if (errors?.length || !bcData) {
getforcePasswordReset(data.emailAddress)
} else {
const {
companyUserInfo: {
userType,
userInfo: {
role,
},
},
} = await getB2BCompanyUserInfo(data.emailAddress)

const {
data: {
customer: {
entityId: customerId,
phone: phoneNumber,
firstName,
lastName,
email: emailAddress,
},
},
} = await getCustomerInfo()

// 2 bc , 3 b2b
dispatch({
type: 'common',
payload: {
isB2BUser: userType === 3,
role,
isLogin: true,
customerId,
customer: {
phoneNumber,
firstName,
lastName,
emailAddress,
},
emailAddress: data.emailAddress,
},
})
const info = await getCurrentCustomerInfo(dispatch)

if (userType === 3 && role === 3) {
if (info?.userType === 3 && info?.role === 3) {
navigate('/dashboard')
} else {
navigate('/order')
Expand Down
Loading

0 comments on commit 7d3fa07

Please sign in to comment.