Skip to content

Commit

Permalink
Notat i sak fix (#5163)
Browse files Browse the repository at this point in the history
* Flytter typer

* Viser bare aktive notater i varselboble
Logikk på om notat kan redigeres

* Formatterer årstall kortere

* Laget apiPaths for å samle api-endepunkter
Rydding i kode
  • Loading branch information
hallvardastark authored Nov 9, 2023
1 parent 2bc89ec commit d6a2472
Show file tree
Hide file tree
Showing 17 changed files with 165 additions and 122 deletions.
1 change: 1 addition & 0 deletions packages/rest-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export { default as AbstractRequestApi } from './src/requestApi/AbstractRequestA
export { ErrorTypes, errorOfType, getErrorResponseData } from './src/requestApi/error/ErrorTypes';
export type { default as ErrorType } from './src/requestApi/error/errorTsType';
export type { default as Link } from './src/requestApi/LinkTsType';
export { default as apiPaths } from './src/requestApi/apiPaths';

let isUnitTestModeOn = false;
export const switchOnTestMode = () => {
Expand Down
5 changes: 5 additions & 0 deletions packages/rest-api/src/requestApi/apiPaths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const apiPaths = {
notatISak: '/k9/sak/api/notat',
};

export default apiPaths;
35 changes: 15 additions & 20 deletions packages/sak-app/src/behandlingsupport/BehandlingSupportIndex.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import SupportMenySakIndex, { SupportTabs } from '@fpsak-frontend/sak-support-meny';
import { httpErrorHandler, useLocalStorage } from '@fpsak-frontend/utils';
import { httpErrorHandler } from '@fpsak-frontend/utils';
import { apiPaths } from '@k9-sak-web/rest-api';
import { useRestApiErrorDispatcher } from '@k9-sak-web/rest-api-hooks';
import {
ArbeidsgiverOpplysningerWrapper,
BehandlingAppKontekst,
Fagsak,
FeatureToggles,
NavAnsatt,
NotatResponse,
Personopplysninger,
} from '@k9-sak-web/types';
import axios from 'axios';
import React, { useCallback, useMemo, useState } from 'react';
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { useQuery } from 'react-query';
import { useNavigate } from 'react-router-dom';
import { getSupportPanelLocationCreator } from '../app/paths';
Expand Down Expand Up @@ -83,34 +85,33 @@ const BehandlingSupportIndex = ({
}: OwnProps) => {
const { addErrorMessage } = useRestApiErrorDispatcher();
const [antallUlesteNotater, setAntallUlesteNotater] = useState(0);
const [lesteNotater] = useLocalStorage('lesteNotater', []);

const getNotater = (signal: AbortSignal) => {
const getNotater = (signal: AbortSignal) =>
axios
.get(`/k9/sak/api/notat`, {
.get<NotatResponse[]>(apiPaths.notatISak, {
signal,
params: {
saksnummer: fagsak.saksnummer,
},
})
.then(response => {
const ulesteNotater = response.data.filter(
notat => lesteNotater.findIndex(lestNotatId => lestNotatId === notat.notatId) === -1,
);
setAntallUlesteNotater(ulesteNotater.length);
})
.then(({ data }) => data)
.catch(error => {
httpErrorHandler(error?.response?.status, addErrorMessage, error?.response?.headers?.location);
});
};

const notaterQueryKey = ['notater', fagsak?.saksnummer];
useQuery({
const { data: notater } = useQuery({
queryKey: notaterQueryKey,
queryFn: ({ signal }) => getNotater(signal),
enabled: featureToggles?.NOTAT_I_SAK && !!fagsak,
refetchOnWindowFocus: false,
});

useEffect(() => {
const ulesteNotater = (notater || []).filter(notat => !notat.skjult);
setAntallUlesteNotater(ulesteNotater?.length);
}, [notater]);

const { selected: valgtSupportPanel, location } = useTrackRouteParam<string>({
paramName: 'stotte',
isQueryParam: true,
Expand Down Expand Up @@ -193,13 +194,7 @@ const BehandlingSupportIndex = ({
/>
)}
{aktivtSupportPanel === SupportTabs.NOTATER && featureToggles?.NOTAT_I_SAK && (
<NotaterIndex
saksnummer={fagsak.saksnummer}
behandlingId={behandlingId}
behandlingVersjon={behandlingVersjon}
fagsakPerson={fagsak.person}
navAnsatt={navAnsatt}
/>
<NotaterIndex navAnsatt={navAnsatt} fagsak={fagsak} />
)}
</div>
</>
Expand Down
28 changes: 17 additions & 11 deletions packages/sak-app/src/behandlingsupport/notater/NotaterIndex.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
import { LoadingPanel, requireProps } from '@fpsak-frontend/shared-components';
import Notater from '@k9-sak-web/sak-notat';
import { FagsakPerson, NavAnsatt } from '@k9-sak-web/types';
import { Fagsak, NavAnsatt } from '@k9-sak-web/types';
import React from 'react';
import ErrorBoundary from '@k9-sak-web/sak-app/src/app/ErrorBoundary';
import { useRestApiErrorDispatcher } from '@k9-sak-web/rest-api-hooks';

interface OwnProps {
saksnummer: string;
behandlingId?: number;
behandlingVersjon?: number;
fagsakPerson?: FagsakPerson;
navAnsatt: NavAnsatt;
fagsak: Fagsak;
}

const EMPTY_ARRAY = [];

/**
* NotaterIndex
*
* Container komponent. Har ansvar for å vise notater i saken.
*/
export const NotaterIndex = ({ saksnummer, navAnsatt }: OwnProps) => (
<Notater fagsakId={saksnummer} navAnsatt={navAnsatt} />
);
export const NotaterIndex = ({ fagsak, navAnsatt }: OwnProps) => {
const { addErrorMessage } = useRestApiErrorDispatcher();

export default requireProps(['saksnummer'], <LoadingPanel />)(NotaterIndex);
return (
<ErrorBoundary errorMessageCallback={addErrorMessage}>
<Notater
fagsakId={fagsak.saksnummer}
navAnsatt={navAnsatt}
fagsakHarPleietrengende={!!fagsak.pleietrengendeAktørId}
/>
</ErrorBoundary>
);
};
export default requireProps(['fagsak'], <LoadingPanel />)(NotaterIndex);
1 change: 1 addition & 0 deletions packages/sak-notat/i18n/nb_NO.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"NotatISakIndex.VisNotatTilknyttetPleietrengende": "Vis notat i alle saker tilknyttet pleietrengende",
"NotatISakIndex.LeggTilNotatButton": "Legg til notat",
"NotatISakIndex.IngenNotaterAlert": "Ingen notater er publisert i saken",
"NotatISakIndex.IngenAktiveNotaterAlert": "Ingen aktive notater i saken",
"NotatISakIndex.NotaterISak": "Notater i sak",
"NotatISakIndex.VisSkjulteNotater": "Vis skjulte notater",
"NotatISakIndex.SkrivNyttNotat": "Skriv et nytt notat",
Expand Down
1 change: 1 addition & 0 deletions packages/sak-notat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"private": true,
"dependencies": {
"@fpsak-frontend/utils": "1.0.0",
"@k9-sak-web/rest-api": "1.0.0",
"@k9-sak-web/types": "1.0.0",
"@navikt/aksel-icons": "5.8.0",
"@navikt/ds-react": "5.8.0",
Expand Down
48 changes: 29 additions & 19 deletions packages/sak-notat/src/Notater.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { NavAnsatt } from '@k9-sak-web/types';
import { NavAnsatt, NotatResponse } from '@k9-sak-web/types';
import { Alert, Button, Heading, Loader, Switch } from '@navikt/ds-react';
import { CheckboxField, Form, TextAreaField } from '@navikt/ft-form-hooks';
import React, { useState } from 'react';
import { UseFormReturn } from 'react-hook-form';
import { FormattedMessage, RawIntlProvider, createIntl, createIntlCache } from 'react-intl';
import messages from '../i18n/nb_NO.json';
import ChatComponent from './components/ChatComponent';
import { NotatResponse } from './types/NotatResponse';
import styles from './notater.module.css';

const cache = createIntlCache();
Expand Down Expand Up @@ -41,6 +40,7 @@ interface NotaterProps {
notater: NotatResponse[];
postNotatMutationError: boolean;
formMethods: UseFormReturn<Inputs, any, undefined>;
fagsakHarPleietrengende: boolean;
}

const Notater: React.FunctionComponent<NotaterProps> = ({
Expand All @@ -53,6 +53,7 @@ const Notater: React.FunctionComponent<NotaterProps> = ({
postNotatMutationError,
submitSkjulNotat,
formMethods,
fagsakHarPleietrengende,
}) => {
const [visSkjulteNotater, setVisSkjulteNotater] = useState(false);

Expand All @@ -62,6 +63,8 @@ const Notater: React.FunctionComponent<NotaterProps> = ({

const submit = (data: Inputs) => submitNotat(data);

const alleNotaterErSkjulte = notater?.every(notat => notat.skjult);

return (
<RawIntlProvider value={intl}>
{isLoading ? (
Expand All @@ -76,11 +79,35 @@ const Notater: React.FunctionComponent<NotaterProps> = ({
<FormattedMessage id="NotatISakIndex.VisSkjulteNotater" />
</Switch>
</div>
<Form<Inputs> formMethods={formMethods} onSubmit={submit}>
<div className={styles.nyttNotat}>
<TextAreaField
name="notatTekst"
size="small"
label={<FormattedMessage id="NotatISakIndex.SkrivNyttNotat" />}
/>
</div>
{fagsakHarPleietrengende && (
<CheckboxField
className={styles.visAlleNotater}
name="visNotatIAlleSaker"
label={<FormattedMessage id="NotatISakIndex.VisNotatTilknyttetPleietrengende" />}
/>
)}
<Button type="submit" className={styles.leggTilNotatKnapp} size="small" variant="primary">
<FormattedMessage id="NotatISakIndex.LeggTilNotatButton" />
</Button>
</Form>
{!hasGetNotaterError && notater?.length === 0 && (
<Alert className={styles.alert} size="small" variant="info">
<FormattedMessage id="NotatISakIndex.IngenNotaterAlert" />
</Alert>
)}
{alleNotaterErSkjulte && !visSkjulteNotater && notater?.length > 0 && (
<Alert className={styles.alert} size="small" variant="info">
<FormattedMessage id="NotatISakIndex.IngenAktiveNotaterAlert" />
</Alert>
)}
{hasGetNotaterError && (
<Alert className={styles.alert} size="small" variant="error">
<FormattedMessage id="NotatISakIndex.NoeGikkGaltHentingNotater" />
Expand All @@ -107,23 +134,6 @@ const Notater: React.FunctionComponent<NotaterProps> = ({
))}
</div>
)}
<Form<Inputs> formMethods={formMethods} onSubmit={submit}>
<div className={styles.nyttNotat}>
<TextAreaField
name="notatTekst"
size="small"
label={<FormattedMessage id="NotatISakIndex.SkrivNyttNotat" />}
/>
</div>
<CheckboxField
className={styles.visAlleNotater}
name="visNotatIAlleSaker"
label={<FormattedMessage id="NotatISakIndex.VisNotatTilknyttetPleietrengende" />}
/>
<Button type="submit" className={styles.leggTilNotatKnapp} size="small" variant="primary">
<FormattedMessage id="NotatISakIndex.LeggTilNotatButton" />
</Button>
</Form>
</>
)}
</RawIntlProvider>
Expand Down
59 changes: 10 additions & 49 deletions packages/sak-notat/src/NotaterIndex.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { useLocalStorage } from '@fpsak-frontend/utils';
import { NavAnsatt } from '@k9-sak-web/types';
import axios from 'axios';
import React from 'react';
import { useForm } from 'react-hook-form';
import { useMutation, useQuery, useQueryClient } from 'react-query';
import Notater, { Inputs, skjulNotatMutationVariables } from './Notater';
import { NotatGjelderType } from './types/NotatGjelderType';
import { NotatResponse } from './types/NotatResponse';
import { getNotater, postNotat, skjulNotat } from './notatApi';

interface NotaterIndexProps {
fagsakId: string;
navAnsatt: NavAnsatt;
fagsakHarPleietrengende: boolean;
}

interface postNotatMutationVariables {
Expand All @@ -20,8 +18,7 @@ interface postNotatMutationVariables {
versjon?: number;
}

const NotaterIndex: React.FC<NotaterIndexProps> = ({ fagsakId, navAnsatt }) => {
const [lesteNotater, setLesteNotater] = useLocalStorage<number[]>('lesteNotater', []);
const NotaterIndex: React.FC<NotaterIndexProps> = ({ fagsakId, navAnsatt, fagsakHarPleietrengende }) => {
const queryClient = useQueryClient();

const notaterQueryKey = ['notater', fagsakId];
Expand All @@ -33,48 +30,19 @@ const NotaterIndex: React.FC<NotaterIndexProps> = ({ fagsakId, navAnsatt }) => {
},
});

const getNotater = (signal: AbortSignal) =>
axios
.get<NotatResponse[]>(`/k9/sak/api/notat`, {
signal,
params: {
saksnummer: fagsakId,
},
})
.then(({ data }) => {
const sorterteNotater = [...data].sort(
(notatA, notatB) => +new Date(notatA.opprettetTidspunkt) - +new Date(notatB.opprettetTidspunkt),
);
setLesteNotater([
...new Set([...lesteNotater, ...data.filter(notat => !notat.skjult).map(notat => notat.notatId)]),
]);
return sorterteNotater;
});

const {
isLoading: getNotaterLoading,
isError: hasGetNotaterError,
data: notater,
} = useQuery({ queryKey: notaterQueryKey, queryFn: ({ signal }) => getNotater(signal), enabled: !!fagsakId });

const postNotat = (data: Inputs, id?: number, fagsakIdFraRedigertNotat?: string, versjon?: number) => {
let notatGjelderType;
if (!id) {
notatGjelderType = data.visNotatIAlleSaker ? NotatGjelderType.pleietrengende : NotatGjelderType.fagsak;
}
const postUrl = id ? '/k9/sak/api/notat/endre' : '/k9/sak/api/notat';
return axios.post(postUrl, {
notatTekst: data.notatTekst,
saksnummer: fagsakIdFraRedigertNotat || fagsakId,
notatGjelderType,
versjon: versjon || 0,
notatId: id,
});
};
} = useQuery({
queryKey: notaterQueryKey,
queryFn: ({ signal }) => getNotater(signal, fagsakId),
enabled: !!fagsakId,
});

const postNotatMutation = useMutation(
({ data, id, fagsakIdFraRedigertNotat, versjon }: postNotatMutationVariables) =>
postNotat(data, id, fagsakIdFraRedigertNotat, versjon),
postNotat(data, fagsakId, id, fagsakIdFraRedigertNotat, versjon),
{
onSuccess: () => {
formMethods.reset();
Expand All @@ -83,14 +51,6 @@ const NotaterIndex: React.FC<NotaterIndexProps> = ({ fagsakId, navAnsatt }) => {
},
);

const skjulNotat = (skjul: boolean, id: number, saksnummer: string, versjon: number) =>
axios.post('/k9/sak/api/notat/skjul', {
notatId: id,
skjul,
saksnummer,
versjon,
});

const skjulNotatMutation = useMutation(
({ skjul, id, saksnummer, versjon }: skjulNotatMutationVariables) => skjulNotat(skjul, id, saksnummer, versjon),
{
Expand Down Expand Up @@ -118,6 +78,7 @@ const NotaterIndex: React.FC<NotaterIndexProps> = ({ fagsakId, navAnsatt }) => {
postNotatMutationError={postNotatMutation.isError}
submitSkjulNotat={submitSkjulNotat}
formMethods={formMethods}
fagsakHarPleietrengende={fagsakHarPleietrengende}
/>
);
};
Expand Down
Loading

0 comments on commit d6a2472

Please sign in to comment.