Skip to content

Commit

Permalink
feat: add address page
Browse files Browse the repository at this point in the history
  • Loading branch information
b3aton committed Nov 30, 2022
1 parent c979931 commit 94641cc
Show file tree
Hide file tree
Showing 10 changed files with 590 additions and 21 deletions.
24 changes: 20 additions & 4 deletions apps/storefront/src/components/filter/B3Filter.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
useState,
ReactElement,
ReactNode,
} from 'react'
import {
Box,
Expand Down Expand Up @@ -209,11 +210,10 @@ const B3Filter:<T, Y> (props: B3FilterProps<T, Y>) => ReactElement = (props) =>
fiterMoreInfo={fiterMoreInfo}
onChange={handleFilterChange}
/>
</Box>
{
{
customButtomConfig?.isEnabled && (
<Button
// size="small"
size="small"
variant="contained"
fullWidth
sx={{
Expand All @@ -227,7 +227,23 @@ const B3Filter:<T, Y> (props: B3FilterProps<T, Y>) => ReactElement = (props) =>
</Button>
)
}

</Box>
{
customButtomConfig?.isEnabled && (
<Button
// size="small"
variant="contained"
fullWidth
sx={{
marginTop: '20px',
...customButtomConfig?.customButtomStyle || {},
}}
onClick={handleCustomBtnClick}
>
{customButtomConfig?.customLabel || ''}
</Button>
)
}
</Box>
)
}
Expand Down
24 changes: 14 additions & 10 deletions apps/storefront/src/components/table/B3Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,20 @@ export const B3Table:<T>(props: TableProps<T>) => ReactElement = ({
spacing={itemSpacing}
>
{
listItems.map((row, index) => (
<Grid
item
xs={itemXs}
>
<>
{row?.node && renderItem(row.node, index)}
</>
</Grid>
))
listItems.map((row, index) => {
const node = row.node || row || {}
return (
<Grid
item
xs={itemXs}
key={node[tableKey || 'id']}
>
<>
{row?.node && renderItem(row.node, index)}
</>
</Grid>
)
})
}
</Grid>
{
Expand Down
232 changes: 232 additions & 0 deletions apps/storefront/src/pages/address/Address.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
import {
Box, Button,
} from '@mui/material'

import {
useEffect,
useContext,
useState,
} from 'react'

import {
useMobile,
} from '@/hooks'

import B3Filter from '../../components/filter/B3Filter'
import {
AddressItemCard,
} from './components/AddressItemCard'
import {
Pagination,
B3Table,
} from '@/components/B3Table'
import {
B3Sping,
} from '@/components/spin/B3Sping'

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

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

import {
filterFormConfig,
filterSortConfig,
filterPickerConfig,
} from './shared/config'

import {
AddressItemType,
} from '../../types/address'

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

const [isRequestLoading, setIsRequestLoading] = useState(false)
const [addressList, setAddressList] = useState([])
const [searchParams, setSearchParams] = useState({})
const [pagination, setPagination] = useState<Pagination>({
offset: 0,
count: 0,
first: 1,
})

const [isMobile] = useMobile()

const getAddressList = async (pagination: Pagination, params = {}) => {
setIsRequestLoading(true)

try {
const addressKey = isB2BUser ? 'addresses' : 'customerAddresses'
const {
[addressKey]: {
edges: list = [],
totalCount,
},
}: CustomFieldItems = await getB2BCustomerAddress({
companyId,
...params,
...pagination,
})

if (isMobile) {
const newList = pagination.offset > 0 ? [...addressList, ...list] : list
setAddressList(newList)
} else {
setAddressList(list)
}

setPagination({
...pagination,
count: totalCount,
})
} finally {
setIsRequestLoading(false)
}
}

const handleChange = (key: string, value: string) => {
if (key === 'search') {
const params = {
...searchParams,
search: value,
}

setSearchParams(params)
getAddressList({
...pagination,
offset: 0,
}, params)
}
}
const handleFilterChange = (values: {[key: string]: string | number | Date}) => {
const params = {
...searchParams,
country: values.country || '',
state: values.state || '',
city: values.city || '',
}
setSearchParams(params)
getAddressList({
...pagination,
offset: 0,
}, params)
}

const handlePaginationChange = (pagination: Pagination) => {
setPagination(pagination)
getAddressList(pagination, searchParams)
}

const [editPermission, setEditPermission] = useState(false)
const [isOpenPermission, setIsOpenPermission] = useState(false)
const isAdmin = !role || role === 3

const getEditPermission = () => {
if (isAdmin) {
// TODO get config
}
}

useEffect(() => {
getEditPermission()
}, [])

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

const handleCreate = () => {
if (!checkPermission()) {
return
}
// TODO show create modal
console.log('create')
}

const handleEdit = () => {
if (!checkPermission()) {
return
}
// TODO show edit modal
console.log('edit')
}

const handleDelete = () => {
if (!checkPermission()) {
return
}
// TODO show delete modal
console.log('delete')
}

const handleSetDefault = () => {
// TODO show delete modal
console.log('setDefault')
}

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

return (
<B3Sping
isSpinning={isRequestLoading}
>
<Box sx={{
flex: 1,
}}
>
<B3Filter
startPicker={filterPickerConfig}
endPicker={filterPickerConfig}
sortByConfig={filterSortConfig}
fiterMoreInfo={filterFormConfig}
handleChange={handleChange}
handleFilterChange={handleFilterChange}
customButtomConfig={AddButtonConfig}
handleFilterCustomButtomClick={handleCreate}
/>
<B3Table
columnItems={[]}
listItems={addressList}
pagination={pagination}
onPaginationChange={handlePaginationChange}
isCustomRender
isInfiniteScroll={isMobile}
isLoading={isRequestLoading}
renderItem={(row: AddressItemType) => (
<AddressItemCard
key={row.id}
item={row}
onEdit={handleEdit}
onDelete={handleDelete}
onSetDefault={handleSetDefault}
/>
)}
/>
</Box>
</B3Sping>
)
}

export default Address
Loading

0 comments on commit 94641cc

Please sign in to comment.