diff --git a/libs/application/templates/aosh/transfer-of-machine-ownership/src/fields/StopBuyerIfSameAsSeller/index.tsx b/libs/application/templates/aosh/transfer-of-machine-ownership/src/fields/StopBuyerIfSameAsSeller/index.tsx new file mode 100644 index 000000000000..02058dbd6d1e --- /dev/null +++ b/libs/application/templates/aosh/transfer-of-machine-ownership/src/fields/StopBuyerIfSameAsSeller/index.tsx @@ -0,0 +1,18 @@ +import { FieldBaseProps } from '@island.is/application/types' +import { FC } from 'react' +import { doSellerAndBuyerHaveSameNationalId } from '../../utils' + +export const StopBuyerIfSameAsSeller: FC< + React.PropsWithChildren +> = (props) => { + const { application, setBeforeSubmitCallback } = props + + setBeforeSubmitCallback?.(async () => { + if (doSellerAndBuyerHaveSameNationalId(application.answers)) { + return [false, ''] + } + return [true, null] + }) + + return <> +} diff --git a/libs/application/templates/aosh/transfer-of-machine-ownership/src/fields/index.ts b/libs/application/templates/aosh/transfer-of-machine-ownership/src/fields/index.ts index 9afdf0c6eec2..60e8c6a65c3c 100644 --- a/libs/application/templates/aosh/transfer-of-machine-ownership/src/fields/index.ts +++ b/libs/application/templates/aosh/transfer-of-machine-ownership/src/fields/index.ts @@ -1,3 +1,4 @@ export { ApplicationStatus } from './ApplicationStatus' export { MachinesField } from './MachinesField' export { Review } from './Review' +export { StopBuyerIfSameAsSeller } from './StopBuyerIfSameAsSeller' diff --git a/libs/application/templates/aosh/transfer-of-machine-ownership/src/forms/TransferOfMachineOwnershipForm/InformationSection/buyerSubSection.ts b/libs/application/templates/aosh/transfer-of-machine-ownership/src/forms/TransferOfMachineOwnershipForm/InformationSection/buyerSubSection.ts index c7f75e4884dd..80fc500d3237 100644 --- a/libs/application/templates/aosh/transfer-of-machine-ownership/src/forms/TransferOfMachineOwnershipForm/InformationSection/buyerSubSection.ts +++ b/libs/application/templates/aosh/transfer-of-machine-ownership/src/forms/TransferOfMachineOwnershipForm/InformationSection/buyerSubSection.ts @@ -1,12 +1,15 @@ import { + buildAlertMessageField, + buildCustomField, buildMultiField, buildNationalIdWithNameField, buildPhoneField, buildSubSection, - buildSubmitField, buildTextField, } from '@island.is/application/core' import { information } from '../../../lib/messages' +import { FormValue } from '@island.is/application/types' +import { doSellerAndBuyerHaveSameNationalId } from '../../../utils' export const buyerSubSection = buildSubSection({ id: 'buyer', @@ -35,6 +38,19 @@ export const buyerSubSection = buildSubSection({ width: 'half', required: true, }), + buildAlertMessageField({ + id: 'buyer.alertMessage', + alertType: 'warning', + title: information.labels.buyer.alertTitle, + message: information.labels.buyer.alertMessage, + condition: (answer: FormValue) => + doSellerAndBuyerHaveSameNationalId(answer), + }), + buildCustomField({ + id: 'buyer.custom', + title: '', + component: 'StopBuyerIfSameAsSeller', + }), ], }), ], diff --git a/libs/application/templates/aosh/transfer-of-machine-ownership/src/lib/messages/information.ts b/libs/application/templates/aosh/transfer-of-machine-ownership/src/lib/messages/information.ts index 5678775a9c72..844d836dfa18 100644 --- a/libs/application/templates/aosh/transfer-of-machine-ownership/src/lib/messages/information.ts +++ b/libs/application/templates/aosh/transfer-of-machine-ownership/src/lib/messages/information.ts @@ -266,6 +266,17 @@ export const information = { defaultMessage: 'Staðfesta', description: 'Submit button for buyer', }, + alertTitle: { + id: 'aosh.tmo.application:information.labels.buyer.alertTitle', + defaultMessage: 'Kennitala sú sama og hjá seljanda', + description: `Buyer alert title`, + }, + alertMessage: { + id: 'aosh.tmo.application:information.labels.buyer.alertMessage', + defaultMessage: + 'Seljandi og kaupandi getur ekki verið sá sami. Vinsamlega skráðu nýja kennitölu.', + description: `Buyer alert message`, + }, }), buyerOperators: defineMessages({ title: { diff --git a/libs/application/templates/aosh/transfer-of-machine-ownership/src/utils/doSellerAndBuyerHaveSameNationalId.ts b/libs/application/templates/aosh/transfer-of-machine-ownership/src/utils/doSellerAndBuyerHaveSameNationalId.ts new file mode 100644 index 000000000000..3113fbe45951 --- /dev/null +++ b/libs/application/templates/aosh/transfer-of-machine-ownership/src/utils/doSellerAndBuyerHaveSameNationalId.ts @@ -0,0 +1,17 @@ +import { getValueViaPath } from '@island.is/application/core' +import { FormValue } from '@island.is/application/types' + +export const doSellerAndBuyerHaveSameNationalId = (answers: FormValue) => { + const buyerNationalId = getValueViaPath( + answers, + 'buyer.nationalId', + '', + ) as string + const sellerNationalId = getValueViaPath( + answers, + 'seller.nationalId', + '', + ) as string + + return buyerNationalId === sellerNationalId +} diff --git a/libs/application/templates/aosh/transfer-of-machine-ownership/src/utils/index.ts b/libs/application/templates/aosh/transfer-of-machine-ownership/src/utils/index.ts index e5350d91740c..d4364c5a10a6 100644 --- a/libs/application/templates/aosh/transfer-of-machine-ownership/src/utils/index.ts +++ b/libs/application/templates/aosh/transfer-of-machine-ownership/src/utils/index.ts @@ -15,6 +15,7 @@ export { getReviewSteps } from './getReviewSteps' export { hasReviewerApproved } from './hasReviewerApproved' export { getApproveAnswers } from './getApproveAnswers' export { getRejecter } from './getRejecter' +export { doSellerAndBuyerHaveSameNationalId } from './doSellerAndBuyerHaveSameNationalId' export const getChargeItemCodes = ( application: Application,