Skip to content

Commit

Permalink
Tilpass toggle og sletting paa forelder barn
Browse files Browse the repository at this point in the history
  • Loading branch information
betsytraran committed Nov 27, 2024
1 parent 0a52a95 commit 62cdfc3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react'
import { useContext, useEffect } from 'react'
import { useContext, useEffect, useState } from 'react'
import { SelectOptionsManager as Options } from '@/service/SelectOptions'
import { FormDollyFieldArray } from '@/components/ui/form/fieldArray/DollyFieldArray'
import {
Expand Down Expand Up @@ -54,14 +54,27 @@ export const ForelderBarnRelasjonForm = ({
const antall = opts?.antall || 1
const identMaster = opts?.identMaster || 'PDLF'

const [erBarn, setErBarn] = React.useState(
const [erBarn, setErBarn] = useState(
formMethods.watch(`${path}.relatertPersonsRolle`) === RELASJON_BARN,
)

const relatertPerson = 'relatertPerson'
const nyRelatertPerson = 'nyRelatertPerson'
const relatertPersonUtenFolkeregisteridentifikator =
'relatertPersonUtenFolkeregisteridentifikator'

const getRolle = () => {
const rolle = formMethods.watch(`${path}.relatertPersonsRolle`)
return forelderTyper.includes(rolle) ? RELASJON_FORELDER : RELASJON_BARN
}

const [relatertPersonsRolle, setRelatertPersonsRolle] = useState(getRolle())

useEffect(() => {
setRelatertPersonsRolle(getRolle())
setErBarn(formMethods.watch(`${path}.relatertPersonsRolle`) === RELASJON_BARN)
}, [formMethods.watch('pdldata.person.forelderBarnRelasjon')?.length])

const handleChangeTypeForelderBarn = (target: Target, path: string) => {
const forelderBarnRelasjon = formMethods.watch(path)
const forelderBarnClone = _.cloneDeep(forelderBarnRelasjon)
Expand Down Expand Up @@ -92,12 +105,6 @@ export const ForelderBarnRelasjonForm = ({
formMethods.trigger(path)
}

const relatertPersonsRolle = forelderTyper.includes(
formMethods.watch(`${path}.relatertPersonsRolle`),
)
? RELASJON_FORELDER
: RELASJON_BARN

const id = formMethods.watch(`${path}.id`)

const getForelderBarnType = () => {
Expand Down Expand Up @@ -130,6 +137,7 @@ export const ForelderBarnRelasjonForm = ({
<div className="toggle--wrapper">
<ToggleGroup
onChange={(value: string) => {
setRelatertPersonsRolle(value)
formMethods.setValue(
path,
value === RELASJON_BARN
Expand All @@ -140,7 +148,7 @@ export const ForelderBarnRelasjonForm = ({
formMethods.trigger(path)
}}
size={'small'}
defaultValue={relatertPersonsRolle || RELASJON_BARN}
value={relatertPersonsRolle}
style={{ backgroundColor: '#ffffff' }}
>
<ToggleGroup.Item value={RELASJON_BARN} style={{ marginRight: 0 }}>
Expand Down Expand Up @@ -249,12 +257,20 @@ export const ForelderBarnRelasjon = ({ formMethods }: ForelderForm) => {
const { identtype, identMaster, personFoerLeggTil } = useContext(BestillingsveilederContext)
const initiellMaster = identMaster === 'PDL' || identtype === 'NPID' ? 'PDL' : 'FREG'

const handleRemoveEntry = (idx: number) => {
const forelderBarnListe = formMethods.watch('pdldata.person.forelderBarnRelasjon')
const filterForelderBarnListe = forelderBarnListe?.filter((_, index) => index !== idx)
formMethods.setValue('pdldata.person.forelderBarnRelasjon', filterForelderBarnListe)
formMethods.trigger('pdldata.person.forelderBarnRelasjon')
}

return (
<FormDollyFieldArray
name="pdldata.person.forelderBarnRelasjon"
header={'Relasjon'}
newEntry={getInitialBarn(initiellMaster)}
canBeEmpty={false}
handleRemoveEntry={handleRemoveEntry}
>
{(path: string, idx: number) => {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export const Sivilstand = ({ formMethods }: SivilstandFormTypes) => {
const sivilstandListe = formMethods.watch('pdldata.person.sivilstand')
const filterSivilstandListe = sivilstandListe?.filter((_, index) => index !== idx)
formMethods.setValue('pdldata.person.sivilstand', filterSivilstandListe)
formMethods.trigger()
formMethods.trigger('pdldata.person.sivilstand')
}

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ export const PdlEksisterendePerson = ({
if (person.doedsfall) {
return false
}

if (label === 'PERSON RELATERT TIL') {
// Sivilstand gift/samboer osv
return person.alder > 17 && gyldigeSivilstanderForPartner.includes(person?.sivilstand)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const SkattekortForm = () => {
const skattekortListe = formMethods.watch('skattekort.arbeidsgiverSkatt')
const filterskattekortListe = skattekortListe?.filter((_, index) => index !== idx)
formMethods.setValue('skattekort.arbeidsgiverSkatt', filterskattekortListe)
formMethods.trigger()
formMethods.trigger('skattekort.arbeidsgiverSkatt')
}

return (
Expand Down

0 comments on commit 62cdfc3

Please sign in to comment.