Skip to content

Commit

Permalink
feat: update permission judgment
Browse files Browse the repository at this point in the history
  • Loading branch information
b3aton committed Nov 30, 2022
1 parent 5c988db commit 6f8b476
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 94 deletions.
6 changes: 6 additions & 0 deletions apps/storefront/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apps/storefront/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"@mui/x-date-pickers": "^5.0.0-beta.0",
"@rollup/plugin-graphql": "^1.1.0",
"@types/babel__core": "^7.1.19",
"@types/lodash": "^4.14.190",
"date-fns": "^2.28.0",
"http-server": "^14.1.1",
"lodash": "^4.17.21",
Expand All @@ -39,6 +38,7 @@
"@b3/tsconfig": "*",
"@testing-library/react": "^13.3.0",
"@testing-library/user-event": "^14.2.5",
"@types/lodash": "^4.14.190",
"@types/react": "^18.0.0",
"@types/react-dom": "^18.0.0",
"@types/react-infinite-scroller": "^1.2.3",
Expand Down
69 changes: 33 additions & 36 deletions apps/storefront/src/pages/address/Address.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ const Address = () => {
state: {
role,
isB2BUser,
isAgenting,
salesRepCompanyId,
companyInfo: {
id: companyId,
id: companyInfoId,
},
addressConfig,
},
Expand All @@ -95,14 +97,18 @@ const Address = () => {
first: 9,
})

const companyId = role === 3 && isAgenting ? salesRepCompanyId : companyInfoId
const hasAdminPermission = isB2BUser && (!role || (role === 3 && isAgenting))
const isBCPermission = !isB2BUser || (role === 3 && !isAgenting)

const [isMobile] = useMobile()

useEffect(() => {
if (addressFields.length === 0) {
const handleGetAddressFields = async () => {
setIsRequestLoading(true)
try {
const addressFields = await getAddressFields(isB2BUser)
const addressFields = await getAddressFields(!isBCPermission)
setAddressFields(addressFields)
} catch (err) {
console.log(err)
Expand All @@ -124,7 +130,7 @@ const Address = () => {
count,
} = pagination

if (isB2BUser) {
if (!isBCPermission) {
const {
addresses: {
edges: addressList = [],
Expand Down Expand Up @@ -216,14 +222,12 @@ const Address = () => {
const [isOpenDelete, setIsOpenDelete] = useState(false)
const [currentAddress, setCurrentAddress] = useState<AddressItemType>()

const isAdmin = !isB2BUser || !role || role === 3

const getEditPermission = async () => {
if (!isB2BUser) {
if (isBCPermission) {
setEditPermission(true)
return
}
if (!role || role === 3) {
if (hasAdminPermission) {
try {
let configList = addressConfig
if (!configList) {
Expand All @@ -240,7 +244,7 @@ const Address = () => {
})
}

const key = !role ? 'address_admin' : 'address_sales_rep'
const key = role === 3 ? 'address_sales_rep' : 'address_admin'

const editPermission = (configList || []).find((config: AddressConfigItem) => config.key === key)?.isEnabled === '1'
setEditPermission(editPermission)
Expand All @@ -254,38 +258,24 @@ const Address = () => {
getEditPermission()
}, [])

const checkPermission = () => {
if (!isAdmin) {
return false
}
if (!editPermission) {
return false
}
return true
}

const handleCreate = () => {
if (!checkPermission()) {
if (!editPermission) {
snackbar.error('You do not have permission to add new address, please contact store owner ')
return
}
// TODO show create modal
addEditAddressRef.current?.handleOpenAddEditAddressClick('add', 111)
console.log('create')
addEditAddressRef.current?.handleOpenAddEditAddressClick('add')
}

const handleEdit = (row: any) => {
if (!checkPermission()) {
if (!editPermission) {
snackbar.error('You do not have permission to edit address, please contact store owner ')
return
}
// TODO show edit modal
addEditAddressRef.current?.handleOpenAddEditAddressClick('edit', row)
console.log('edit')
}

const handleDelete = (address: AddressItemType) => {
if (!checkPermission()) {
if (!editPermission) {
snackbar.error('You do not have permission to delete address, please contact store owner ')
return
}
Expand All @@ -303,7 +293,7 @@ const Address = () => {
}

const AddButtonConfig = {
isEnabled: isAdmin,
isEnabled: editPermission,
customLabel: 'Add new address',
}

Expand Down Expand Up @@ -338,35 +328,42 @@ const Address = () => {
onEdit={() => handleEdit(row)}
onDelete={handleDelete}
onSetDefault={handleSetDefault}
editPermission={editPermission}
isBCPermission={isBCPermission}
/>
)}
/>
<B3AddressForm
updateAddressList={updateAddressList}
addressFields={addressFields}
ref={addEditAddressRef}
companyId={companyId}
isBCPermission={isBCPermission}
/>
</Box>

{
isAdmin && isB2BUser && (
<SetDefaultDialog
isOpen={isOpenSetDefault}
setIsOpen={setIsOpenSetDefault}
setIsLoading={setIsRequestLoading}
addressData={currentAddress}
updateAddressList={updateAddressList}
/>
editPermission && !isBCPermission && (
<SetDefaultDialog
isOpen={isOpenSetDefault}
setIsOpen={setIsOpenSetDefault}
setIsLoading={setIsRequestLoading}
addressData={currentAddress}
updateAddressList={updateAddressList}
companyId={companyId}
/>
)
}
{
isAdmin && (
editPermission && (
<DeleteAddressDialog
isOpen={isOpenDelete}
setIsOpen={setIsOpenDelete}
setIsLoading={setIsRequestLoading}
addressData={currentAddress}
updateAddressList={updateAddressList}
companyId={companyId}
isBCPermission={isBCPermission}
/>
)
}
Expand Down
24 changes: 14 additions & 10 deletions apps/storefront/src/pages/address/components/AddressForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ import {
b2bShippingBilling,
} from '../shared/config'

import {
snackbar,
} from '@/utils'

import {
GlobaledContext,
} from '@/shared/global'
Expand Down Expand Up @@ -77,16 +81,9 @@ const deepClone = (data: any) => {
const AddressForm = ({
addressFields,
updateAddressList,
companyId,
isBCPermission,
}: any, ref: Ref<unknown> | undefined) => {
const {
state: {
isB2BUser,
companyInfo: {
id: companyId,
},
},
} = useContext(GlobaledContext)

const [open, setOpen] = useState<boolean>(false)
const [type, setType] = useState<string>('')
const [countries, setCountries] = useState<any>([])
Expand All @@ -103,6 +100,8 @@ const AddressForm = ({
isDefaultBilling: false,
})

const isB2BUser = !isBCPermission

const {
control,
handleSubmit,
Expand Down Expand Up @@ -225,6 +224,7 @@ const AddressForm = ({

if (type === 'add') {
await createB2BAddress(params)
snackbar.success('New address is added')
} else if (type === 'edit') {
const {
id,
Expand All @@ -234,6 +234,8 @@ const AddressForm = ({
...params,
id: +id,
})

snackbar.success('Edit address success')
}
setOpen(false)

Expand Down Expand Up @@ -298,6 +300,7 @@ const AddressForm = ({

if (type === 'add') {
await createBcAddress(params)
snackbar.success('New address is added')
} else if (type === 'edit') {
const {
bcAddressId,
Expand All @@ -307,6 +310,7 @@ const AddressForm = ({
...params,
id: +bcAddressId,
})
snackbar.success('Edit address success')
}
setOpen(false)

Expand Down Expand Up @@ -443,7 +447,7 @@ const AddressForm = ({

setValue('state', '')

setAllAddressFields(allAddressFields)
setAllAddressFields([...allAddressFields])
}

const handleChangeAddressType = (check: boolean, name: string) => {
Expand Down
23 changes: 5 additions & 18 deletions apps/storefront/src/pages/address/components/AddressItemCard.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import {
useContext,
} from 'react'

import Card from '@mui/material/Card'
import CardContent from '@mui/material/CardContent'
import Box from '@mui/material/Box'
Expand All @@ -19,10 +15,6 @@ import {
Theme,
} from '@mui/material'

import {
GlobaledContext,
} from '@/shared/global'

import {
AddressItemType,
} from '../../../types/address'
Expand All @@ -36,6 +28,8 @@ export interface OrderItemCardProps {
onEdit: (data: AddressItemType) => void
onDelete: (data: AddressItemType) => void
onSetDefault: (data: AddressItemType) => void
editPermission: boolean
isBCPermission: boolean
}

interface TagBoxProps {
Expand Down Expand Up @@ -70,19 +64,12 @@ export const AddressItemCard = (props: OrderItemCardProps) => {
onEdit,
onDelete,
onSetDefault,
editPermission: hasPermission,
isBCPermission,
} = props

const theme = useTheme()

const {
state: {
role,
isB2BUser,
},
} = useContext(GlobaledContext)

const hasPermission = !isB2BUser || !role || role === 3

return (
<Card
key={addressInfo.id}
Expand Down Expand Up @@ -139,7 +126,7 @@ export const AddressItemCard = (props: OrderItemCardProps) => {
hasPermission && (
<Flex>
{
isB2BUser && (
!isBCPermission && (
<Button
variant="text"
sx={{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
Dispatch,
SetStateAction,
useContext,
} from 'react'

import {
Expand All @@ -16,10 +15,6 @@ import {
B3Dialog,
} from '@/components'

import {
GlobaledContext,
} from '@/shared/global'

import {
snackbar,
} from '@/utils'
Expand All @@ -39,6 +34,8 @@ interface DeleteAddressDialogProps {
setIsLoading: Dispatch<SetStateAction<boolean>>
addressData?: AddressItemType
updateAddressList: (isFirst?: boolean) => void
companyId: string | number,
isBCPermission: boolean,
}

export const DeleteAddressDialog = (props: DeleteAddressDialogProps) => {
Expand All @@ -48,19 +45,12 @@ export const DeleteAddressDialog = (props: DeleteAddressDialogProps) => {
addressData,
updateAddressList,
setIsLoading,
companyId,
isBCPermission,
} = props

const [isMobile] = useMobile()

const {
state: {
isB2BUser,
companyInfo: {
id: companyId,
},
},
} = useContext(GlobaledContext)

const handleDelete = async () => {
if (!addressData) {
return
Expand All @@ -75,7 +65,7 @@ export const DeleteAddressDialog = (props: DeleteAddressDialogProps) => {
bcAddressId = '',
} = addressData

if (isB2BUser) {
if (!isBCPermission) {
await deleteB2BAddress({
addressId: id,
companyId,
Expand Down
Loading

0 comments on commit 6f8b476

Please sign in to comment.