Skip to content

Commit

Permalink
fix(samgongustofa): Validate when buyer fields are empty in TransferO…
Browse files Browse the repository at this point in the history
…fVehicleOwnership (#16999)

* Validate buyer + buyerCoOwnerAndOperator in TransferOfVehicleOwnership

* Validate added co-owners i ChangeCoOwnerOfVehicle

* Fix getApproveAnswers for changeOperatorOfVehicle

* Add error message if no changes in ChangeOperatorOfVehicle

* Revert "Validate added co-owners i ChangeCoOwnerOfVehicle"

This reverts commit 719277e.

* Add error message if no changes in ChangeCoOwnerOfVehicle

* Cleanup

* Use watch instead of getApplicationInfo

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and jonnigs committed Nov 26, 2024
1 parent 8ccbed8 commit 0276c91
Show file tree
Hide file tree
Showing 12 changed files with 204 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,38 @@ export const CoOwnerRepeater: FC<React.PropsWithChildren<FieldBaseProps>> = (
) as CoOwnersInformation[],
)
const [identicalError, setIdenticalError] = useState<boolean>(false)
const [noCoOwnerChangesError, setNoCoOwnerChangesError] =
useState<boolean>(false)

const filteredCoOwners = coOwners.filter(
({ wasRemoved }) => wasRemoved !== 'true',
)
const filteredOwnerCoOwners = ownerCoOwners.filter(
({ wasRemoved }) => wasRemoved !== 'true',
)

const updateData = useCallback(async (position: number) => {
await updateApplication({
variables: {
input: {
id: application.id,
answers: {
ownerCoOwners: ownerCoOwners.map((coOwner, index) => {
if (index === position) {
return { ...coOwner, wasRemoved: 'true' }
} else {
return coOwner
}
}),
const updateData = useCallback(
async (position: number) => {
await updateApplication({
variables: {
input: {
id: application.id,
answers: {
ownerCoOwners: ownerCoOwners.map((coOwner, index) => {
if (index === position) {
return { ...coOwner, wasRemoved: 'true' }
} else {
return coOwner
}
}),
},
},
locale,
},
locale,
},
})
}, [])
})
},
[application.id, locale, ownerCoOwners, updateApplication],
)

const addNationalIdToCoOwners = (nationalId: string, newIndex: number) => {
setCoOwners(
Expand Down Expand Up @@ -144,14 +150,29 @@ export const CoOwnerRepeater: FC<React.PropsWithChildren<FieldBaseProps>> = (
}
}, [coOwners, setValue])

setBeforeSubmitCallback &&
setBeforeSubmitCallback(async () => {
setIdenticalError(checkDuplicate())
if (checkDuplicate()) {
return [false, 'Identical nationalIds']
}
return [true, null]
})
setBeforeSubmitCallback?.(async () => {
setIdenticalError(false)
setNoCoOwnerChangesError(false)

const hasDuplicate = await checkDuplicate()
if (hasDuplicate) {
setIdenticalError(true)
return [false, 'Identical nationalIds']
}

const coOwnerWasAdded =
coOwners?.filter(({ wasRemoved }) => wasRemoved !== 'true').length > 0
const coOwnerWasRemoved = !!ownerCoOwners?.find(
(x) => x.wasRemoved === 'true',
)
const noCoOwnerChanges = !coOwnerWasAdded && !coOwnerWasRemoved
if (noCoOwnerChanges) {
setNoCoOwnerChangesError(true)
return [false, 'No co-owner has been added/removed']
}

return [true, null]
})

return (
<Box>
Expand Down Expand Up @@ -206,6 +227,14 @@ export const CoOwnerRepeater: FC<React.PropsWithChildren<FieldBaseProps>> = (
/>
</Box>
)}
{noCoOwnerChangesError && (
<Box marginTop={4}>
<AlertMessage
type="error"
title={formatMessage(information.labels.coOwner.noChangesError)}
/>
</Box>
)}
</Box>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ export const information = {
defaultMessage: 'Það má ekki nota sömu kennitölu tvisvar',
description: 'coOwner identical error',
},
noChangesError: {
id: 'ta.ccov.application:information.labels.coOwner.noChangesError',
defaultMessage: 'Ekki er búið að gera neinar breytingar á meðeigendum',
description: 'coOwner no changes error',
},
}),
vehicle: defineMessages({
sectionTitle: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ export const OperatorRepeater: FC<React.PropsWithChildren<FieldBaseProps>> = (
[],
) as OldOperatorInformation[],
)

const [identicalError, setIdenticalError] = useState<boolean>(false)
const [noOperatorChangesError, setNoOperatorChangesError] =
useState<boolean>(false)

const filteredOperators = operators.filter(
({ wasRemoved }) => wasRemoved !== 'true',
Expand Down Expand Up @@ -212,14 +215,29 @@ export const OperatorRepeater: FC<React.PropsWithChildren<FieldBaseProps>> = (
}
}, [operators, setValue])

setBeforeSubmitCallback &&
setBeforeSubmitCallback(async () => {
setIdenticalError(checkDuplicate())
if (checkDuplicate()) {
return [false, 'Identical nationalIds']
}
return [true, null]
})
setBeforeSubmitCallback?.(async () => {
setIdenticalError(false)
setNoOperatorChangesError(false)

const hasDuplicate = await checkDuplicate()
if (hasDuplicate) {
setIdenticalError(true)
return [false, 'Identical nationalIds']
}

const operatorWasAdded =
operators?.filter(({ wasRemoved }) => wasRemoved !== 'true').length > 0
const operatorWasRemoved = !!oldOperators?.find(
(x) => x.wasRemoved === 'true',
)
const noOperatorChanges = !operatorWasAdded && !operatorWasRemoved
if (noOperatorChanges) {
setNoOperatorChangesError(true)
return [false, 'No operator has been added/removed']
}

return [true, null]
})

return (
<Box>
Expand Down Expand Up @@ -288,6 +306,14 @@ export const OperatorRepeater: FC<React.PropsWithChildren<FieldBaseProps>> = (
/>
</Box>
)}
{noOperatorChangesError && (
<Box marginTop={4}>
<AlertMessage
type="error"
title={formatMessage(information.labels.operator.noChangesError)}
/>
</Box>
)}
</Box>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ export const information = {
defaultMessage: 'Það má ekki nota sömu kennitölu tvisvar',
description: 'operator identical error',
},
noChangesError: {
id: 'ta.cov.application:information.labels.operator.noChangesError',
defaultMessage: 'Ekki er búið að gera neinar breytingar á umráðamönnum',
description: 'operator no changes error',
},
}),
mainOperator: defineMessages({
sectionTitle: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const getApproveAnswers = (
)
if (ownerCoOwner) {
return {
ownerCoOwners: ownerCoOwners.map((ownerCoOwner) => {
ownerCoOwner: ownerCoOwners.map((ownerCoOwner) => {
return {
nationalId: ownerCoOwner.nationalId,
name: ownerCoOwner.name,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { getValueViaPath } from '@island.is/application/core'
import { FieldBaseProps } from '@island.is/application/types'
import { Box } from '@island.is/island-ui/core'
import { useLocale } from '@island.is/localization'
import { FC, useState, useCallback, useEffect } from 'react'
import { UserInformation } from '../../shared'
import { useMutation } from '@apollo/client'
import { UPDATE_APPLICATION } from '@island.is/application/graphql'
import { BuyerItem } from './BuyerItem'

const emailRegex =
/^[\w!#$%&'*+/=?`{|}~^-]+(?:\.[\w!#$%&'*+/=?`{|}~^-]+)*@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}$/i

export const Buyer: FC<React.PropsWithChildren<FieldBaseProps>> = (props) => {
const { locale } = useLocale()
const { application, field } = props
const { id } = field

const [updateApplication] = useMutation(UPDATE_APPLICATION)

const [buyer, setBuyer] = useState<UserInformation>(
getValueViaPath(application.answers, 'buyer', {
name: '',
nationalId: '',
phone: '',
email: '',
}) as UserInformation,
)

const updateBuyer = useCallback(
async (buyer: UserInformation) => {
await updateApplication({
variables: {
input: {
id: application.id,
answers: {
buyer: buyer,
},
},
locale,
},
})
},
[application.id, locale, updateApplication],
)

useEffect(() => {
if (
buyer.name.length > 0 &&
buyer.nationalId.length === 10 &&
buyer.phone.length >= 7 &&
emailRegex.test(buyer.email)
) {
updateBuyer(buyer)
}
}, [buyer, updateBuyer])

return (
<Box>
<BuyerItem id={id} buyer={buyer} setBuyer={setBuyer} {...props} />
</Box>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface Props {
addNationalIdToCoOwners: (nationalId: string, index: number) => void
}

export const CoOwnerAndOperatorRepeaterItem: FC<
export const BuyerCoOwnerAndOperatorRepeaterItem: FC<
React.PropsWithChildren<Props & FieldBaseProps>
> = ({
id,
Expand Down
Loading

0 comments on commit 0276c91

Please sign in to comment.