Skip to content

Commit

Permalink
finish generalisation error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelDemey committed Jan 21, 2025
1 parent 9344801 commit 465ce7d
Show file tree
Hide file tree
Showing 52 changed files with 93 additions and 199 deletions.
12 changes: 4 additions & 8 deletions src/packages/components/errors-bloc/errors-bloc.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,19 @@ describe('ClientSideError', () => {

describe('GlobalClientSideErrorBloc', () => {
it('renders global error message when clientSideErrors are provided', () => {
render(
<GlobalClientSideErrorBloc clientSideErrors={['error1']} D={mockD} />,
);
render(<GlobalClientSideErrorBloc clientSideErrors={['error1']} />);
const errorElement = screen.getByRole('alert');
expect(errorElement).toHaveTextContent(
'A global client-side error occurred.',
);
expect(errorElement).toHaveTextContent('You have errors in this form.');
});

it('does not render anything when clientSideErrors is undefined', () => {
render(<GlobalClientSideErrorBloc D={mockD} />);
render(<GlobalClientSideErrorBloc />);
const errorElement = screen.queryByRole('alert');
expect(errorElement).toBeNull();
});

it('does not render anything when clientSideErrors is an empty array', () => {
render(<GlobalClientSideErrorBloc clientSideErrors={[]} D={mockD} />);
render(<GlobalClientSideErrorBloc clientSideErrors={[]} />);
const errorElement = screen.queryByRole('alert');
expect(errorElement).toBeNull();
});
Expand Down
25 changes: 19 additions & 6 deletions src/packages/components/errors-bloc/index.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
import NewDictionnary from '../../i18n';
import D from '../../i18n';
import './errors-bloc.css';

/**
* Component used next to an form input.
* Inside this component, we will display the client-side
* error of the corresponding input.
*/
export const ClientSideError = ({
error,
id,
}: Readonly<{
error?: string;
id: string;
}>) => {
return error ? (
if (!error) {
return null;
}
return (
<div
id={id}
className="text-danger"
dangerouslySetInnerHTML={{ __html: error }}
></div>
) : null;
);
};

export const GlobalClientSideErrorBloc = ({
clientSideErrors,
D,
}: Readonly<{
clientSideErrors?: string[];
D: any;
}>) => {
if (!clientSideErrors) {
return null;
Expand All @@ -31,14 +39,19 @@ export const GlobalClientSideErrorBloc = ({
{(
<div
dangerouslySetInnerHTML={{
__html: D.errors.GlobalClientSideErrorBloc,
__html: D.errors.globalClientSideErrorBloc,
}}
/>
) || <span style={{ whiteSpace: 'pre-wrap' }}> </span>}
</div>
) : null;
};
export const ErrorBloc = ({ error, D }: { error: unknown; D?: any }) => {

export const ErrorBloc = ({ error, D }: { error?: unknown; D?: any }) => {
if (!error) {
return null;
}

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

const formattedErrors = errors.map((e) => {
Expand Down
4 changes: 2 additions & 2 deletions src/packages/components/picker-page/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ export const Picker = ({
<div className="container">
<PageTitle title={title} />
{controls}
{clientSideErrors && <ErrorBloc error={clientSideErrors} />}
{disabled && <ErrorBloc error={disabledWarningMessage} />}
<ErrorBloc error={clientSideErrors} />
<ErrorBloc error={disabledWarningMessage} />
<Row>
<div className="col-md-6">
<Panel title={panelTitle}>{addedEls}</Panel>
Expand Down
4 changes: 0 additions & 4 deletions src/packages/deprecated-locales/dictionary/errors.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
const messages = {
errors: {
GlobalClientSideErrorBloc: {
fr: 'Vous avez des erreurs dans ce formulaire.',
en: 'You have errors in this form.',
},
'406_OPERATION_DOCUMENT_OPERATION_DOCUMENT_LINK_EXISTING_LABEL_LG1': {
fr: () =>
'La propriété <strong>Intitulé</strong> est déjà utilisée par un autre document ou lien.',
Expand Down
4 changes: 4 additions & 0 deletions src/packages/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ const dictionary = {
},
},
errors: {
globalClientSideErrorBloc: {
fr: 'Vous avez des erreurs dans ce formulaire.',
en: 'You have errors in this form.',
},
serversideErrors: {
500: {
fr: (error: string) =>
Expand Down
5 changes: 1 addition & 4 deletions src/packages/modules-classifications/edition/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,7 @@ export const Component = () => {
<SaveButton type="submit"></SaveButton>
</ActionToolbar>

<GlobalClientSideErrorBloc
clientSideErrors={Object.values(errors)}
D={D}
/>
<GlobalClientSideErrorBloc clientSideErrors={Object.values(errors)} />

<Row>
<div className="col-md-6 form-group">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export const Component = () => {
<CancelButton action="/classifications" type="button"></CancelButton>
<SaveButton type="submit"></SaveButton>
</ActionToolbar>
{errorMessage && <ErrorBloc error={errorMessage} />}
<ErrorBloc error={errorMessage} />
<Row>
<div className="col-md-6 form-group">
<LabelRequired htmlFor="prefLabelLg1">{D1.title}</LabelRequired>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useParams } from 'react-router-dom';

import { Loading } from '@components/loading';
import { Loading, Publishing } from '@components/loading';

import { useSecondLang } from '../../utils/hooks/second-lang';
import { useClassification, usePublishClassification } from '../hooks';
Expand All @@ -17,7 +17,7 @@ export const Component = () => {
return <Loading />;
}

if (isPublishing) return <Loading text="publishing" />;
if (isPublishing) return <Publishing />;

if (!classification) return <Loading />;
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const ClassificationVisualization = ({
</Row>
<Menu classification={general} publish={publish} />
<CheckSecondLang />
{serverSideError && <ErrorBloc error={serverSideError} D={D} />}
<ErrorBloc error={serverSideError} D={D} />
<General general={general} secondLang={secondLang} />
{notes.scopeNoteLg1 && <Notes notes={notes} secondLang={secondLang} />}
{levels.length !== 0 && (
Expand Down
2 changes: 1 addition & 1 deletion src/packages/modules-codelists/apis/codelists-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const api = {
getCodelistsPartial: () => ['partial'],
getCodelistPartial: (id: string) => [`partial/${id}`],
publishPartialCodelist: (id: string) => [
`partial/validate/${id}`,
`partial/${id}/validate`,
{ method: 'PUT' },
(res: Response) => res.text(),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,9 @@ export const CodeDetailEdit = ({
{submitting && clientSideErrors && (
<GlobalClientSideErrorBloc
clientSideErrors={clientSideErrors.errorMessage}
D={D}
/>
)}
{serverSideError && <ErrorBloc error={serverSideError} D={D} />}
<ErrorBloc error={serverSideError} D={D} />
<div>
<Row>
<div className="col-md-12 form-group">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ export const CodeDetailView = ({
<ActionToolbar>
<ReturnButton action={handleBack} col={col} />
</ActionToolbar>
{serverSideError && <ErrorBloc error={serverSideError} />}

<ErrorBloc error={serverSideError} />
<Row>
<Note
text={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ const CodeSlidingPanel = ({
{submitting && clientSideErrors && (
<GlobalClientSideErrorBloc
clientSideErrors={clientSideErrors.errorMessage}
D={D}
/>
)}
<Row>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState, useEffect, useCallback, useContext } from 'react';
import { useParams, useNavigate, useLocation } from 'react-router-dom';

import { Loading } from '@components/loading';
import { Loading, Saving } from '@components/loading';

import { useStampsOptions } from '@utils/hooks/stamps';

Expand Down Expand Up @@ -80,7 +80,7 @@ const CodelistEdit = (props) => {
return <Loading />;
}
if (saving) {
return <Loading text="saving" />;
return <Saving />;
}

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ export const DumbCodelistDetailEdit = ({
{submitting && clientSideErrors && (
<GlobalClientSideErrorBloc
clientSideErrors={clientSideErrors.errorMessage}
D={D}
/>
)}
{serverSideError && (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState, useEffect, useCallback } from 'react';
import { useCallback, useEffect, useState } from 'react';
import { useParams } from 'react-router-dom';

import { Loading } from '@components/loading';
import { Loading, Publishing } from '@components/loading';

import { useSecondLang } from '@utils/hooks/second-lang';
import { useGoBack } from '@utils/hooks/useGoBack';
Expand Down Expand Up @@ -66,7 +66,7 @@ export const Component = (props) => {
if (loading) {
return <Loading />;
}
if (publishing) return <Loading text="publishing" />;
if (publishing) return <Publishing />;

return (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const CodeListDetailView = ({
codelist={codelist}
deletable={deletable}
></ViewMenu>
{serverSideError && <ErrorBloc error={serverSideError} />}
<ErrorBloc error={serverSideError} />
<Row>
<Note
text={
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState, useEffect, useCallback } from 'react';
import { useParams, useNavigate, useLocation } from 'react-router-dom';

import { Loading } from '@components/loading';
import { Loading, Saving } from '@components/loading';

import { useStampsOptions } from '@utils/hooks/stamps';

Expand Down Expand Up @@ -114,7 +114,7 @@ export const Component = (props) => {
return <Loading />;
}
if (saving) {
return <Loading text="saving" />;
return <Saving />;
}

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ export const DumbCodelistPartialDetailEdit = ({
{submitting && clientSideErrors && (
<GlobalClientSideErrorBloc
clientSideErrors={clientSideErrors.errorMessage}
D={D}
/>
)}
{serverSideError && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const CodeListPartialDetailView = ({
{deletable && <DeleteButton action={handleDelete} col={col} />}
</Auth>
</ActionToolbar>
{serverSideError && <ErrorBloc error={serverSideError} />}
<ErrorBloc error={serverSideError} />
<Row>
<Note
text={
Expand Down
22 changes: 0 additions & 22 deletions src/packages/modules-codelists/i18n/dictionary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,22 +155,6 @@ const dictionary = {
fr: 'Description (fr)',
en: 'Description (en)',
},
statusValidatedM: {
fr: 'Publié',
en: 'Published',
},
statusValidatedF: {
fr: 'Publiée',
en: 'Published',
},
statusModifiedM: {
fr: 'Provisoire, déjà publié',
en: 'Temporary, already published',
},
statusModifiedF: {
fr: 'Provisoire, déjà publiée',
en: 'Temporary, already published',
},
disseminationStatusPlaceholder: {
fr: 'Sélectionnez un statut de diffusion...',
en: 'Select dissemination status...',
Expand Down Expand Up @@ -269,11 +253,5 @@ const dictionary = {
en: (propertyName: string) =>
`The property <strong>${propertyName}</strong> has invalid characters.`,
},
errors: {
GlobalClientSideErrorBloc: {
fr: 'Vous avez des erreurs dans ce formulaire.',
en: 'You have errors in this form.',
},
},
};
export default dictionary;
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,7 @@ import {
} from '@components/buttons/buttons-with-icons';
import { GlobalClientSideErrorBloc } from '@components/errors-bloc';

import { createAllDictionary } from '@utils/dictionnary';

import D from '../../../deprecated-locales/build-dictionary';

const { D: dict } = createAllDictionary({
errors: {
GlobalClientSideErrorBloc: {
fr: 'Remplissez les champs requis pour pouvoir sauvegarder cette collection.',
en: 'Complete mandatory fields to save this collection.',
},
},
});

function Controls({ handleSave, redirectCancel, errors, submitting }) {
function Controls({ handleSave, redirectCancel, errors }) {
return (
<>
<ActionToolbar>
Expand All @@ -28,10 +15,7 @@ function Controls({ handleSave, redirectCancel, errors, submitting }) {
disabled={errors?.errorMessage?.length > 0}
/>
</ActionToolbar>
<GlobalClientSideErrorBloc
clientSideErrors={errors?.errorMessage}
D={submitting ? D : dict}
/>
<GlobalClientSideErrorBloc clientSideErrors={errors?.errorMessage} />
</>
);
}
Expand Down
Loading

0 comments on commit 465ce7d

Please sign in to comment.