From 20a0dcc2a439190fde918c7a6597ad3a1669afbb Mon Sep 17 00:00:00 2001 From: Betsy Carina Traran Date: Thu, 4 Jan 2024 15:19:28 +0100 Subject: [PATCH] Forhaapentligvis raskere henting av identer --- .../src/main/js/src/api/index.ts | 2 +- .../pdlPerson/PdlEksisterendePerson.tsx | 68 ++++++++----------- .../js/src/service/SelectOptionsOppslag.tsx | 65 +----------------- .../services/pdl/PdlForvalterService.tsx | 21 +----- .../js/src/utils/hooks/useSelectOptions.tsx | 24 +++++-- 5 files changed, 51 insertions(+), 129 deletions(-) diff --git a/apps/dolly-frontend/src/main/js/src/api/index.ts b/apps/dolly-frontend/src/main/js/src/api/index.ts index 24c56207f32..4425dbdef05 100644 --- a/apps/dolly-frontend/src/main/js/src/api/index.ts +++ b/apps/dolly-frontend/src/main/js/src/api/index.ts @@ -111,7 +111,7 @@ export const multiFetcherDokarkiv = (miljoUrlListe) => ? fetcher(obj.url, { miljo: obj.miljo }).then((result) => ({ miljo: obj.miljo, data: result, - })) + })) : { miljo: obj.miljo, data: null }, ), ) diff --git a/apps/dolly-frontend/src/main/js/src/components/fagsystem/pdlf/form/partials/pdlPerson/PdlEksisterendePerson.tsx b/apps/dolly-frontend/src/main/js/src/components/fagsystem/pdlf/form/partials/pdlPerson/PdlEksisterendePerson.tsx index 2ea674831c0..4d747be6465 100644 --- a/apps/dolly-frontend/src/main/js/src/components/fagsystem/pdlf/form/partials/pdlPerson/PdlEksisterendePerson.tsx +++ b/apps/dolly-frontend/src/main/js/src/components/fagsystem/pdlf/form/partials/pdlPerson/PdlEksisterendePerson.tsx @@ -1,16 +1,16 @@ -import React, { useContext, useEffect, useState } from 'react' +import React, { useContext } from 'react' import { FormikSelect } from '@/components/ui/form/inputs/select/Select' import Loading from '@/components/ui/loading/Loading' import { isEmpty } from '@/components/fagsystem/pdlf/form/partials/utils' import * as _ from 'lodash-es' import { BestillingsveilederContext } from '@/components/bestillingsveileder/BestillingsveilederContext' -import { identFraTestnorge } from '@/components/bestillingsveileder/stegVelger/steg/steg1/Steg1Person' -import { Option, SelectOptionsOppslag } from '@/service/SelectOptionsOppslag' -import { useBoolean } from 'react-use' +import { Option } from '@/service/SelectOptionsOppslag' import { FormikProps } from 'formik' -import { ForeldreBarnRelasjon, NyIdent, Sivilstand } from '@/components/fagsystem/pdlf/PdlTypes' +import { ForeldreBarnRelasjon, NyIdent } from '@/components/fagsystem/pdlf/PdlTypes' import { Alert } from '@navikt/ds-react' import { useParams } from 'react-router-dom' +import { useGruppeIdenter } from '@/utils/hooks/useGruppe' +import { usePdlOptions } from '@/utils/hooks/useSelectOptions' interface PdlEksisterendePersonValues { nyPersonPath?: string @@ -37,10 +37,8 @@ export const PdlEksisterendePerson = ({ const antall = opts?.antall || 1 const { gruppeId } = useParams() - const isTestnorgeIdent = identFraTestnorge(opts) - - const [identOptions, setIdentOptions] = useState>([]) - const [loadingIdentOptions, setLoadingIdentOptions] = useBoolean(true) + const { identer, loading: gruppeLoading, error: gruppeError } = useGruppeIdenter(gruppeId) + const { data: pdlOptions, loading: pdlLoading, error: pdlError } = usePdlOptions(identer) const harSivilstand = eksisterendePersonPath?.includes('sivilstand') const harNyIdent = eksisterendePersonPath?.includes('nyident') @@ -116,34 +114,17 @@ export const PdlEksisterendePerson = ({ const getFilteredOptionList = () => { const eksisterendeIdent = opts?.personFoerLeggTil?.pdlforvalter?.person?.ident - let tmpOptions = [] - // @ts-ignore - SelectOptionsOppslag.hentGruppeIdentOptions(gruppeId).then((response: [Option]) => { - tmpOptions = response?.filter((person) => { - return person.value !== eksisterendeIdent && filterOptions(person) - }) - if ( - eksisterendeNyPerson && - !tmpOptions.find((person) => person.value === eksisterendeNyPerson.value) - ) { - tmpOptions.push(eksisterendeNyPerson) - } - setIdentOptions(tmpOptions) - setLoadingIdentOptions(false) - }) - } - - useEffect(() => { - if (!isTestnorgeIdent && gruppeId) { - getFilteredOptionList() - } - }, []) - - useEffect(() => { - if (formikBag) { - getFilteredOptionList() + const tmpOptions = pdlOptions?.filter( + (person) => person.value !== eksisterendeIdent && filterOptions(person), + ) + if ( + eksisterendeNyPerson && + !tmpOptions.find((person) => person.value === eksisterendeNyPerson.value) + ) { + tmpOptions.push(eksisterendeNyPerson) } - }, [formikBag]) + return tmpOptions + } const hasNyPersonValues = nyIdentValg ? !isEmpty(nyIdentValg, ['syntetisk']) @@ -151,19 +132,26 @@ export const PdlEksisterendePerson = ({ const bestillingFlerePersoner = parseInt(antall) > 1 && (harSivilstand || harNyIdent) + const filteredOptions = getFilteredOptionList() + return (
- {loadingIdentOptions && } - {identOptions?.length > 0 ? ( + {(pdlLoading || gruppeLoading) && } + {filteredOptions?.length > 0 ? ( + ) : pdlError || gruppeError ? ( + + {pdlError?.message || gruppeError?.message || 'Feil ved henting av personer'} + ) : ( - !loadingIdentOptions && ( + !pdlLoading && + !gruppeLoading && ( Det finnes ingen eksisterende gyldige personer i denne gruppen. diff --git a/apps/dolly-frontend/src/main/js/src/service/SelectOptionsOppslag.tsx b/apps/dolly-frontend/src/main/js/src/service/SelectOptionsOppslag.tsx index 79128faa5c0..42545b513ce 100644 --- a/apps/dolly-frontend/src/main/js/src/service/SelectOptionsOppslag.tsx +++ b/apps/dolly-frontend/src/main/js/src/service/SelectOptionsOppslag.tsx @@ -1,8 +1,6 @@ import { useAsync } from 'react-use' -import { BrregstubApi, DollyApi, KrrApi, PdlforvalterApi } from '@/service/Api' +import { BrregstubApi, DollyApi, KrrApi } from '@/service/Api' import Api from '@/api' -import { Person, PersonData } from '@/components/fagsystem/pdlf/PdlTypes' -import { getAlder } from '@/ducks/fagsystem' const uri = `/dolly-backend/api/v1` @@ -19,68 +17,7 @@ export type Option = { relasjoner?: Array } -type Data = { - label: string - id: string - navn: string - value: { - data: any - } -} - -type PersonListe = { - value: string - label: string - alder: number - sivilstand: string - vergemaal: boolean - doedsfall: boolean - foreldre: Array - foreldreansvar: Array -} - export const SelectOptionsOppslag = { - hentGruppeIdentOptions: async (gruppeId: string) => { - const gruppe = await DollyApi.getGruppeById(gruppeId).then((response: any) => { - return response.data?.identer?.map((person: PersonData) => { - if (person.master === 'PDL' || person.master === 'PDLF') { - return person.ident - } - }) - }) - if (gruppe?.length < 1) { - return null - } - const options = await PdlforvalterApi.getPersoner(gruppe).then((response: any) => { - if (gruppe?.length < 1) { - return null - } - const personListe: Array = [] - response?.data?.forEach((id: Person) => { - const navn = - id.person.navn?.length > 0 - ? `- ${id.person.navn?.[0]?.fornavn} ${id.person.navn?.[0]?.etternavn}` - : '' - personListe.push({ - value: id.person.ident, - label: `${id.person.ident} ${navn}`, - alder: getAlder(id.person.foedsel?.[0]?.foedselsdato), - sivilstand: id.person.sivilstand?.[0]?.type, - vergemaal: id.person.vergemaal?.length > 0, - doedsfall: id.person.doedsfall?.length > 0, - foreldre: id.relasjoner - ?.filter((relasjon) => relasjon.relasjonType === 'FAMILIERELASJON_FORELDER') - ?.map((relasjon) => relasjon.relatertPerson?.ident), - foreldreansvar: id.relasjoner - ?.filter((relasjon) => relasjon.relasjonType === 'FORELDREANSVAR_BARN') - ?.map((relasjon) => relasjon.relatertPerson?.ident), - }) - }) - return personListe - }) - return options || Promise.resolve() - }, - hentKrrLeverandoerer: () => { return useAsync(async () => KrrApi.getSdpLeverandoerListe(), [KrrApi.getSdpLeverandoerListe]) }, diff --git a/apps/dolly-frontend/src/main/js/src/service/services/pdl/PdlForvalterService.tsx b/apps/dolly-frontend/src/main/js/src/service/services/pdl/PdlForvalterService.tsx index e5547009be2..ee25d3016f2 100644 --- a/apps/dolly-frontend/src/main/js/src/service/services/pdl/PdlForvalterService.tsx +++ b/apps/dolly-frontend/src/main/js/src/service/services/pdl/PdlForvalterService.tsx @@ -2,30 +2,13 @@ import Request from '@/service/services/Request' const getPdlUrl = () => '/testnav-pdl-forvalter/api/v1' -const sliceIdentListe = (identListe: string[]) => { - const maxAntall = 150 - if (identListe.length <= maxAntall) return [identListe] - const identer = [] - for (let i = 0; i < identListe.length; i += maxAntall) { - identer.push(identListe.slice(i, i + maxAntall)) - } - return identer -} - export default { getPersoner(identListe: string[]) { if (!identListe) { return } - const identerDelt = sliceIdentListe(identListe) - const promises = identerDelt.map((identer) => { - const endpoint = `${getPdlUrl()}/personer?identer=${identer}` - return Request.get(endpoint) - }) - return Promise.all(promises).then((responses) => { - const data = responses.map((response: any) => response?.data).flat() - return { data } - }) + const endpoint = `${getPdlUrl()}/personer?identer=${identListe}` + return Request.get(endpoint) }, soekPersoner(fragment: string) { if (!fragment || fragment.length > 11) { diff --git a/apps/dolly-frontend/src/main/js/src/utils/hooks/useSelectOptions.tsx b/apps/dolly-frontend/src/main/js/src/utils/hooks/useSelectOptions.tsx index 3966b2e5dc8..fd12f3e32b2 100644 --- a/apps/dolly-frontend/src/main/js/src/utils/hooks/useSelectOptions.tsx +++ b/apps/dolly-frontend/src/main/js/src/utils/hooks/useSelectOptions.tsx @@ -1,5 +1,6 @@ import useSWR from 'swr' import { fetcher, multiFetcherAll } from '@/api' +import { getAlder } from '@/ducks/fagsystem' const sliceGruppe = (gruppe, maxAntall, url) => { let urlListe = [] @@ -12,20 +13,23 @@ const sliceGruppe = (gruppe, maxAntall, url) => { const multiplePdlforvalterUrl = (gruppe) => { if (!gruppe) return null - const maxAntall = 40 + const maxAntall = 150 const url = '/testnav-pdl-forvalter/api/v1/personer?' return sliceGruppe(gruppe, maxAntall, url) } const multiplePdlPersonUrl = (gruppe) => { if (!gruppe) return null - const maxAntall = 40 + const maxAntall = 150 const url = '/person-service/api/v2/personer/identer?' return sliceGruppe(gruppe, maxAntall, url) } export const usePdlOptions = (gruppe) => { - const { data, error } = useSWR([multiplePdlforvalterUrl(gruppe)], multiFetcherAll) + const { data, isLoading, error } = useSWR( + multiplePdlforvalterUrl(gruppe), + multiFetcherAll, + ) const personData = [] data?.flat().forEach((id) => { @@ -37,18 +41,28 @@ export const usePdlOptions = (gruppe) => { value: id?.person?.ident, label: `${id?.person?.ident} - ${fornavn} ${mellomnavn} ${etternavn}`, relasjoner: id?.relasjoner?.map((r) => r?.relatertPerson?.ident), + alder: getAlder(id.person.foedsel?.[0]?.foedselsdato), + sivilstand: id.person.sivilstand?.[0]?.type, + vergemaal: id.person.vergemaal?.length > 0, + doedsfall: id.person.doedsfall?.length > 0, + foreldre: id.relasjoner + ?.filter((relasjon) => relasjon.relasjonType === 'FAMILIERELASJON_FORELDER') + ?.map((relasjon) => relasjon.relatertPerson?.ident), + foreldreansvar: id.relasjoner + ?.filter((relasjon) => relasjon.relasjonType === 'FORELDREANSVAR_BARN') + ?.map((relasjon) => relasjon.relatertPerson?.ident), }) }) return { data: personData, - loading: false, + loading: isLoading, error: gruppe ? error : undefined, } } export const useTestnorgeOptions = (gruppe) => { - const { data, error } = useSWR([multiplePdlPersonUrl(gruppe)], multiFetcherAll) + const { data, error } = useSWR(multiplePdlPersonUrl(gruppe), multiFetcherAll) const getRelatertePersoner = (person) => { if (!person) {