Skip to content

Commit ce3834a

Browse files
authored
fix(contributions): fix de l'erreur js dans la console quand une CC était présente dans le localStorage (#6445)
1 parent d26db43 commit ce3834a

File tree

3 files changed

+31
-34
lines changed

3 files changed

+31
-34
lines changed

packages/code-du-travail-frontend/src/modules/contributions/ContributionGeneric.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useContributionTracking } from "./tracking";
44
import { isAgreementSupported, isAgreementValid } from "./contributionUtils";
55
import { ContributionGenericContent } from "./ContributionGenericContent";
66
import { Contribution } from "./type";
7-
import { useLocalStorageForAgreement } from "../common/useLocalStorage";
7+
import { useLocalStorageForAgreementOnPageLoad } from "../common/useLocalStorage";
88
import { ContributionGenericAgreementSearch } from "./ContributionGenericAgreementSearch";
99
import { Button } from "@codegouvfr/react-dsfr/Button";
1010
import { fr } from "@codegouvfr/react-dsfr";
@@ -20,7 +20,7 @@ export function ContributionGeneric({ contribution }: Props) {
2020
const [displayGeneric, setDisplayGeneric] = useState(false);
2121

2222
const [selectedAgreement, setSelectedAgreement] =
23-
useLocalStorageForAgreement();
23+
useLocalStorageForAgreementOnPageLoad();
2424
const {
2525
emitAgreementTreatedEvent,
2626
emitAgreementUntreatedEvent,
@@ -62,7 +62,7 @@ export function ContributionGeneric({ contribution }: Props) {
6262
emitDisplayAgreementContent(getTitle());
6363
}
6464
}}
65-
defaultAgreement={selectedAgreement}
65+
selectedAgreement={selectedAgreement}
6666
trackingActionName={getTitle()}
6767
/>
6868

packages/code-du-travail-frontend/src/modules/contributions/ContributionGenericAgreementSearch.tsx

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,23 @@ type Props = {
2020
onAgreementSelect: (agreement?: EnterpriseAgreement) => void;
2121
onDisplayClick: (ev: React.MouseEvent<HTMLAnchorElement, MouseEvent>) => void;
2222
contribution: Contribution;
23-
defaultAgreement?: EnterpriseAgreement;
23+
selectedAgreement?: EnterpriseAgreement;
2424
trackingActionName: string;
2525
};
2626

2727
export function ContributionGenericAgreementSearch({
2828
contribution,
2929
onAgreementSelect,
3030
onDisplayClick,
31-
defaultAgreement,
31+
selectedAgreement,
3232
trackingActionName,
3333
}: Props) {
3434
const { slug } = contribution;
35-
36-
const [selectedAgreement, setSelectedAgreement] =
37-
useState<EnterpriseAgreement>();
38-
const [isValid, setIsValid] = useState(
39-
defaultAgreement ? isAgreementValid(contribution, defaultAgreement) : false
40-
);
35+
const [isValid, setIsValid] = useState(false);
4136
useEffect(() => {
42-
setIsValid(
43-
isAgreementValid(contribution, selectedAgreement ?? defaultAgreement)
44-
);
45-
}, [selectedAgreement, defaultAgreement]);
37+
setIsValid(isAgreementValid(contribution, selectedAgreement));
38+
}, [selectedAgreement]);
39+
4640
const selectedAgreementAlert = (agreement: EnterpriseAgreement) => {
4741
const isSupported = isAgreementSupported(contribution, agreement);
4842
const isUnextended = isAgreementUnextended(contribution, agreement);
@@ -96,23 +90,19 @@ export function ContributionGenericAgreementSearch({
9690
</div>
9791
<div>
9892
<AgreementSearchForm
99-
onAgreementSelect={(agreement) => {
100-
onAgreementSelect(agreement);
101-
setSelectedAgreement(
102-
isAgreementValid(contribution, agreement) ? agreement : undefined
103-
);
104-
}}
93+
onAgreementSelect={onAgreementSelect}
10594
selectedAgreementAlert={selectedAgreementAlert}
106-
defaultAgreement={defaultAgreement}
95+
defaultAgreement={selectedAgreement}
10796
trackingActionName={trackingActionName}
10897
/>
10998
{((contribution.isNoCDT && isValid) || !contribution.isNoCDT) && (
11099
<Button
111100
className={fr.cx("fr-mt-2w")}
112101
linkProps={{
113-
href: isValid
114-
? `/contribution/${(selectedAgreement ?? defaultAgreement)?.num}-${slug}`
115-
: "",
102+
href:
103+
isValid && selectedAgreement
104+
? `/contribution/${selectedAgreement?.num}-${slug}`
105+
: "",
116106
onClick: onDisplayClick,
117107
}}
118108
>

packages/code-du-travail-frontend/src/modules/convention-collective/AgreementSearch/AgreementSearchForm.tsx

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
EnterpriseAgreement,
99
EnterpriseAgreementSearchInput,
1010
} from "../../enterprise";
11+
import { AgreementRoute } from "../../../outils/common/type/WizardType";
1112

1213
type Props = {
1314
onAgreementSelect: (agreement?: EnterpriseAgreement) => void;
@@ -24,9 +25,15 @@ export const AgreementSearchForm = ({
2425
defaultAgreement,
2526
trackingActionName,
2627
}: Props) => {
27-
const [mode, setMode] = useState<
28-
"agreementSearch" | "enterpriseSearch" | "noSearch" | undefined
29-
>(!!defaultAgreement ? "agreementSearch" : undefined);
28+
const [selectedRoute, setSelectedRoute] = useState<
29+
AgreementRoute | undefined
30+
>();
31+
32+
useEffect(() => {
33+
if (defaultAgreement && !selectedRoute) {
34+
setSelectedRoute("agreement");
35+
}
36+
}, [defaultAgreement]);
3037

3138
const { emitClickP1, emitClickP2 } = useContributionTracking();
3239

@@ -39,24 +46,24 @@ export const AgreementSearchForm = ({
3946
label:
4047
"Je sais quelle est ma convention collective et je la saisis.",
4148
nativeInputProps: {
42-
checked: mode === "agreementSearch",
43-
onChange: () => setMode("agreementSearch"),
49+
checked: selectedRoute === "agreement",
50+
onChange: () => setSelectedRoute("agreement"),
4451
},
4552
},
4653
{
4754
label:
4855
"Je cherche mon entreprise pour trouver ma convention collective.",
4956
nativeInputProps: {
50-
checked: mode === "enterpriseSearch",
57+
checked: selectedRoute === "enterprise",
5158
onChange: () => {
5259
onAgreementSelect();
53-
setMode("enterpriseSearch");
60+
setSelectedRoute("enterprise");
5461
},
5562
},
5663
},
5764
]}
5865
/>
59-
{mode === "agreementSearch" && (
66+
{selectedRoute === "agreement" && (
6067
<AgreementSearchInput
6168
onAgreementSelect={(agreement) => {
6269
emitClickP1(trackingActionName);
@@ -67,7 +74,7 @@ export const AgreementSearchForm = ({
6774
trackingActionName={trackingActionName}
6875
/>
6976
)}
70-
{mode === "enterpriseSearch" && (
77+
{selectedRoute === "enterprise" && (
7178
<EnterpriseAgreementSearchInput
7279
onAgreementSelect={(agreement) => {
7380
emitClickP2(trackingActionName);

0 commit comments

Comments
 (0)