From fbece809992fcb01f82fcf9aa5e114c68b5b9432 Mon Sep 17 00:00:00 2001 From: Veronika Sif Date: Thu, 15 Aug 2024 14:11:02 +0000 Subject: [PATCH] [TS-839] Calculate income per year --- .../income-plan/src/forms/IncomePlanForm.ts | 37 +++ libs/application/types/src/lib/Fields.ts | 14 +- .../TableRepeaterFormField.tsx | 261 ++++++++++-------- 3 files changed, 198 insertions(+), 114 deletions(-) diff --git a/libs/application/templates/social-insurance-administration/income-plan/src/forms/IncomePlanForm.ts b/libs/application/templates/social-insurance-administration/income-plan/src/forms/IncomePlanForm.ts index 143092113d34..d4e66c75598b 100644 --- a/libs/application/templates/social-insurance-administration/income-plan/src/forms/IncomePlanForm.ts +++ b/libs/application/templates/social-insurance-administration/income-plan/src/forms/IncomePlanForm.ts @@ -172,6 +172,43 @@ export const IncomePlanForm: Form = buildForm({ readonly: (_, activeField) => { return activeField?.income === RatioType.MONTHLY }, + updateValueObj: { + valueModifier: (_, activeField) => { + if ( + activeField?.income === RatioType.MONTHLY && + activeField?.currency === ISK + ) { + return ( + Number(activeField?.equalIncomePerMonth) * 12 + ).toString() + } + + if ( + activeField?.income === RatioType.MONTHLY && + activeField?.currency !== ISK + ) { + return ( + Number(activeField?.equalForeignIncomePerMonth) * 12 + ).toString() + } + return undefined + }, + watchValue: (_, activeField) => { + if ( + activeField?.income === RatioType.MONTHLY && + activeField?.currency === ISK + ) { + return 'equalIncomePerMonth' + } + if ( + activeField?.income === RatioType.MONTHLY && + activeField?.currency !== ISK + ) { + return 'equalForeignIncomePerMonth' + } + return undefined + }, + }, suffix: '', condition: (_, activeField) => { return ( diff --git a/libs/application/types/src/lib/Fields.ts b/libs/application/types/src/lib/Fields.ts index 0571cf036565..ebca208d90cc 100644 --- a/libs/application/types/src/lib/Fields.ts +++ b/libs/application/types/src/lib/Fields.ts @@ -68,7 +68,7 @@ type TableRepeaterOptions = | RepeaterOption[] | (( application: Application, - activeField: Record, + activeField?: Record, ) => RepeaterOption[] | []) export type TableRepeaterItem = { @@ -94,6 +94,18 @@ export type TableRepeaterItem = { application: Application, activeField?: Record, ) => boolean) + updateValueObj?: { + valueModifier: ( + application: Application, + activeField?: Record, + ) => unknown + watchValue: + | string + | (( + application: Application, + activeField?: Record, + ) => string | undefined) + } } & ( | { component: 'input' diff --git a/libs/application/ui-fields/src/lib/TableRepeaterFormField/TableRepeaterFormField.tsx b/libs/application/ui-fields/src/lib/TableRepeaterFormField/TableRepeaterFormField.tsx index 225e3acdac89..dc141d9766e4 100644 --- a/libs/application/ui-fields/src/lib/TableRepeaterFormField/TableRepeaterFormField.tsx +++ b/libs/application/ui-fields/src/lib/TableRepeaterFormField/TableRepeaterFormField.tsx @@ -1,19 +1,22 @@ import { coreMessages, formatText } from '@island.is/application/core' import { + Application, FieldBaseProps, TableRepeaterField, + TableRepeaterItem, } from '@island.is/application/types' +import { NationalIdWithName } from '@island.is/application/ui-components' import { - Stack, + AlertMessage, Box, Button, + GridColumn, + GridRow, + Icon, + Stack, Table as T, Text, - Icon, Tooltip, - GridRow, - GridColumn, - AlertMessage, } from '@island.is/island-ui/core' import { useLocale } from '@island.is/localization' import { @@ -24,9 +27,8 @@ import { RadioController, SelectController, } from '@island.is/shared/form-fields' -import { FC, useState } from 'react' +import { FC, useEffect, useState } from 'react' import { useFieldArray, useFormContext, useWatch } from 'react-hook-form' -import { NationalIdWithName } from '@island.is/application/ui-components' import { handleCustomMappedValues } from './utils' interface Props extends FieldBaseProps { @@ -117,16 +119,6 @@ export const TableRepeaterFormField: FC = ({ setActiveIndex(index) } - const getFieldError = (id: string) => { - /** - * Errors that occur in a field-array have incorrect typing - * This hack is needed to get the correct type - */ - const errorList = error as unknown as Record[] | undefined - const errors = errorList?.[activeIndex] - return errors?.[id] - } - const formatTableValue = (key: string, item: Record) => { const formatFn = table?.format?.[key] const formatted = formatFn ? formatFn(item[key]) : item[key] @@ -242,102 +234,17 @@ export const TableRepeaterFormField: FC = ({ )} - {items.map((item) => { - const { - component, - id: itemId, - backgroundColor = 'blue', - label = '', - placeholder = '', - options, - width = 'full', - condition, - readonly = false, - ...props - } = item - const isHalfColumn = component !== 'radio' && width === 'half' - const isThirdColumn = - component !== 'radio' && width === 'third' - const span = isHalfColumn - ? '1/2' - : isThirdColumn - ? '1/3' - : '1/1' - const Component = componentMapper[component] - const id = `${data.id}[${activeIndex}].${itemId}` - const activeValues = - activeIndex >= 0 && values ? values[activeIndex] : undefined - - let translatedOptions: any = [] - if (typeof options === 'function') { - translatedOptions = options(application, activeValues) - } else { - translatedOptions = options?.map((option) => ({ - ...option, - label: formatText( - option.label, - application, - formatMessage, - ), - ...(option.tooltip && { - tooltip: formatText( - option.tooltip, - application, - formatMessage, - ), - }), - })) - } - - let Readonly: boolean | undefined - if (typeof readonly === 'function') { - Readonly = readonly(application, activeValues) - } else { - Readonly = readonly - } - - if (condition && !condition(application, activeValues)) { - return null - } - - return ( - - {component === 'radio' && label && ( - - {formatText(label, application, formatMessage)} - - )} - { - if (error) { - methods.clearErrors(id) - } - }} - application={application} - {...props} - /> - - ) - })} + {items.map((item) => ( + + ))}