Skip to content

Commit

Permalink
fix: salesRep* remove dependency from global context (#1021)
Browse files Browse the repository at this point in the history
fix: remove dependency from global context
  • Loading branch information
bc-marco authored Apr 26, 2024
1 parent 6f954af commit 1098d30
Show file tree
Hide file tree
Showing 14 changed files with 21 additions and 74 deletions.
2 changes: 1 addition & 1 deletion apps/storefront/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export default function App() {
}

if (!customerId) {
const info = await getCurrentCustomerInfo(dispatch)
const info = await getCurrentCustomerInfo()
if (info) {
userInfo.role = info?.role
}
Expand Down
9 changes: 4 additions & 5 deletions apps/storefront/src/components/HeadlessController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ export default function HeadlessController({
const b3Lang = useB3Lang()

const {
dispatch,
state: {
salesRepCompanyId = 0,
currentChannelId,
registerEnabled,
productQuoteEnabled,
Expand All @@ -84,6 +82,9 @@ export default function HeadlessController({
},
} = useContext(GlobaledContext)
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 platform = useAppSelector(({ global }) => global.storeInfo.platform)
Expand Down Expand Up @@ -207,8 +208,6 @@ export default function HeadlessController({
endMasquerade: () => {
if (typeof customerRef.current.b2bId !== 'number') return
endMasquerade({
dispatch,
salesRepCompanyId: salesRepCompanyIdRef.current,
b2bId: customerRef.current.b2bId,
})
},
Expand All @@ -217,7 +216,7 @@ export default function HeadlessController({
b2bStorefrontJWTToken: string
) => {
storeDispatch(setB2BToken(b2bStorefrontJWTToken))
await getCurrentCustomerInfo(dispatch, b2bStorefrontJWTToken)
await getCurrentCustomerInfo(b2bStorefrontJWTToken)
},
},
shoppingList: {
Expand Down
7 changes: 3 additions & 4 deletions apps/storefront/src/components/layout/B3Mainheader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Box, Button, Typography } from '@mui/material'

import useMobile from '@/hooks/useMobile'
import { CustomStyleContext } from '@/shared/customStyleButtton'
import { GlobaledContext } from '@/shared/global'
import { useAppSelector } from '@/store'
import b3TriggerCartNumber from '@/utils/b3TriggerCartNumber'

Expand All @@ -16,11 +15,11 @@ import B3AccountInfo from './B3AccountInfo'
import B3StatusNotification from './B3StatusNotification'

export default function B3Mainheader({ title }: { title: string }) {
const {
state: { salesRepCompanyName },
} = useContext(GlobaledContext)
const role = useAppSelector(({ company }) => company.customer.role)
const companyInfo = useAppSelector(({ company }) => company.companyInfo)
const salesRepCompanyName = useAppSelector(
({ b2bFeatures }) => b2bFeatures.masqueradeCompany.companyName
)
const cartNumber = useAppSelector(({ global }) => global.cartNumber)
const navigate = useNavigate()
const b3Lang = useB3Lang()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,8 @@ export default function B3MasquradeGobalTip(props: B3MasquradeGobalTipProps) {
if (typeof b2bId === 'number') {
await superAdminEndMasquerade(+salesRepCompanyId, b2bId)
}
dispatch({
type: 'common',
payload: {
salesRepCompanyId: '',
salesRepCompanyName: '',
salesRepCustomerGroupId: '',
},
})

dispatch(clearMasqueradeCompany())
dispatch({
type: 'common',
payload: {
isAgenting: false,
},
})
setOpenPage({
isOpen: true,
openUrl: '/dashboard',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function AccountSetting() {
const [currentEamil, setCurrentEmail] = useState<string>('')

const companyId =
role === 3 && isAgenting ? +salesRepCompanyId : +companyInfoId
role === 3 && isAgenting ? salesRepCompanyId : +companyInfoId

const isBCUser = !isB2BUser || (role === 3 && !isAgenting)

Expand Down
13 changes: 6 additions & 7 deletions apps/storefront/src/pages/address/Address.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,18 @@ function Address() {
const isB2BUser = useAppSelector(isB2BUserSelector)
const companyInfoId = useAppSelector(({ company }) => company.companyInfo.id)
const role = useAppSelector(({ company }) => company.customer.role)
const {
state: { addressConfig },
dispatch,
} = useContext(GlobaledContext)

const b3Lang = useB3Lang()

const salesRepCompanyId = useAppSelector(
({ b2bFeatures }) => b2bFeatures.masqueradeCompany.id
)
const isAgenting = useAppSelector(
({ b2bFeatures }) => b2bFeatures.masqueradeCompany.isAgenting
)
const {
state: { addressConfig },
dispatch,
} = useContext(GlobaledContext)

const b3Lang = useB3Lang()
const isExtraLarge = useCardListColumn()
const [paginationTableRef] = useTableRef()

Expand Down
2 changes: 0 additions & 2 deletions apps/storefront/src/pages/dashboard/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,6 @@ function Dashboard(props: DashboardProps) {
showPageMask(dispatch, true)
if (typeof b2bId === 'number') {
await endMasquerade({
dispatch,
salesRepCompanyId: +salesRepCompanyId,
b2bId,
})
}
Expand Down
3 changes: 1 addition & 2 deletions apps/storefront/src/pages/login/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ export default function Login(props: RegisteredProps) {

const {
state: { isCheckout, logo, registerEnabled },
dispatch,
} = useContext(GlobaledContext)

const {
Expand Down Expand Up @@ -280,7 +279,7 @@ export default function Login(props: RegisteredProps) {
}
getforcePasswordReset(data.emailAddress)
} else {
const info = await getCurrentCustomerInfo(dispatch, token)
const info = await getCurrentCustomerInfo(token)

if (
info?.userType === UserTypes.MULTIPLE_B2C &&
Expand Down
3 changes: 1 addition & 2 deletions apps/storefront/src/pages/registered/Registered.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ function Registered(props: RegisteredProps) {

const {
state: { isCheckout, isCloseGotoBCHome, logo, storeName, registerEnabled },
dispatch: globalDispatch,
} = useContext(GlobaledContext)

const {
Expand Down Expand Up @@ -302,7 +301,7 @@ function Registered(props: RegisteredProps) {
})
}

await getCurrentCustomerInfo(globalDispatch)
await getCurrentCustomerInfo()

clearRegisterInfo()

Expand Down
3 changes: 1 addition & 2 deletions apps/storefront/src/pages/registered/RegisteredBCToB2B.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ export default function RegisteredBCToB2B(props: RegisteredProps) {
blockPendingAccountOrderCreation,
registerEnabled,
},
dispatch: globalDispatch,
} = useContext(GlobaledContext)

const navigate = useNavigate()
Expand Down Expand Up @@ -543,7 +542,7 @@ export default function RegisteredBCToB2B(props: RegisteredProps) {
accountType: '1',
},
})
await getCurrentCustomerInfo(globalDispatch)
await getCurrentCustomerInfo()
setShowFinishPage(true)
}
} catch (err: any) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ function Usermanagement() {
const salesRepCompanyId = useAppSelector(
({ b2bFeatures }) => b2bFeatures.masqueradeCompany.id
)

const role = useAppSelector(({ company }) => company.customer.role)
const companyInfo = useAppSelector(({ company }) => company.companyInfo)

Expand Down
6 changes: 0 additions & 6 deletions apps/storefront/src/shared/global/context/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ export interface GlobalState {
logo: string
isCompanyAccount: boolean
isAgenting: boolean
salesRepCompanyId: string
salesRepCompanyName: string
salesRepCustomerGroupId: string
tipMessage: TipMessagesProps
addressConfig?: {
key: string
Expand Down Expand Up @@ -108,9 +105,6 @@ export const initState: GlobalState = {
isCheckout: false,
isCloseGotoBCHome: false,
isAgenting: B3SStorage.get('isAgenting') || false,
salesRepCompanyId: B3SStorage.get('salesRepCompanyId') || '',
salesRepCompanyName: B3SStorage.get('salesRepCompanyName') || '',
salesRepCustomerGroupId: B3SStorage.get('salesRepCustomerGroupId') || '',
logo: '',
bcLanguage: 'en',
isCompanyAccount: false,
Expand Down
14 changes: 2 additions & 12 deletions apps/storefront/src/utils/loginInfo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { DispatchProps } from '@/shared/global/context/config'
import {
getAgentInfo,
getB2BCompanyUserInfo,
Expand Down Expand Up @@ -124,7 +123,7 @@ export const loginInfo = async () => {
store.dispatch(setbcGraphqlToken(token))
}

export const clearCurrentCustomerInfo = async (dispatch: DispatchProps) => {
export const clearCurrentCustomerInfo = async () => {
store.dispatch(setB2BToken(''))
B3SStorage.set('nextPath', '')

Expand All @@ -138,14 +137,6 @@ export const clearCurrentCustomerInfo = async (dispatch: DispatchProps) => {

store.dispatch(clearCompanySlice())
store.dispatch(clearMasqueradeCompany())

dispatch({
type: 'common',
payload: {
salesRepCompanyId: '',
salesRepCompanyName: '',
},
})
}

// companyStatus
Expand Down Expand Up @@ -290,7 +281,6 @@ const loginWithCurrentCustomerJWT = async () => {
}

export const getCurrentCustomerInfo = async (
dispatch: DispatchProps,
b2bToken?: string
) => {
const { B2BToken } = store.getState().company.tokens
Expand Down Expand Up @@ -363,7 +353,7 @@ export const getCurrentCustomerInfo = async (
}
} catch (error) {
b2bLogger.error(error)
clearCurrentCustomerInfo(dispatch)
clearCurrentCustomerInfo()
}
return undefined
}
Expand Down
16 changes: 1 addition & 15 deletions apps/storefront/src/utils/masquerade.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { DispatchProps } from '@/shared/global/context/config'
import {
getAgentInfo,
superAdminBeginMasquerade,
Expand All @@ -18,8 +17,6 @@ interface StartMasqueradeParams {
}

interface EndMasqueradeParams {
dispatch: DispatchProps
salesRepCompanyId: number
b2bId: number
}

Expand Down Expand Up @@ -48,23 +45,12 @@ export const startMasquerade = async ({
store.dispatch(setMasqueradeCompany(masqueradeCompany))
}

export const endMasquerade = async ({
b2bId,
dispatch,
}: EndMasqueradeParams) => {
export const endMasquerade = async ({ b2bId }: EndMasqueradeParams) => {
const { masqueradeCompany } = store.getState().b2bFeatures
const salesRepCompanyId = masqueradeCompany.id

// change group in bc throug b2b api
await superAdminEndMasquerade(salesRepCompanyId, b2bId)

store.dispatch(clearMasqueradeCompany())

dispatch({
type: 'common',
payload: {
salesRepCompanyName: '',
salesRepCustomerGroupId: '',
},
})
}

0 comments on commit 1098d30

Please sign in to comment.