Skip to content

Commit 288df2d

Browse files
committed
Renaming and move loading to provider
1 parent 491d107 commit 288df2d

File tree

11 files changed

+200
-76
lines changed

11 files changed

+200
-76
lines changed

packages/bygger-backend/src/services/RecipientService.ts

+1-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
1+
import { Recipient } from '@navikt/skjemadigitalisering-shared-domain';
12
import { fetchWithErrorHandling } from '../fetchUtils';
23

3-
interface Recipient {
4-
recipientId?: string;
5-
name: string;
6-
poBoxAddress: string;
7-
postalCode: string;
8-
postalName: string;
9-
createdAt?: string;
10-
createdBy?: string;
11-
changedAt?: string;
12-
changedBy?: string;
13-
}
14-
154
export class RecipientService {
165
readonly formsApiUrl: string;
176
readonly recipientsUrl: string;

packages/bygger/src/AuthenticatedApp.jsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { FormsRouter } from './Forms';
44
import ImportFormsPage from './import/ImportFormsPage';
55
import BulkPublishPage from './migration/BulkPublishPage';
66
import MigrationRouter from './migration/MigrationRouter';
7-
import MottaksadresserPage from './mottaksadresser/MottaksadresserPage';
7+
import RecipientsPage from './recipients/recipientsPage';
88
import ReportsPage from './reports/ReportsPage';
99
import TranslationsRouter from './translations/TranslationsRouter';
1010

@@ -15,7 +15,7 @@ function AuthenticatedApp({ serverURL, formio }) {
1515
<Route path="/forms/*" element={<FormsRouter formio={formio} serverURL={serverURL} />} />
1616
<Route path="/translations/*" element={<TranslationsRouter formio={formio} serverURL={serverURL} />} />
1717
<Route path="/import/skjema" element={<ImportFormsPage />} />
18-
<Route path="/mottaksadresser" element={<MottaksadresserPage />} />
18+
<Route path="/mottaksadresser" element={<RecipientsPage />} />
1919
<Route path="/migrering/*" element={<MigrationRouter />} />
2020
<Route path="/bulk-publisering" element={<BulkPublishPage />} />
2121
<Route path="/rapporter" element={<ReportsPage />} />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Recipient } from '@navikt/skjemadigitalisering-shared-domain';
2+
import { createContext, ReactNode, useCallback, useContext, useEffect, useState } from 'react';
3+
import useFormsApi from '../../hooks/useFormsApi';
4+
5+
interface RecipientsContextValues {
6+
isReady: boolean;
7+
recipients: Recipient[];
8+
}
9+
10+
const defaultValue = {
11+
isReady: false,
12+
recipients: [],
13+
};
14+
15+
const RecipientsContext = createContext<RecipientsContextValues>(defaultValue);
16+
17+
const RecipientsProvider = ({ children }: { children: ReactNode }) => {
18+
const { recipientsApi } = useFormsApi();
19+
const [isReady, setIsReady] = useState(defaultValue.isReady);
20+
const [recipients, setRecipients] = useState<Recipient[]>(defaultValue.recipients);
21+
22+
const loadRecipients = useCallback(async (): Promise<void> => {
23+
const recipients = await recipientsApi.getAll();
24+
setRecipients(recipients ?? []);
25+
}, [recipientsApi]);
26+
27+
useEffect(() => {
28+
if (!isReady) {
29+
loadRecipients().then(() => setIsReady(true));
30+
}
31+
}, [isReady, loadRecipients]);
32+
33+
const value = {
34+
isReady,
35+
recipients,
36+
};
37+
return <RecipientsContext.Provider value={value}>{children}</RecipientsContext.Provider>;
38+
};
39+
40+
export const useRecipients = () => useContext(RecipientsContext);
41+
export default RecipientsProvider;
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { http as baseHttp, useAppConfig } from '@navikt/skjemadigitalisering-shared-components';
2+
import { Recipient } from '@navikt/skjemadigitalisering-shared-domain';
3+
import { useFeedbackEmit } from '../context/notifications/FeedbackContext';
4+
5+
const useFormsApi = () => {
6+
const feedbackEmit = useFeedbackEmit();
7+
const appConfig = useAppConfig();
8+
const http = appConfig.http ?? baseHttp;
9+
10+
const getAllRecipients = async (): Promise<Recipient[] | undefined> => {
11+
try {
12+
return await http.get<Recipient[]>('/api/recipients');
13+
} catch (error) {
14+
const message = (error as Error)?.message;
15+
feedbackEmit.error(`Feil ved henting av mottakere. ${message}`);
16+
}
17+
};
18+
19+
const recipientsApi = {
20+
getAll: getAllRecipients,
21+
};
22+
23+
return {
24+
recipientsApi,
25+
};
26+
};
27+
export default useFormsApi;

packages/bygger/src/mottaksadresser/MottaksadresseTable.tsx

-42
This file was deleted.

packages/bygger/src/mottaksadresser/MottaksadresserListe.tsx

+14-20
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
import { Alert, Button, VStack } from '@navikt/ds-react';
2-
import { useState } from 'react';
31
import RowLayout from '../components/layout/RowLayout';
4-
import SidebarLayout from '../components/layout/SidebarLayout';
5-
import UserFeedback from '../components/UserFeedback';
6-
import useMottaksadresser from '../hooks/useMottaksadresser';
7-
import MottaksadresseTable from './MottaksadresseTable';
82

93
const MottaksadresserListe = () => {
10-
const { mottaksadresser, ready, errorMessage, publishMottaksadresser } = useMottaksadresser();
4+
// const { mottaksadresser, ready, errorMessage, publishMottaksadresser } = useMottaksadresser();
115
// const [editAddressId, setEditAddressId] = useState<string | undefined>(undefined);
126
// const [loadingForm, setLoadingForm] = useState<boolean>(false);
13-
const [publishing, setPublishing] = useState<boolean>(false);
7+
// const [publishing, setPublishing] = useState<boolean>(false);
148

159
// const onSubmitDone = () => {
1610
// setEditAddressId(undefined);
@@ -26,32 +20,32 @@ const MottaksadresserListe = () => {
2620
// setEditAddressId(undefined);
2721
// };
2822

29-
const onPublish = () => {
30-
setPublishing(true);
31-
publishMottaksadresser().finally(() => setPublishing(false));
32-
};
23+
// const onPublish = () => {
24+
// setPublishing(true);
25+
// publishMottaksadresser().finally(() => setPublishing(false));
26+
// };
3327

3428
return (
3529
<RowLayout
36-
right={
30+
/*right={
3731
<SidebarLayout noScroll={true}>
3832
<VStack gap="1">
3933
<Button onClick={onPublish} loading={publishing} size="small">
4034
Publiser mottaksadresser
4135
</Button>
42-
{/*<Button variant="secondary" onClick={() => editMottaksadresse('new')} type="button" size="small">*/}
43-
{/* Legg til ny*/}
44-
{/*</Button>*/}
36+
{/!*<Button variant="secondary" onClick={() => editMottaksadresse('new')} type="button" size="small">*!/}
37+
{/!* Legg til ny*!/}
38+
{/!*</Button>*!/}
4539
<UserFeedback />
4640
</VStack>
4741
</SidebarLayout>
48-
}
42+
}*/
4943
>
5044
<div>
51-
{!ready && 'Laster mottaksadresser...'}
52-
{errorMessage && <Alert variant="error">{errorMessage}</Alert>}
45+
{/*{!ready && 'Laster mottaksadresser...'}*/}
46+
{/*{errorMessage && <Alert variant="error">{errorMessage}</Alert>}*/}
5347

54-
<MottaksadresseTable mottaksadresser={mottaksadresser} />
48+
{/*<RecipientTable recipients={mottaksadresser} />*/}
5549
{/*{editAddressId === 'new' && (*/}
5650
{/* <MottaksadresseEditor*/}
5751
{/* onSubmitDone={onSubmitDone}*/}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { Table } from '@navikt/ds-react';
2+
import { Recipient } from '@navikt/skjemadigitalisering-shared-domain';
3+
4+
interface Props {
5+
recipients: Recipient[];
6+
}
7+
8+
const RecipientTable = ({ recipients }: Props) => {
9+
console.log(recipients);
10+
return (
11+
<Table>
12+
<Table.Header>
13+
<Table.Row>
14+
<Table.HeaderCell scope="col">Enhetsnavn</Table.HeaderCell>
15+
<Table.HeaderCell scope="col">Postboksadresse</Table.HeaderCell>
16+
<Table.HeaderCell scope="col">Postnr.</Table.HeaderCell>
17+
<Table.HeaderCell scope="col">Poststed</Table.HeaderCell>
18+
</Table.Row>
19+
</Table.Header>
20+
<Table.Body>
21+
{recipients.map(({ recipientId, name, poBoxAddress, postalCode, postalName }) => {
22+
return (
23+
<Table.Row key={recipientId}>
24+
<Table.HeaderCell scope="row">{name}</Table.HeaderCell>
25+
<Table.DataCell>{poBoxAddress}</Table.DataCell>
26+
<Table.DataCell>{postalCode}</Table.DataCell>
27+
<Table.DataCell>{postalName}</Table.DataCell>
28+
</Table.Row>
29+
);
30+
})}
31+
</Table.Body>
32+
</Table>
33+
);
34+
};
35+
36+
export default RecipientTable;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { VStack } from '@navikt/ds-react';
2+
import { SkeletonList } from '@navikt/skjemadigitalisering-shared-components';
3+
import RowLayout from '../components/layout/RowLayout';
4+
import SidebarLayout from '../components/layout/SidebarLayout';
5+
import UserFeedback from '../components/UserFeedback';
6+
import { useRecipients } from '../context/recipients/RecipientsContext';
7+
import RecipientTable from './RecipientTable';
8+
9+
const Recipients = () => {
10+
const { isReady, recipients } = useRecipients();
11+
12+
if (!isReady) {
13+
return (
14+
<RowLayout
15+
right={
16+
<SidebarLayout noScroll={true}>
17+
<SkeletonList size={3} height={'4rem'} />
18+
</SidebarLayout>
19+
}
20+
>
21+
<SkeletonList size={8} height={'4rem'} />
22+
</RowLayout>
23+
);
24+
}
25+
26+
return (
27+
<RowLayout
28+
right={
29+
<SidebarLayout noScroll={true}>
30+
<VStack gap="1">
31+
{/*<Button onClick={onPublish} loading={publishing} size="small">*/}
32+
{/* Publiser mottaksadresser*/}
33+
{/*</Button>*/}
34+
{/*<Button variant="secondary" onClick={() => addNewRecipient()} type="button" size="small">
35+
Legg til ny
36+
</Button>*/}
37+
<UserFeedback />
38+
</VStack>
39+
</SidebarLayout>
40+
}
41+
>
42+
<RecipientTable recipients={recipients} />
43+
</RowLayout>
44+
);
45+
};
46+
47+
export default Recipients;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { AppLayout } from '../components/AppLayout';
2+
import Title from '../components/layout/Title';
3+
import TitleRowLayout from '../components/layout/TitleRowLayout';
4+
import RecipientsProvider from '../context/recipients/RecipientsContext';
5+
import Recipients from './Recipients';
6+
7+
const RecipientsPage = () => {
8+
return (
9+
<AppLayout>
10+
<TitleRowLayout>
11+
<Title>Mottakere</Title>
12+
</TitleRowLayout>
13+
<RecipientsProvider>
14+
<Recipients />
15+
</RecipientsProvider>
16+
</AppLayout>
17+
);
18+
};
19+
export default RecipientsPage;

packages/shared-domain/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import type { FrontendLoggerConfigType, LogLevel } from './logging/types';
6060
import migrationUtils, { MigrationLevel } from './migration';
6161
import { Operator } from './migration/operator';
6262
import type { Mottaksadresse, MottaksadresseData } from './mottaksadresse';
63+
import type { Recipient } from './recipient/Recipient';
6364
import type { ReportDefinition } from './reports';
6465
import type { FormioResource, ResourceName } from './resource';
6566
import type {
@@ -199,6 +200,7 @@ export type {
199200
Panel,
200201
PrefillData,
201202
PrefillKey,
203+
Recipient,
202204
ReportDefinition,
203205
ResourceAccess,
204206
ResourceContent,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export interface Recipient {
2+
recipientId?: string;
3+
name: string;
4+
poBoxAddress: string;
5+
postalCode: string;
6+
postalName: string;
7+
createdAt?: string;
8+
createdBy?: string;
9+
changedAt?: string;
10+
changedBy?: string;
11+
}

0 commit comments

Comments
 (0)