Skip to content

Commit

Permalink
fix: junior can reach cart & etc
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlLiu2023 committed May 10, 2023
1 parent fd39143 commit f88be7f
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 39 deletions.
3 changes: 3 additions & 0 deletions apps/storefront/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { GlobaledContext } from '@/shared/global'
import { gotoAllowedAppPage } from '@/shared/routes'
import { setChannelStoreType } from '@/shared/service/b2b'
import {
getCompanyUserInfo,
getCurrentCustomerInfo,
getQuoteEnabled,
getStoreTaxZoneRates,
Expand All @@ -43,6 +44,7 @@ export default function App() {
quoteConfig,
storefrontConfig,
productQuoteEnabled,
emailAddress,
// showPageMask
},
dispatch,
Expand Down Expand Up @@ -140,6 +142,7 @@ export default function App() {
getStoreTaxZoneRates(),
setStorefrontConfig(dispatch, currentChannelId),
getTemPlateConfig(currentChannelId, styleDispatch, dispatch),
getCompanyUserInfo(emailAddress, dispatch, customerId, isB2BUser),
])
const userInfo = {
role: +role,
Expand Down
10 changes: 7 additions & 3 deletions apps/storefront/src/hooks/dom/useDomHooks.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Dispatch, SetStateAction, useContext } from 'react'
import { Dispatch, SetStateAction, useContext, useEffect } from 'react'
import { OpenPageState } from '@b3/hooks'

import { GlobaledContext } from '@/shared/global'
import { removeCartPermissions } from '@/utils/b3RolePermissions'

import useCartToQuote from './useCartToQuote'
import useJuniorCart from './useJuniorCart'
import useMyQuote from './useMyQuote'
import useOpenPDP from './useOpenPDP'
import useRegisteredbctob2b from './useRegisteredbctob2b'
Expand All @@ -25,7 +25,11 @@ const useDomHooks = ({ setOpenPage }: MutationObserverProps) => {
},
} = useContext(GlobaledContext)

useJuniorCart({ role })
useEffect(() => {
if (+role === 2) {
removeCartPermissions(role)
}
}, [role])

useOpenPDP({
setOpenPage,
Expand Down
17 changes: 0 additions & 17 deletions apps/storefront/src/hooks/dom/useJuniorCart.ts

This file was deleted.

1 change: 0 additions & 1 deletion apps/storefront/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export { default as useCartToQuote } from './dom/useCartToQuote'
export { default as useDomHooks } from './dom/useDomHooks'
export { default as useJuniorCart } from './dom/useJuniorCart'
export { default as useMyQuote } from './dom/useMyQuote'
export { default as useOpenPDP } from './dom/useOpenPDP'
export { default as useRegisteredbctob2b } from './dom/useRegisteredbctob2b'
Expand Down
8 changes: 3 additions & 5 deletions apps/storefront/src/shared/service/b2b/graphql/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@ const getRegisterLogo = () => `{
}
}`

const getCompanyUserInfo = <T>(email: T, customerId?: number) => `{
companyUserInfo(storeHash:"${storeHash}", email:"${email}", ${
customerId ? `customerId: ${customerId}` : ''
}) {
const getCompanyUserInfo = <T>(email: T, customerId: string | number) => `{
companyUserInfo(storeHash:"${storeHash}", email:"${email}", ${`customerId: ${customerId}`}) {
userType,
userInfo {
id
Expand Down Expand Up @@ -180,7 +178,7 @@ export const getB2BAccountFormFields = (type: number): CustomFieldItems =>

export const getB2BCompanyUserInfo = (
email: string,
customerId?: number
customerId: string | number
): CustomFieldItems =>
B3Request.graphqlB2B({
query: getCompanyUserInfo(email, customerId),
Expand Down
2 changes: 2 additions & 0 deletions apps/storefront/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
} from './graphqlDataConvert'
import {
clearCurrentCustomerInfo,
getCompanyUserInfo,
getCurrentCustomerInfo,
getCurrentJwt,
getSearchVal,
Expand Down Expand Up @@ -58,6 +59,7 @@ export {
displayFormat,
distanceDay,
getActiveCurrencyInfo,
getCompanyUserInfo,
getCookie,
getCurrentCustomerInfo,
getCurrentJwt,
Expand Down
65 changes: 52 additions & 13 deletions apps/storefront/src/utils/loginInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,44 @@ export const agentInfo = async (
}
}

export const getCompanyUserInfo = async (
emailAddress: string,
dispatch: DispatchProps,
customerId: string | number,
isB2BUser = false
) => {
try {
if (!emailAddress) return undefined

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

if (isB2BUser) {
B3SStorage.set('B3Role', role)

dispatch({
type: 'common',
payload: {
role,
},
})
}

return {
userType,
role,
id,
}
} catch (error) {
console.log(error)
}
return undefined
}

export const getCurrentCustomerInfo = async (dispatch: DispatchProps) => {
try {
const {
Expand All @@ -231,24 +269,25 @@ export const getCurrentCustomerInfo = async (dispatch: DispatchProps) => {
if (!customer) return undefined

const {
entityId: customerId,
entityId: customerId = '',
phone: phoneNumber,
firstName,
lastName,
email: emailAddress = '',
customerGroupId,
} = customer

const {
companyUserInfo: {
userType,
userInfo: { role = '', id },
},
} = await getB2BCompanyUserInfo(emailAddress, customerId)
const companyUserInfo = await getCompanyUserInfo(
emailAddress,
dispatch,
customerId
)

await getCurrentJwtAndB2BToken(userType)
if (companyUserInfo && customerId) {
const { userType, role, id } = companyUserInfo

await getCurrentJwtAndB2BToken(userType)

if (customerId) {
const [companyInfo] = await Promise.all([
getCompanyInfo(id, userType, role),
agentInfo(customerId, role, id, dispatch),
Expand Down Expand Up @@ -294,11 +333,11 @@ export const getCurrentCustomerInfo = async (dispatch: DispatchProps) => {
emailAddress,
},
})
}

return {
role,
userType,
return {
role,
userType,
}
}
} catch (error) {
console.log(error)
Expand Down

0 comments on commit f88be7f

Please sign in to comment.