Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { useContributionTracking } from "./tracking";
import { isAgreementSupported, isAgreementValid } from "./contributionUtils";
import { ContributionGenericContent } from "./ContributionGenericContent";
import { Contribution } from "./type";
import { useLocalStorageForAgreementOnPageLoad } from "../utils/useLocalStorage";
import {
useLocalStorageForAgreementOnPageLoad,
getAgreementFromLocalStorage,
} from "../utils/useLocalStorage";
import { useRouter } from "next/navigation";
import { ContributionGenericAgreementSearch } from "./ContributionGenericAgreementSearch";
import { Button } from "@codegouvfr/react-dsfr/Button";
import { fr } from "@codegouvfr/react-dsfr";
Expand All @@ -14,6 +18,7 @@ type Props = {
};

export function ContributionGeneric({ contribution }: Props) {
const router = useRouter();
const [hash, setHash] = useState("");
const personalizeTitleRef = useRef<HTMLParagraphElement>(null);
const getTitle = () => `/contribution/${slug}`;
Expand Down Expand Up @@ -52,6 +57,19 @@ export function ContributionGeneric({ contribution }: Props) {
}
}, [hash]);

useEffect(() => {
if (window.location.hash === "#retour") return;

const storedAgreement = getAgreementFromLocalStorage();
if (storedAgreement && isAgreementValid(contribution, storedAgreement)) {
const targetUrl =
slug === "les-conges-pour-evenements-familiaux"
? `/contribution/${slug}/${storedAgreement.slug || storedAgreement.num}`
: `/contribution/${storedAgreement.num}-${slug}`;
Comment on lines +65 to +68
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[IMPORTANT] Hardcoded slug string is a fragile magic constant

  • If the slug for "congés pour événements familiaux" ever changes (rename, typo fix, etc.), this silent branch will silently stop working with no compile-time error.
  • The special-casing logic is duplicated here and likely elsewhere in the codebase.

Proposed fix: Extract this to a named constant (or derive it from the contribution data) so the intent is clear and the value is maintained in one place.

Suggested change
const targetUrl =
slug === "les-conges-pour-evenements-familiaux"
? `/contribution/${slug}/${storedAgreement.slug || storedAgreement.num}`
: `/contribution/${storedAgreement.num}-${slug}`;
const CONGES_FAMILIAUX_SLUG = "les-conges-pour-evenements-familiaux";
const targetUrl =
slug === CONGES_FAMILIAUX_SLUG
? `/contribution/${slug}/${storedAgreement.slug || storedAgreement.num}`
: `/contribution/${storedAgreement.num}-${slug}`;

router.replace(targetUrl);
}
}, []);

return (
<>
<ContributionGenericAgreementSearch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ jest.mock("uuid", () => ({
}));

const pushMock = jest.fn();
const replaceMock = jest.fn();

jest.mock("next/navigation", () => ({
redirect: jest.fn(),
usePathname: jest.fn(),
useRouter: () => ({
push: pushMock,
replace: replaceMock,
}),
}));

Expand All @@ -59,6 +61,7 @@ describe("<ContributionLayout />", () => {
const ma = sendEvent as jest.MockedFunction<typeof sendEvent>;
ma.mockReset();
pushMock.mockClear();
replaceMock.mockClear();
});
it("should render title only if generic", () => {
rendering = render(<ContributionLayout contribution={contribution} />);
Expand Down Expand Up @@ -305,4 +308,78 @@ describe("<ContributionLayout />", () => {
expect(ccUi.warning.noCdtUnextendedAgreement.query()).toBeInTheDocument();
});
});

describe("auto-redirect from header CC", () => {
it("should redirect to CC-specific page when a valid agreement is in localStorage", () => {
window.localStorage.setItem(
"convention",
JSON.stringify({
id: "0016",
num: 16,
shortTitle:
"Transports routiers et activités auxiliaires du transport",
slug: "16-transports-routiers-et-activites-auxiliaires-du-transport",
title:
"Convention collective nationale des transports routiers et activités auxiliaires du transport du 21 décembre 1950",
})
);
render(<ContributionLayout contribution={contribution} />);
expect(replaceMock).toHaveBeenCalledWith("/contribution/16-slug");
});

it("should not redirect when agreement is unsupported", () => {
window.localStorage.setItem(
"convention",
JSON.stringify({
id: "1388",
num: 1388,
shortTitle: "Industrie du pétrole",
slug: "1388-industrie-du-petrole",
title: "Convention collective nationale de l'industrie du pétrole",
})
);
render(<ContributionLayout contribution={contribution} />);
expect(replaceMock).not.toHaveBeenCalled();
});

it("should not redirect when agreement is unextended", () => {
window.localStorage.setItem(
"convention",
JSON.stringify({
id: "0029",
num: 29,
shortTitle: "Hospitalisation privée",
slug: "29-hospitalisation-privee",
title: "Convention collective nationale des établissements privés",
})
);
render(<ContributionLayout contribution={contribution} />);
expect(replaceMock).not.toHaveBeenCalled();
});

it("should not redirect when hash is #retour", () => {
window.localStorage.setItem(
"convention",
JSON.stringify({
id: "0016",
num: 16,
shortTitle:
"Transports routiers et activités auxiliaires du transport",
slug: "16-transports-routiers-et-activites-auxiliaires-du-transport",
title:
"Convention collective nationale des transports routiers et activités auxiliaires du transport du 21 décembre 1950",
})
);
window.location.hash = "#retour";
render(<ContributionLayout contribution={contribution} />);
expect(replaceMock).not.toHaveBeenCalled();
window.location.hash = "";
});

it("should not redirect when no agreement is in localStorage", () => {
window.localStorage.clear();
render(<ContributionLayout contribution={contribution} />);
expect(replaceMock).not.toHaveBeenCalled();
});
});
});
Loading