Skip to content

Commit dc3796c

Browse files
committed
feat(participation): add same email validation for employer and display error message
1 parent 90ce622 commit dc3796c

2 files changed

Lines changed: 40 additions & 40 deletions

File tree

lab/assets/js/participation/components/FieldError.tsx

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,20 @@ export default function FieldError<T>({
1616

1717
if (Array.isArray(fieldErrors)) {
1818
return (
19-
<div
20-
className="fr-messages-group"
19+
<ErrorMessage
2120
id={`participation-${String(firstKey)}-messages`}
22-
aria-live="polite"
23-
>
24-
{fieldErrors.map((errorMsg, idx) => (
25-
<p
26-
className="fr-message fr-message--error"
27-
key={`participation-${String(firstKey)}-error-${idx}`}
28-
>
29-
{errorMsg}
30-
</p>
31-
))}
32-
</div>
21+
errors={fieldErrors}
22+
/>
3323
);
3424
}
3525

3626
if (!secondKey) {
3727
if ("non_field_errors" in fieldErrors) {
3828
return (
39-
<div
40-
className="fr-messages-group"
29+
<ErrorMessage
4130
id={`participation-${String(firstKey)}-messages`}
42-
aria-live="polite"
43-
>
44-
{fieldErrors.non_field_errors.map((errorMsg, idx) => (
45-
<p
46-
className="fr-message fr-message--error"
47-
key={`participation-${String(firstKey)}-non-field-error-${idx}`}
48-
>
49-
{errorMsg}
50-
</p>
51-
))}
52-
</div>
31+
errors={fieldErrors.non_field_errors}
32+
/>
5333
);
5434
}
5535
return null;
@@ -66,21 +46,23 @@ export default function FieldError<T>({
6646

6747
if (Array.isArray(secondFieldErrors)) {
6848
return (
69-
<div
70-
className="fr-messages-group"
49+
<ErrorMessage
7150
id={`participation-${String(firstKey)}-${String(secondKey)}-messages`}
72-
aria-live="polite"
73-
>
74-
{secondFieldErrors.map((errorMsg, idx) => (
75-
<p
76-
className="fr-message fr-message--error"
77-
key={`participation-${String(firstKey)}-${String(secondKey)}-error-${idx}`}
78-
>
79-
{errorMsg}
80-
</p>
81-
))}
82-
</div>
51+
errors={secondFieldErrors}
52+
/>
8353
);
8454
}
8555
}
8656
}
57+
58+
export function ErrorMessage({ id, errors }: { id: string; errors: string[] }) {
59+
return (
60+
<div className="fr-messages-group" id={id} aria-live="polite">
61+
{errors.map((errorMsg, idx) => (
62+
<p className="fr-message fr-message--error" key={`${id}-error-${idx}`}>
63+
{errorMsg}
64+
</p>
65+
))}
66+
</div>
67+
);
68+
}

lab/assets/js/participation/components/ParticipationForm.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
setProjectLeader,
1515
ValidationDataError,
1616
} from "../participation.service";
17-
import FieldError from "./FieldError";
17+
import FieldError, { ErrorMessage } from "./FieldError";
1818
import { useFormStatus } from "react-dom";
1919

2020
interface ParticipationFormProps {
@@ -61,6 +61,9 @@ export default function ParticipationForm({
6161
employerFormExemptInfo: window.gettext(
6262
"Employer information is not required for the selected institution.",
6363
),
64+
sameEmailError: window.gettext(
65+
"Employer email must be different from member email.",
66+
),
6467
};
6568

6669
const [email, setEmail] = useState("");
@@ -109,6 +112,13 @@ export default function ParticipationForm({
109112
if (!institution.country) {
110113
return;
111114
}
115+
if (
116+
email &&
117+
employer.email &&
118+
email.toLowerCase() === employer.email.toLowerCase()
119+
) {
120+
return;
121+
}
112122
try {
113123
if (participationType === "leader") {
114124
await setProjectLeader(
@@ -283,6 +293,14 @@ export default function ParticipationForm({
283293
firstKey="employer"
284294
secondKey="email"
285295
/>
296+
{email &&
297+
employer.email &&
298+
email.toLowerCase() === employer.email.toLowerCase() && (
299+
<ErrorMessage
300+
id="participation-employer-email-messages"
301+
errors={[t.sameEmailError]}
302+
/>
303+
)}
286304
</div>
287305
</div>
288306
<div className="fr-fieldset__element">

0 commit comments

Comments
 (0)