Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(company data): extended error handling #958

Merged
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
### Feature

- Use scroll to top button from shared components
- Company data
- Integrate /ready api to trigger once the new record is created
- Show sharing state error details in the company overlay details page
- Subscription Overlay
- implement loading state for provider subscription detail overlay

Expand Down
23 changes: 17 additions & 6 deletions src/components/pages/CompanyData/components/CompanyAddressList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import DetailsOverlay from './DetailsOverlay'
import {
setSelectedCompanyData,
setSelectedCompanyStatus,
setSharingStateInfo,
} from 'features/companyData/slice'
import LoadingProgress from 'components/shared/basic/LoadingProgress'

Expand Down Expand Up @@ -75,7 +76,9 @@ export const CompanyAddressList = ({
?.filter(
(state) =>
state.sharingStateType === SharingStateStatusType.Pending ||
state.sharingStateType === SharingStateStatusType.Initial
state.sharingStateType === SharingStateStatusType.Initial ||
state.sharingStateType === SharingStateStatusType.Ready ||
state.sharingStateType === SharingStateStatusType.Error
)
.map((state) => state.externalId)

Expand Down Expand Up @@ -122,18 +125,25 @@ export const CompanyAddressList = ({
.sharingStateType

const onRowClick = (params: GridCellParams) => {
const sharingStateInfo = sharingStates
?.filter(
(state) => state.sharingStateType === SharingStateStatusType.Error
)
.filter((state) => state.externalId === params.row.externalId)
const status = getStatus(params.row.externalId)
setDetails(true)
dispatch(setSelectedCompanyStatus(status))
dispatch(setSelectedCompanyData(params.row))
if (sharingStateInfo) dispatch(setSharingStateInfo(sharingStateInfo[0]))
}

const renderIcon = (status: string | undefined) => {
if (status === SharingStateStatusType.Success) {
return <CheckCircleIcon />
} else if (
status === SharingStateStatusType.Pending ||
status === SharingStateStatusType.Initial
status === SharingStateStatusType.Initial ||
status === SharingStateStatusType.Ready
) {
return <HourglassBottomIcon />
} else {
Expand All @@ -144,11 +154,12 @@ export const CompanyAddressList = ({
const getStatusColor = (status: string | undefined) => {
if (status === SharingStateStatusType.Success) {
return 'success'
} else if (
status === SharingStateStatusType.Pending ||
status === SharingStateStatusType.Initial
) {
} else if (status === SharingStateStatusType.Initial) {
return 'warning'
} else if (status === SharingStateStatusType.Pending) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it has been in the code before but we don't need else in cases where the previous code will return 100% sure.

instead of

if (a) {
  return x
} else if (b) {
  return y
} else {
  return z
}

simply write

if (a)
  return x
if (b)
  return y
return z

return 'info'
} else if (status === SharingStateStatusType.Ready) {
return 'primary'
} else {
return 'error'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ import { Box, Divider } from '@mui/material'
import { useState } from 'react'
import StatusInformation from './StatusInformation'
import CompanyInfo from './CompanyInfo'
import { companyDataSelector } from 'features/companyData/slice'
import {
companyDataSelector,
sharingStateInfoSelector,
} from 'features/companyData/slice'
import { useSelector } from 'react-redux'
import EditForm from './EditForm'
import { AddressType } from 'features/companyData/companyDataApiSlice'
Expand All @@ -53,6 +56,7 @@ export default function DetailsOverlay({
setEdit(true)
}
const companyData = useSelector(companyDataSelector)
const sharingStateErrorInfo = useSelector(sharingStateInfoSelector)
const isSite = companyData.address.addressType === AddressType.SiteMainAddress
return (
<Box>
Expand All @@ -64,7 +68,7 @@ export default function DetailsOverlay({
onCloseWithIcon={handleClose}
/>
<DialogContent>
<StatusInformation />
<StatusInformation error={sharingStateErrorInfo} />
<Divider
sx={{
borderColor: '#111111',
Expand Down
21 changes: 19 additions & 2 deletions src/components/pages/CompanyData/components/EditForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
type CompanyDataType,
useUpdateCompanySiteAndAddressMutation,
type CompanyDataFieldsType,
useUpdateCompanyStatusToReadyMutation,
} from 'features/companyData/companyDataApiSlice'
import { useSelector } from 'react-redux'
import {
Expand Down Expand Up @@ -67,6 +68,7 @@ export default function EditForm({
const [loading, setLoading] = useState<boolean>(false)
const [isValid, setIsValid] = useState<boolean>(false)
const [updateData] = useUpdateCompanySiteAndAddressMutation()
const [updateReadyState] = useUpdateCompanyStatusToReadyMutation()
const companyData = useSelector(companyDataSelector)
const [success, setSuccess] = useState<boolean>(false)
const [error, setError] = useState<boolean>(false)
Expand All @@ -79,6 +81,7 @@ export default function EditForm({
})
const [input, setInput] = useState<CompanyDataType>(companyDataInitialData)
const inputParams = cloneDeep(newForm ? companyDataInitialData : companyData)

if (companyInfo) {
inputParams.externalId = `${companyInfo?.bpn}_${new Date().toISOString()}`
inputParams.legalEntity.legalEntityBpn = companyInfo?.bpn
Expand Down Expand Up @@ -120,9 +123,11 @@ export default function EditForm({
}
}

const handleCreation = async () => {
const updateStateToReady = async (response: CompanyDataType[]) => {
try {
await updateData([input])
await updateReadyState({
externalIds: [response[0].externalId],
})
.unwrap()
.then(() => {
setSuccess(true)
Expand All @@ -133,6 +138,18 @@ export default function EditForm({
setLoading(false)
}

const handleCreation = async () => {
try {
await updateData([input])
.unwrap()
.then((response) => {
updateStateToReady(response)
})
} catch (e) {
setError(true)
}
}

const getTitle = () => {
if (newForm) {
return isAddress
Expand Down
4 changes: 1 addition & 3 deletions src/components/pages/CompanyData/components/FormFields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,7 @@ export const FormFields = ({
current.street &&
current.city &&
current.postalCode &&
current.countryCode &&
current.identifierNumber &&
current.countryIdentifier
current.countryCode
onValid(
formValid
? {
Expand Down
128 changes: 88 additions & 40 deletions src/components/pages/CompanyData/components/StatusInformation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,16 @@ import { useSelector } from 'react-redux'
import HourglassBottomIcon from '@mui/icons-material/HourglassBottom'
import WarningAmberIcon from '@mui/icons-material/WarningAmber'
import CheckCircleIcon from '@mui/icons-material/CheckCircle'
import { SharingStateStatusType } from 'features/companyData/companyDataApiSlice'
import {
SharingStateStatusType,
type SharingStateType,
} from 'features/companyData/companyDataApiSlice'

export default function StatusInformation() {
export default function StatusInformation({
error,
}: {
readonly error?: SharingStateType
}) {
const { t } = useTranslation()
const status = useSelector(statusSelector)

Expand All @@ -42,58 +49,99 @@ export default function StatusInformation() {
const getStatusColor = (status: string | undefined) => {
if (status === SharingStateStatusType.Success) {
return 'success'
} else if (
status === SharingStateStatusType.Pending ||
status === SharingStateStatusType.Initial
) {
} else if (status === SharingStateStatusType.Initial) {
return 'warning'
} else if (status === SharingStateStatusType.Pending) {
return 'info'
} else if (status === SharingStateStatusType.Ready) {
return 'primary'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again else after return

} else {
return 'error'
}
}

return (
<Box
sx={{
display: 'flex',
justifyContent: 'flex-start',
alignItems: 'flex-start',
marginBottom: '50px',
padding: '0px 10%',
marginTop: '50px',
}}
>
<Typography
variant="body1"
sx={{
fontSize: '18px',
width: '200px',
}}
>
{t('content.companyData.statusInfo.title')}
</Typography>
<>
<Box
sx={{
display: 'flex',
alignItems: 'center',
marginLeft: '20%',
justifyContent: 'flex-start',
alignItems: 'flex-start',
marginBottom: '50px',
padding: '0px 10%',
marginTop: '50px',
}}
>
<Chip
icon={statusIcon[status]}
color={getStatusColor(status)}
variant="filled"
label={status}
size="medium"
withIcon={true}
<Typography
variant="body1"
sx={{
marginRight: '0 !important',
margin: '0 auto',
width: '100px',
maxWidth: '100px',
fontSize: '18px',
width: '200px',
}}
/>
>
{t('content.companyData.statusInfo.title')}
</Typography>
<Box
sx={{
display: 'flex',
alignItems: 'center',
marginLeft: '20%',
flexDirection: 'column',
}}
>
<Chip
icon={statusIcon[status]}
color={getStatusColor(status)}
variant="filled"
label={status}
size="medium"
withIcon={true}
sx={{
marginRight: '0 !important',
margin: '0 auto',
width: '100px',
maxWidth: '100px',
}}
/>
</Box>
</Box>
</Box>
{error && (
<Box
sx={{
display: 'flex',
justifyContent: 'flex-start',
alignItems: 'flex-start',
marginBottom: '50px',
padding: '0px 10%',
marginTop: '50px',
}}
>
<Box
sx={{
width: '200px',
}}
></Box>
<Box
sx={{
display: 'flex',
alignItems: 'center',
marginLeft: '20%',
flexDirection: 'column',
}}
>
<Typography
variant="body1"
sx={{
fontSize: '18px',
width: '200px',
color: '#d32f2f',
}}
>
{error.sharingErrorMessage}
</Typography>
</Box>
</Box>
)}
</>
)
}
19 changes: 17 additions & 2 deletions src/features/companyData/companyDataApiSlice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export interface CompanyDataType {
deliveryServiceType: string | undefined | null
deliveryServiceQualifier: string | undefined | null
deliveryServiceNumber: string | undefined | null
}
} | null
confidenceCriteria: {
sharedByOwner: true
checkedByExternalDataSource: true
Expand Down Expand Up @@ -211,6 +211,10 @@ export enum SharingStateStatusType {
Initial = 'Initial',
}

export interface ReadyStateRequestBody {
externalIds: string[]
}

export const apiSlice = createApi({
reducerPath: 'rtk/companyData',
baseQuery: fetchBaseQuery(apiBpdmGateQuery()),
Expand Down Expand Up @@ -241,7 +245,7 @@ export const apiSlice = createApi({
}),
}),
updateCompanySiteAndAddress: builder.mutation<
CompanyDataResponse,
CompanyDataType[],
CompanyDataType[]
>({
query: (data: CompanyDataType[]) => ({
Expand All @@ -250,6 +254,16 @@ export const apiSlice = createApi({
body: data,
}),
}),
updateCompanyStatusToReady: builder.mutation<
CompanyDataType[],
ReadyStateRequestBody
>({
query: (data) => ({
url: '/sharing-state/ready',
method: 'POST',
body: data,
}),
}),
}),
})

Expand All @@ -258,4 +272,5 @@ export const {
useFetchInputCompanyBusinessPartnersMutation,
useFetchOutputCompanyBusinessPartnersMutation,
useUpdateCompanySiteAndAddressMutation,
useUpdateCompanyStatusToReadyMutation,
} = apiSlice
Loading
Loading