Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1135 intégrer le nouveau composant markdown sur lensemble du module jeux de données #1144

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions src/packages/components/buttons/button.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ComponentProps, PropsWithChildren, ReactNode } from 'react';

import { Link } from '../link';
import { ExternalLink, Link } from '../link';
import './button.css';

const DEFAULT_CLASSES: string[] = [];
Expand Down Expand Up @@ -29,14 +29,9 @@ export const Button = ({
if (typeof action === 'string') {
if (externalLink) {
button = (
<a
className={className}
href={action}
rel="noopener noreferrer"
target="_blank"
>
<ExternalLink className={className} href={action}>
{content}
</a>
</ExternalLink>
);
} else {
button = (
Expand Down
42 changes: 22 additions & 20 deletions src/packages/components/errors-bloc/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,28 +54,30 @@ export const ErrorBloc = ({ error, D }: { error?: unknown; D?: any }) => {

const errors = Array.isArray(error) ? error : [error];

const formattedErrors = errors.map((e) => {
let errorMsg;
try {
const parsedError =
e !== null && typeof e === 'object' ? e : JSON.parse(e);
const formattedErrors = errors
.filter((e) => !!e)
.map((e) => {
let errorMsg;
try {
const parsedError =
e !== null && typeof e === 'object' ? e : JSON.parse(e);

if (parsedError.code && D.errors[parsedError.code]) {
errorMsg = D.errors[parsedError.code](parsedError);
} else if (parsedError.message && D.errors[parsedError.message]) {
errorMsg = D.errors[parsedError.message](parsedError);
} else if (parsedError.status === 500) {
errorMsg = NewDictionnary.errors.serversideErrors['500'](
parsedError.message,
);
} else {
errorMsg = parsedError.message;
if (parsedError.code && D.errors[parsedError.code]) {
errorMsg = D.errors[parsedError.code](parsedError);
} else if (parsedError.message && D.errors[parsedError.message]) {
errorMsg = D.errors[parsedError.message](parsedError);
} else if (parsedError.status === 500) {
errorMsg = NewDictionnary.errors.serversideErrors['500'](
parsedError.message,
);
} else {
errorMsg = parsedError.message;
}
} catch {
errorMsg = e;
}
} catch {
errorMsg = e;
}
return errorMsg;
});
return errorMsg;
});
return (
<>
{formattedErrors.map((e, index) => (
Expand Down
17 changes: 16 additions & 1 deletion src/packages/components/link/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { ComponentProps, PropsWithChildren } from 'react';
import {
ComponentProps,
ComponentPropsWithoutRef,
PropsWithChildren,
} from 'react';
import { Link as ReactLink } from 'react-router-dom';

type LinkTypes = {
Expand All @@ -21,3 +25,14 @@ export const Link = ({
</ReactLink>
);
};

export const ExternalLink = ({
children,
...props
}: ComponentPropsWithoutRef<'a'>) => {
return (
<a target="_blank" rel="noreferrer noopener" {...props}>
{children}
</a>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Link } from 'react-router-dom';

import { getDisseminationStatus } from '@components/dissemination-status/disseminationStatus';
import { Row } from '@components/layout';
import { ExternalLink } from '@components/link';
import { Note } from '@components/note';

import { stringToDate } from '@utils/date-utils';
Expand Down Expand Up @@ -151,11 +152,9 @@ const General = ({ general, secondLang }) => {
return (
<li key={fieldName}>
{`${mapping[fieldName]} : `}
<a
<ExternalLink
href={general[fieldName]}
target="_blank"
rel="noopener noreferrer"
>{`${general[fieldName]}`}</a>
>{`${general[fieldName]}`}</ExternalLink>
</li>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('Component', () => {
it('should show the loading spinner initially', () => {
// Simuler une promesse qui ne se résout pas immédiatement
CollectionApi.getCollectionList.mockImplementation(
() => new Promise(() => {}),
() => new Promise(vi.fn()),
);

render(<Component />);
Expand Down
2 changes: 1 addition & 1 deletion src/packages/modules-concepts/home.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('Component (home.tsx)', () => {
});

it('renders the loading state initially', () => {
ConceptsApi.getConceptList.mockReturnValue(new Promise(() => {}));
ConceptsApi.getConceptList.mockReturnValue(new Promise(vi.fn()));

render(<Component />);

Expand Down
7 changes: 3 additions & 4 deletions src/packages/modules-concepts/visualization/general.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DisseminationStatusVisualisation } from '@components/dissemination-status/disseminationStatus';
import { Row } from '@components/layout';
import { ExternalLink } from '@components/link';
import { Note } from '@components/note';

import { arrayToString } from '@utils/array-utils';
Expand Down Expand Up @@ -95,11 +96,9 @@ function ConceptGeneral({ attr, secondLang }) {
return (
<li key={fieldName}>
{`${mapping[fieldName]} : `}
<a
<ExternalLink
href={attr[fieldName]}
target="_blank"
rel="noopener noreferrer"
>{`${attr[fieldName]}`}</a>
>{`${attr[fieldName]}`}</ExternalLink>
</li>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('CloseMatchLinks', () => {
const linkElement = screen.getByText(link.urn);
expect(linkElement.getAttribute('href')).toBe(link.urn);
expect(linkElement.getAttribute('target')).toBe('_blank');
expect(linkElement.getAttribute('rel')).toBe('noopener noreferrer');
expect(linkElement.getAttribute('rel')).toBe('noreferrer noopener');
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ExternalLink } from '@components/link';

import { Link as LinkType } from '../../../model/concepts/concept';

export const CloseMatchLinks = ({
Expand All @@ -11,9 +13,7 @@ export const CloseMatchLinks = ({
<ul>
{links.map((cm) => (
<li key={cm.urn}>
<a href={cm.urn} target="_blank" rel="noopener noreferrer">
{cm.urn}
</a>
<ExternalLink href={cm.urn}>{cm.urn}</ExternalLink>
</li>
))}
</ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import {

describe('CollapsibleTrigger', () => {
it('should display the correct title based on the "opened" prop', () => {
render(<CollapsibleTrigger opened={true} onClick={() => {}} />);
render(<CollapsibleTrigger opened={true} onClick={vi.fn()} />);
expect(screen.getByTitle('Hide')).toBeInTheDocument();

render(<CollapsibleTrigger opened={false} onClick={() => {}} />);
render(<CollapsibleTrigger opened={false} onClick={vi.fn()} />);
expect(screen.getByTitle('Display')).toBeInTheDocument();
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import Editor from '@uiw/react-md-editor';

import { Row } from '@components/layout';
import { ExternalLink } from '@components/link';
import { Note } from '@components/note';
import { PublicationFemale } from '@components/status';

import { stringToDate } from '@utils/date-utils';
import { useSecondLang } from '@utils/hooks/second-lang';
import { renderMarkdownElement } from '@utils/html-utils';

import D, { D1, D2 } from '../../../deprecated-locales/build-dictionary';
import { Distribution } from '../../../model/Dataset';
Expand Down Expand Up @@ -38,13 +40,9 @@ export const ViewMainBlock = ({
</li>
<li>
{D.downloadUrlTitle} :{' '}
<a
target="_blank"
rel="noreferrer noopener"
href={distribution.url}
>
{distribution.url}{' '}
</a>
<ExternalLink href={distribution.url}>
{distribution.url}
</ExternalLink>
</li>
</ul>
}
Expand All @@ -54,14 +52,14 @@ export const ViewMainBlock = ({
</Row>
<Row>
<Note
text={renderMarkdownElement(distribution.descriptionLg1)}
text={<Editor.Markdown source={distribution.descriptionLg1} />}
title={D1.descriptionTitle}
alone={!secondLang}
allowEmpty={true}
/>
{secondLang && (
<Note
text={renderMarkdownElement(distribution.descriptionLg1)}
text={<Editor.Markdown source={distribution.descriptionLg2} />}
title={D2.descriptionTitle}
alone={false}
allowEmpty={true}
Expand Down
3 changes: 1 addition & 2 deletions src/packages/modules-datasets/distributions/view/view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
import { ViewMenu } from './menu';
import { ViewMainBlock } from './view-main-block';

export const Component = (props) => {
export const Component = () => {
const { id } = useParams<{ id: string }>();

const { data: distribution, isLoading } = useDistribution(id!);
Expand Down Expand Up @@ -49,7 +49,6 @@ export const Component = (props) => {
dataset={dataset}
onPublish={publish}
onDelete={remove}
{...props}
/>

<ErrorBloc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ describe('ConfirmationModal', () => {
<ConfirmationModal
isOpen={true}
document={{ sims: [] }}
onNo={() => {}}
onYes={() => {}}
onNo={vi.fn()}
onYes={vi.fn()}
/>,
);
await screen.findByText('Yes');
Expand Down
15 changes: 4 additions & 11 deletions src/packages/modules-operations/document/visualization/home.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useEffect, useState } from 'react';

import { Row } from '@components/layout';
import { ExternalLink } from '@components/link';
import { Note } from '@components/note';

import { useTitle } from '@utils/hooks/useTitle';
Expand Down Expand Up @@ -95,13 +96,9 @@ function OperationsDocumentationVisualization({
<Row>
<Note
text={
<a
href={`${baseURI}/documents/document/${id}/file`}
rel="noopener noreferrer"
target="_blank"
>
<ExternalLink href={`${baseURI}/documents/document/${id}/file`}>
{attr.labelLg1}
</a>
</ExternalLink>
}
title={D1.titleDocument}
alone={true}
Expand All @@ -112,11 +109,7 @@ function OperationsDocumentationVisualization({
{isLink(attr) && (
<Row>
<Note
text={
<a href={attr.url} rel="noopener noreferrer" target="_blank">
{attr.url}
</a>
}
text={<ExternalLink href={attr.url}>{attr.url}</ExternalLink>}
title={D1.titleLink}
alone={true}
allowEmpty={true}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('OperationsDocumentationVisualization', () => {
const a = notes[1].querySelector('a');
expect(a).toBeDefined();
expect(a.href).toContain(document.url);
expect(a).toHaveAttribute('rel', 'noopener noreferrer');
expect(a).toHaveAttribute('rel', 'noreferrer noopener');
expect(a).toHaveAttribute('target', '_blank');
expect(a.innerHTML).toContain(document.url);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('Component', () => {
const familyData = { id: '123', prefLabelLg1: 'Test Family' };
(useParams as Mock).mockReturnValue({ id: '123' });
(OperationsApi.getFamilyById as Mock).mockResolvedValueOnce(familyData);
(useGoBack as Mock).mockReturnValue(() => {});
(useGoBack as Mock).mockReturnValue(vi.fn());

render(<Component />);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('DocumentLink', () => {
'http://example.com/documents/document/123/file',
);
expect(link.getAttribute('target')).toBe('_blank');
expect(link.getAttribute('rel')).toBe('noopener noreferrer');
expect(link.getAttribute('rel')).toBe('noreferrer noopener');
});

it('renders a link with document.url if it is not a document', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ExternalLink } from '@components/link';

import { Document } from '../../../../model/operations/document';
import { getLang } from '../../../../utils/dictionnary';
import { isDocument } from '../../../document/utils';
Expand Down Expand Up @@ -51,13 +53,8 @@ export const DocumentLink = ({
const label =
document[`label${localPrefix}`] || document.labelLg1 || document.labelLg2;
return (
<a
target="_blank"
rel="noopener noreferrer"
href={uri}
title={document[`description${localPrefix}`]}
>
<ExternalLink href={uri} title={document[`description${localPrefix}`]}>
{label}
</a>
</ExternalLink>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe('DocumentsBloc', () => {
const lis = container.querySelectorAll('li');
for (let i = 0; i < lis.length; i++) {
expect(lis[i].outerHTML).toEqual(
`<li class="list-group-item documentbloc__item"><span><a target="_blank" rel="noopener noreferrer" href="${orderedList[i].url}" title="${orderedList[i].descriptionLg1}">${orderedList[i].labelLg1}</a><i>(${orderedList[i].aside})</i></span></li>`,
`<li class="list-group-item documentbloc__item"><span><a target="_blank" rel="noreferrer noopener" href="${orderedList[i].url}" title="${orderedList[i].descriptionLg1}">${orderedList[i].labelLg1}</a><i>(${orderedList[i].aside})</i></span></li>`,
);
}
});
Expand All @@ -94,7 +94,7 @@ describe('DocumentsBloc', () => {
const lis = container.querySelectorAll('li');
for (let i = 0; i < lis.length; i++) {
expect(lis[i].outerHTML).toEqual(
`<li class="list-group-item documentbloc__item"><span><a target="_blank" rel="noopener noreferrer" href="${orderedList[i].url}" title="${orderedList[i].descriptionLg2}">${orderedList[i].labelLg2}</a><i>(${orderedList[i].aside})</i></span></li>`,
`<li class="list-group-item documentbloc__item"><span><a target="_blank" rel="noreferrer noopener" href="${orderedList[i].url}" title="${orderedList[i].descriptionLg2}">${orderedList[i].labelLg2}</a><i>(${orderedList[i].aside})</i></span></li>`,
);
}
});
Expand Down
Loading