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(samgongustofa): Validate when buyer fields are empty in TransferOfVehicleOwnership #16999

Merged
merged 10 commits into from
Nov 25, 2024
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],
)
johannaagma marked this conversation as resolved.
Show resolved Hide resolved

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,
)

johannaagma marked this conversation as resolved.
Show resolved Hide resolved
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])
johannaagma marked this conversation as resolved.
Show resolved Hide resolved

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