Skip to content

Commit

Permalink
Merge pull request #1114 from InseeFr/1108-le-tableau-des-codes-nest-…
Browse files Browse the repository at this point in the history
…pas-mis-à-jour-dans-le-formulaire-de-modification

1108 le tableau des codes nest pas mis à jour dans le formulaire de modification
  • Loading branch information
PierreVasseur authored Jan 9, 2025
2 parents 820c4f0 + e8ac35a commit 7a36132
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 49 deletions.
5 changes: 5 additions & 0 deletions src/packages/model/CodesList.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
export interface Code {
iri: string;
labelLg1: string;
labelLg2: string;
code: string;
lastCodeUriSegment: string;
broader?: string[];
narrower?: string[];
closeMatch?: string[];
}

export interface CodesList {
Expand Down
4 changes: 2 additions & 2 deletions src/packages/modules-codelists/apis/codelists-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,6 @@ const api = {
{},
() => Promise.resolve(),
],
} as const;
};

export default buildApi('codeList', api);
export default buildApi('codeList', api) as any;
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Column } from 'primereact/column';
import { useEffect, useState } from 'react';

import { DataTable } from '@components/datatable';
import { SeeButton } from '@components/buttons/see';
import {
ClientSideError,
GlobalClientSideErrorBloc,
Expand All @@ -18,6 +17,7 @@ import { CollapsiblePanel } from '../collapsible-panel';
import { CodeSlidingPanelMenu } from './code-sliding-panel-menu';
import { CodesPanelAddButton } from './codes-panel-add-button';
import './codes-panel.scss';
import { Table } from './codes/table';

const CodeSlidingPanel = ({
code: initialCode,
Expand Down Expand Up @@ -187,13 +187,18 @@ export const CodesCollapsiblePanel = ({ codelist, hidden, editable }) => {

const [loading, setLoading] = useState(true);

useEffect(() => {
const fetchCodes = () => {
setLoading(true);

API.getCodesDetailedCodelist(codelist.id, (lazyState.page ?? 0) + 1)
.then((cl) => {
setCodes(cl ?? {});
})
.finally(() => setLoading(false));
};

useEffect(() => {
fetchCodes();
}, [codelist.id, lazyState.page]);

const [openPanel, setOpenPanel] = useState(false);
Expand All @@ -213,27 +218,23 @@ export const CodesCollapsiblePanel = ({ codelist, hidden, editable }) => {
actions: (
<>
{editable && (
<button
type="button"
className="btn btn-default"
<SeeButton
data-component-id={code.code}
onClick={() => {
setSelectedCode(code);
setOpenPanel(true);
}}
aria-label={D.see}
title={D.see}
>
<span className="glyphicon glyphicon-eye-open"></span>
</button>
></SeeButton>
)}
{editable && (
<button
type="button"
className="btn btn-default"
data-component-id={code.code}
onClick={() => {
API.deleteCodesDetailedCodelist(codelist.id, code);
API.deleteCodesDetailedCodelist(codelist.id, code).then(() =>
fetchCodes(),
);
}}
aria-label={D.remove}
title={D.remove}
Expand All @@ -246,9 +247,6 @@ export const CodesCollapsiblePanel = ({ codelist, hidden, editable }) => {
};
});

const onPage = (e) => {
setlazyState(e);
};
return (
<Row>
<CollapsiblePanel
Expand Down Expand Up @@ -292,34 +290,13 @@ export const CodesCollapsiblePanel = ({ codelist, hidden, editable }) => {
</div>
</Row>

<DataTable
loadind={loading}
lazy
first={lazyState.first}
rows={lazyState.rows}
withPagination={codes.total > 10}
rowsPerPageOptions={[10]}
totalRecords={codes.total}
value={codesWithActions}
onPage={onPage}
>
<Column field="code" header={D1.codeTitle}></Column>

<Column field="labelLg1" header={D1.codeLabel}></Column>

<Column field="labelLg2" header={D2.codeLabel}></Column>

<Column field="broader" header={D1.codelistBroader}></Column>

<Column field="narrower" header={D1.codelistNarrower}></Column>

<Column
field="closeMatch"
header={D1.codelistCloseMatch}
></Column>

<Column field="actions" header=""></Column>
</DataTable>
<Table
codesWithActions={codesWithActions}
loading={loading}
onPage={setlazyState}
total={codes.total}
state={lazyState}
/>
</>
}
/>
Expand All @@ -345,10 +322,12 @@ export const CodesCollapsiblePanel = ({ codelist, hidden, editable }) => {
promise = API.putCodesDetailedCodelist;
}

promise(codelist.id, code).then(() => {
setSelectedCode({});
setOpenPanel(false);
});
promise(codelist.id, code)
.then(() => fetchCodes())
.then(() => {
setSelectedCode({});
setOpenPanel(false);
});
}}
></CodeSlidingPanel>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { render, screen } from '@testing-library/react';
import { vi } from 'vitest';

import { Table, TableTypes } from './table';

const mockData = [
{
code: '001',
labelLg1: 'Label 1',
labelLg2: 'Étiquette 1',
broader: 'Parent 1',
narrower: 'Child 1',
iri: 'iri 1',
closeMatch: 'Close Match 1',
actions: <button>Action 1</button>,
},
{
code: '002',
labelLg1: 'Label 2',
labelLg2: 'Étiquette 2',
broader: 'Parent 2',
narrower: 'Child 2',
iri: 'iri 2',

closeMatch: 'Close Match 2',
actions: <button>Action 2</button>,
},
];

describe('Table Component', () => {
it('renders the table with data', () => {
render(
<Table
loading={false}
codesWithActions={mockData as unknown as TableTypes['codesWithActions']}
total={2}
state={{ first: 0, rows: 10 }}
onPage={vi.fn()}
/>,
);

screen.getByText('Label 1');
screen.getByText('Étiquette 1');
screen.getByText('Parent 1');
screen.getByText('Child 1');
screen.getByText('Close Match 1');

screen.getByText('Label 2');
screen.getByText('Étiquette 2');
screen.getByText('Parent 2');
screen.getByText('Child 2');
screen.getByText('Close Match 2');
});

it('renders loading state', () => {
const { container } = render(
<Table
loading={true}
codesWithActions={[] as unknown as TableTypes['codesWithActions']}
total={0}
state={{ first: 0, rows: 10 }}
onPage={vi.fn()}
/>,
);

expect(container.querySelector('.p-datatable-loading-icon')).not.toBeNull();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Code } from '@model/CodesList';
import { Column } from 'primereact/column';
import { DataTableStateEvent } from 'primereact/datatable';

import { DataTable } from '@components/datatable';

import { D1, D2 } from '../../../i18n/build-dictionary';

export interface TableTypes {
loading: boolean;
codesWithActions: Omit<Code, 'broader' | 'narrower' | 'closeMatch'> &
{
broader: string;
narrower: string;
closeMatch: string;
actions: JSX.Element;
}[];
total: number;
state: {
first: number;
rows: number;
};
onPage: (event: DataTableStateEvent) => void;
}

export const Table = ({
codesWithActions,
loading,
state,
total,
onPage,
}: Readonly<TableTypes>) => {
return (
<DataTable
loading={loading}
lazy
first={state.first}
rows={state.rows}
withPagination={total > 10}
rowsPerPageOptions={[10]}
totalRecords={total}
value={codesWithActions}
onPage={onPage}
>
<Column field="code" header={D1.codeTitle}></Column>

<Column field="labelLg1" header={D1.codeLabel}></Column>

<Column field="labelLg2" header={D2.codeLabel}></Column>

<Column field="broader" header={D1.codelistBroader}></Column>

<Column field="narrower" header={D1.codelistNarrower}></Column>

<Column field="closeMatch" header={D1.codelistCloseMatch}></Column>

<Column field="actions" header=""></Column>
</DataTable>
);
};

0 comments on commit 7a36132

Please sign in to comment.