|
| 1 | +import { Alert, Heading } from "@navikt/ds-react"; |
| 2 | +import BeOmOppfolgingsplan from "@/sider/oppfolgingsplan/oppfolgingsplaner/BeOmOppfolgingsplan"; |
| 3 | +import OppfolgingsplanerOversiktLPS from "@/sider/oppfolgingsplan/lps/OppfolgingsplanerOversiktLPS"; |
| 4 | +import OppfolgingsplanLink from "@/sider/oppfolgingsplan/oppfolgingsplaner/OppfolgingsplanLink"; |
| 5 | +import React from "react"; |
| 6 | +import { erIdag, tilLesbarDatoMedArUtenManedNavn } from "@/utils/datoUtils"; |
| 7 | +import dayjs from "dayjs"; |
| 8 | +import { useLedereQuery } from "@/data/leder/ledereQueryHooks"; |
| 9 | +import { |
| 10 | + useGetOppfolgingsplanForesporselQuery, |
| 11 | + usePostOppfolgingsplanForesporsel, |
| 12 | +} from "@/data/oppfolgingsplan/oppfolgingsplanForesporselHooks"; |
| 13 | +import { useOppfolgingstilfellePersonQuery } from "@/data/oppfolgingstilfelle/person/oppfolgingstilfellePersonQueryHooks"; |
| 14 | +import { useFeatureToggles } from "@/data/unleash/unleashQueryHooks"; |
| 15 | +import { NarmesteLederRelasjonDTO } from "@/data/leder/ledereTypes"; |
| 16 | +import { OppfolgingstilfelleDTO } from "@/data/oppfolgingstilfelle/person/types/OppfolgingstilfellePersonDTO"; |
| 17 | +import { OppfolgingsplanLPSMedPersonoppgave } from "@/data/oppfolgingsplan/types/OppfolgingsplanLPS"; |
| 18 | +import { OppfolgingsplanDTO } from "@/data/oppfolgingsplan/types/OppfolgingsplanDTO"; |
| 19 | + |
| 20 | +const texts = { |
| 21 | + aktiveOppfolgingsplaner: "Aktive oppfølgingsplaner", |
| 22 | + ingenAktiveOppfolgingsplaner: "Det er ingen aktive oppfølgingsplaner", |
| 23 | + foresporselSendt: "Forespørsel om oppfølgingsplan ble sendt", |
| 24 | + aktivForesporsel: |
| 25 | + "Obs! Det ble bedt om oppfølgingsplan fra denne arbeidsgiveren", |
| 26 | +}; |
| 27 | + |
| 28 | +function activeNarmesteLederForCurrentOppfolgingstilfelle( |
| 29 | + ledere: NarmesteLederRelasjonDTO[], |
| 30 | + oppfolgingsTilfelle: OppfolgingstilfelleDTO |
| 31 | +): NarmesteLederRelasjonDTO[] { |
| 32 | + return ledere.filter( |
| 33 | + (leder) => |
| 34 | + leder.status === "INNMELDT_AKTIV" && |
| 35 | + oppfolgingsTilfelle.virksomhetsnummerList.includes( |
| 36 | + leder.virksomhetsnummer |
| 37 | + ) |
| 38 | + ); |
| 39 | +} |
| 40 | + |
| 41 | +interface Props { |
| 42 | + aktivePlaner: OppfolgingsplanDTO[]; |
| 43 | + oppfolgingsplanerLPSMedPersonoppgave: OppfolgingsplanLPSMedPersonoppgave[]; |
| 44 | +} |
| 45 | + |
| 46 | +export default function AktiveOppfolgingsplaner({ |
| 47 | + aktivePlaner, |
| 48 | + oppfolgingsplanerLPSMedPersonoppgave, |
| 49 | +}: Props) { |
| 50 | + const { toggles } = useFeatureToggles(); |
| 51 | + const { currentLedere } = useLedereQuery(); |
| 52 | + const getOppfolgingsplanForesporsel = useGetOppfolgingsplanForesporselQuery(); |
| 53 | + const postOppfolgingsplanForesporsel = usePostOppfolgingsplanForesporsel(); |
| 54 | + const { latestOppfolgingstilfelle, hasActiveOppfolgingstilfelle } = |
| 55 | + useOppfolgingstilfellePersonQuery(); |
| 56 | + const currentOppfolgingstilfelle = hasActiveOppfolgingstilfelle |
| 57 | + ? latestOppfolgingstilfelle |
| 58 | + : undefined; |
| 59 | + |
| 60 | + const oppfolgingsplanerLPSUnprocessed = oppfolgingsplanerLPSMedPersonoppgave |
| 61 | + .filter((oppfolgingsplanLPS) => { |
| 62 | + if (oppfolgingsplanLPS.personoppgave) { |
| 63 | + if (oppfolgingsplanLPS.personoppgave.behandletTidspunkt) { |
| 64 | + return ( |
| 65 | + Date.now() < |
| 66 | + dayjs(oppfolgingsplanLPS.personoppgave.behandletTidspunkt) |
| 67 | + .add(1, "days") |
| 68 | + .toDate() |
| 69 | + .getTime() |
| 70 | + ); |
| 71 | + } |
| 72 | + return !oppfolgingsplanLPS.personoppgave.behandletTidspunkt; |
| 73 | + } |
| 74 | + return erIdag(oppfolgingsplanLPS.opprettet); |
| 75 | + }) |
| 76 | + .sort((a, b) => { |
| 77 | + return new Date(b.opprettet).getTime() - new Date(a.opprettet).getTime(); |
| 78 | + }); |
| 79 | + |
| 80 | + const hasActivePlan = |
| 81 | + aktivePlaner.length !== 0 || oppfolgingsplanerLPSUnprocessed.length !== 0; |
| 82 | + |
| 83 | + const activeNarmesteLedere = !!currentOppfolgingstilfelle |
| 84 | + ? activeNarmesteLederForCurrentOppfolgingstilfelle( |
| 85 | + currentLedere, |
| 86 | + currentOppfolgingstilfelle |
| 87 | + ) |
| 88 | + : []; |
| 89 | + const activeNarmesteLederIfSingle = |
| 90 | + activeNarmesteLedere.length === 1 ? activeNarmesteLedere[0] : undefined; |
| 91 | + const isBeOmOppfolgingsplanVisible = |
| 92 | + toggles.isBeOmOppfolgingsplanEnabled && |
| 93 | + !hasActivePlan && |
| 94 | + !!currentOppfolgingstilfelle && |
| 95 | + !!activeNarmesteLederIfSingle && |
| 96 | + !postOppfolgingsplanForesporsel.isSuccess; |
| 97 | + |
| 98 | + const lastForesporselCreatedAt = |
| 99 | + getOppfolgingsplanForesporsel.data?.[0]?.createdAt; |
| 100 | + |
| 101 | + const isAktivForesporsel = |
| 102 | + !!lastForesporselCreatedAt && |
| 103 | + !!currentOppfolgingstilfelle && |
| 104 | + !postOppfolgingsplanForesporsel.isSuccess |
| 105 | + ? currentOppfolgingstilfelle.start <= lastForesporselCreatedAt && |
| 106 | + lastForesporselCreatedAt <= currentOppfolgingstilfelle.end |
| 107 | + : false; |
| 108 | + const aktivForesporselTekst = `${ |
| 109 | + texts.aktivForesporsel |
| 110 | + } ${tilLesbarDatoMedArUtenManedNavn(lastForesporselCreatedAt)}`; |
| 111 | + |
| 112 | + return ( |
| 113 | + <div className="mb-8"> |
| 114 | + <Heading spacing level="2" size="medium"> |
| 115 | + {texts.aktiveOppfolgingsplaner} |
| 116 | + </Heading> |
| 117 | + {!hasActivePlan && |
| 118 | + !postOppfolgingsplanForesporsel.isSuccess && |
| 119 | + !isAktivForesporsel && ( |
| 120 | + <Alert variant="info" className="mb-4"> |
| 121 | + <p>{texts.ingenAktiveOppfolgingsplaner}</p> |
| 122 | + </Alert> |
| 123 | + )} |
| 124 | + {isAktivForesporsel && ( |
| 125 | + <Alert variant="warning" className="mb-2"> |
| 126 | + {aktivForesporselTekst} |
| 127 | + </Alert> |
| 128 | + )} |
| 129 | + {postOppfolgingsplanForesporsel.isSuccess && ( |
| 130 | + <Alert variant="success" className="mb-4"> |
| 131 | + {texts.foresporselSendt} |
| 132 | + </Alert> |
| 133 | + )} |
| 134 | + {isBeOmOppfolgingsplanVisible && ( |
| 135 | + <BeOmOppfolgingsplan |
| 136 | + aktivNarmesteLeder={activeNarmesteLederIfSingle} |
| 137 | + postOppfolgingsplanForesporsel={postOppfolgingsplanForesporsel} |
| 138 | + /> |
| 139 | + )} |
| 140 | + {oppfolgingsplanerLPSUnprocessed.map((planLPS, index) => { |
| 141 | + return ( |
| 142 | + <OppfolgingsplanerOversiktLPS |
| 143 | + key={index} |
| 144 | + oppfolgingsplanLPSBistandsbehov={planLPS} |
| 145 | + /> |
| 146 | + ); |
| 147 | + })} |
| 148 | + {aktivePlaner.map((dialog, index) => { |
| 149 | + return <OppfolgingsplanLink key={index} dialog={dialog} />; |
| 150 | + })} |
| 151 | + </div> |
| 152 | + ); |
| 153 | +} |
0 commit comments