Skip to content

Commit

Permalink
feat: add Business Account Application
Browse files Browse the repository at this point in the history
  • Loading branch information
kris liu authored and kris-liu-smile committed Jul 21, 2022
1 parent 055ebab commit 8229889
Show file tree
Hide file tree
Showing 12 changed files with 327 additions and 42 deletions.
8 changes: 7 additions & 1 deletion apps/storefront/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { useB3AppOpen } from '@b3/hooks'
import styled from '@emotion/styled'

import { ThemeFrame } from './ThemeFrame'
import { Home, Form, Registered } from './pages'
import {
Home, Form, Registered, RegisteredBCToB2B,
} from './pages'
import { Layout, RegisteredCloseButton } from './components'
import { RegisteredProvider } from './pages/registered/context/RegisteredContext'

Expand Down Expand Up @@ -51,6 +53,10 @@ export default function App() {
path="/registered"
element={<Registered setIsOpen={setIsOpen} />}
/>
<Route
path="/registeredbctob2b"
element={<RegisteredBCToB2B />}
/>
</Routes>
</Layout>
) : null}
Expand Down
1 change: 1 addition & 0 deletions apps/storefront/src/components/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export function Layout({
{ label: 'Home', to: '/', icon: HomeIcon },
{ label: 'Form', to: '/form', icon: VerifiedUserIcon },
{ label: 'Registered', to: '/registered', icon: VerifiedUserIcon },
{ label: 'registeredbctob2b', to: '/registeredbctob2b', icon: VerifiedUserIcon },
].map(({ label, icon: Icon, to }) => (
<ListItem
disablePadding
Expand Down
2 changes: 1 addition & 1 deletion apps/storefront/src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { Home } from './Home'
export { Form } from './Form'
export { Registered } from './registered'
export { Registered, RegisteredBCToB2B } from './registered'
57 changes: 24 additions & 33 deletions apps/storefront/src/pages/registered/RegisterComplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from '@mui/material'
import { useForm } from 'react-hook-form'
import { useB3Lang } from '@b3/lang'
import { format } from 'date-fns'

import { RegisteredContext } from './context/RegisteredContext'
import RegisteredStepButton from './component/RegisteredStepButton'
Expand Down Expand Up @@ -113,10 +114,17 @@ export default function RegisterComplete(props: RegisterCompleteProps) {

if (additionalInformation && (additionalInformation as Array<CustomFieldItems>).length) {
additionalInformation.forEach((field: CustomFieldItems) => {
bcFields.form_fields.push({
name: field.label,
value: field.default,
})
if (field.fieldType === 'date' && field.default && typeof field.default !== 'string') {
bcFields.form_fields.push({
name: field.label,
value: format(field.default, 'yyyy-MM-dd'),
})
} else {
bcFields.form_fields.push({
name: field.label,
value: field.default,
})
}
})
}
}
Expand Down Expand Up @@ -260,15 +268,12 @@ export default function RegisterComplete(props: RegisterCompleteProps) {
return
}
try {
if (dispatch) {
dispatch({
type: 'loading',
payload: {
isLoading: true,
},
})
}

dispatch({
type: 'loading',
payload: {
isLoading: true,
},
})
let isAuto = true
if (accountType === '2') {
await getBCFieldsValue(completeData)
Expand All @@ -280,30 +285,16 @@ export default function RegisterComplete(props: RegisterCompleteProps) {
const { companyCreate: { company: { companyStatus } } } = accountInfo
isAuto = +companyStatus === 1
}
if (dispatch) {
dispatch({
type: 'loading',
payload: {
isLoading: false,
},
})
dispatch({
type: 'finishInfo',
payload: {
submitSuccess: true,
isAutoApproval: isAuto,
},
})
}
handleNext()
} catch (error) {
dispatch({
type: 'loading',
type: 'finishInfo',
payload: {
isLoading: false,
submitSuccess: true,
isAutoApproval: isAuto,
},
})
console.log(error, 'error')
handleNext()
} catch (err: any) {
setErrorMessage(err?.message || err)
} finally {
dispatch({
type: 'loading',
Expand Down
2 changes: 1 addition & 1 deletion apps/storefront/src/pages/registered/RegisteredAccount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { RegisterFields, CustomFieldItems } from './config'
import { getB2BCompanyUserInfo } from '../../shared/service/b2b'

import {
InformationLabels, InformationFourLabels, TipContent, TipLogin,
InformationFourLabels, TipContent, TipLogin,
} from './styled'

interface RegisteredAccountProps {
Expand Down
265 changes: 265 additions & 0 deletions apps/storefront/src/pages/registered/RegisteredBCToB2B.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
import {
useEffect, useState, useContext, MouseEvent,
} from 'react'

import {
Box,
Button,
ImageListItem,
} from '@mui/material'

import { useB3Lang } from '@b3/lang'

import { useForm } from 'react-hook-form'
import {
InformationFourLabels, InformationLabels,
RegisteredContainer, RegisteredImage,
} from './styled'

import {
getB2BRegisterCustomFields, getB2BRegisterLogo, getB2BCountries, storeB2BBasicInfo,
} from '../../shared/service/b2b'

import { B3CustomForm } from '../../components'

import { RegisteredContext } from './context/RegisteredContext'

import { B3Sping } from '../../components/spin/B3Sping'

import {
conversionDataFormat,
RegisterFields,
contactInformationFields,
getRegisterLogo,
companyInformationFields,
companyAttachmentsFields,
addressInformationFields,
addressFieldsRequired,
Country,
State,
CustomFieldItems,
} from './config'

export default function RegisteredBCToB2B() {
const [logo, setLogo] = useState('')

const b3Lang = useB3Lang()

const {
control, handleSubmit, getValues, formState: { errors }, setValue, watch,
} = useForm({
mode: 'onSubmit',
})

const { state, dispatch } = useContext(RegisteredContext)

useEffect(() => {
const getBCAdditionalFields = async () => {
try {
if (dispatch) {
dispatch({
type: 'loading',
payload: {
isLoading: true,
},
})
dispatch({
type: 'finishInfo',
payload: {
submitSuccess: false,
},
})
}

const { companyExtraFields } = await getB2BRegisterCustomFields()
const { quoteConfig } = await getB2BRegisterLogo()
const { countries } = await getB2BCountries()
const { storeBasicInfo: { storeName } } = await storeB2BBasicInfo()
const registerLogo = getRegisterLogo(quoteConfig)

const filterCompanyExtraFields = companyExtraFields.length ? companyExtraFields.filter((field: RegisterFields) => field?.visibleToEnduser) : []
const newCompanyExtraFields: Array<RegisterFields> = conversionDataFormat(filterCompanyExtraFields)

const newAddressInformationFields = addressInformationFields(b3Lang).map((addressFields) => {
if (addressFields.name === 'country') {
addressFields.options = countries
}
return addressFields
})

if (dispatch) {
dispatch({
type: 'all',
payload: {
isLoading: false,
storeName,
contactInformation: [...contactInformationFields(b3Lang)],
companyExtraFields: [...newCompanyExtraFields],
companyInformation: [...companyInformationFields(b3Lang)],
companyAttachment: [...companyAttachmentsFields(b3Lang)],
addressBasicFields: [...newAddressInformationFields],
countryList: [...countries],
},
})
}
setLogo(registerLogo)
} catch (e) {
console.log(e)
}
}

getBCAdditionalFields()
}, [])

const setAddressFieldsRequire = (accountType: string, addressBasicFields: Array<RegisterFields>) => {
const fieldRequired = addressFieldsRequired[`account_type_${accountType || '1'}`] || {}

addressBasicFields.forEach((field: RegisterFields) => {
field.required = fieldRequired[field.name] || false
})

return addressBasicFields
}

const {
contactInformation,
isLoading,
companyInformation = [],
companyAttachment = [],
addressBasicFields = [],
countryList = [],
companyExtraFields = [],
} = state

const handleCountryChange = (countryCode: string, stateCode: string = '') => {
const stateList = countryList.find((country: Country) => country.countryCode === countryCode)?.states || []
const stateFields = addressBasicFields.find((formFields: RegisterFields) => formFields.name === 'state')

if (stateFields) {
if (stateList.length > 0) {
stateFields.fieldType = 'dropdown'
stateFields.options = stateList
} else {
stateFields.fieldType = 'text'
stateFields.options = []
}
}

setValue('state', stateCode && countryCode && (stateList.find((state: State) => state.stateCode === stateCode) || stateList.length === 0) ? stateCode : '')

dispatch({
type: 'stateList',
payload: {
stateList,
addressBasicFields: [...addressBasicFields],
},
})
}

const [addressFields, setAddressFields] = useState<Array<RegisterFields>>(addressBasicFields)

useEffect(() => {
if (addressBasicFields && addressBasicFields.length) {
setAddressFields([...setAddressFieldsRequire('1', addressBasicFields)])
}
}, [addressBasicFields])

useEffect(() => {
const subscription = watch((value, { name, type }) => {
const { country, state } = value
if (name === 'country' && type === 'change') {
handleCountryChange(country, state)
}
})
return () => subscription.unsubscribe()
}, [countryList])

const handleNext = (event: MouseEvent) => {
handleSubmit(async (data: CustomFieldItems) => {
console.log(data, 'data')
})(event)
}

return (
<RegisteredContainer>
<B3Sping
isSpinning={isLoading}
tip={b3Lang('intl.global.tips.loading')}
>
{
logo && (
<RegisteredImage>
<ImageListItem sx={{
maxWidth: '130px',
maxHeight: '130px',
}}
>
<img
src={`${logo}`}
alt={b3Lang('intl.user.register.tips.registerLogo')}
loading="lazy"
/>
</ImageListItem>
</RegisteredImage>
)
}

<InformationLabels>Business Account Application</InformationLabels>

<Box>
<InformationFourLabels>{b3Lang('intl.user.register.registeredAccount.contactInformation')}</InformationFourLabels>
<B3CustomForm
formFields={contactInformation}
errors={errors}
control={control}
getValues={getValues}
setValue={setValue}
/>

</Box>

<Box>
<InformationFourLabels>{b3Lang('intl.user.register.title.businessDetails')}</InformationFourLabels>
<B3CustomForm
formFields={[...companyInformation, ...companyExtraFields]}
errors={errors}
control={control}
getValues={getValues}
setValue={setValue}
/>
</Box>
<Box>
<InformationFourLabels>{b3Lang('intl.user.register.title.attachments')}</InformationFourLabels>
<B3CustomForm
formFields={companyAttachment}
errors={errors}
control={control}
getValues={getValues}
setValue={setValue}
/>
</Box>

<Box>
<InformationFourLabels>{b3Lang('intl.user.register.title.address')}</InformationFourLabels>

<B3CustomForm
formFields={addressFields}
errors={errors}
control={control}
getValues={getValues}
setValue={setValue}
/>
</Box>
</B3Sping>
<Box sx={{ display: 'flex', flexDirection: 'row-reverse', pt: 2 }}>
<Button
variant="contained"
onClick={handleNext}
>
{b3Lang('intl.global.button.submit')}
</Button>
</Box>

</RegisteredContainer>
)
}
Loading

0 comments on commit 8229889

Please sign in to comment.