From fce2d77fbf139c7b12f9b666d2cabf23fecee3a8 Mon Sep 17 00:00:00 2001 From: Manu Date: Fri, 29 Jan 2021 11:34:40 +0100 Subject: [PATCH] feat(front): add relatedItems to tools (#3276) * feat(front): add relatedItems to tools * fix(frontend): double survey --- .../pages/outils/[slug].js | 23 ++++- .../src/common/RelatedItems.js | 90 ++++++++++--------- 2 files changed, 68 insertions(+), 45 deletions(-) diff --git a/packages/code-du-travail-frontend/pages/outils/[slug].js b/packages/code-du-travail-frontend/pages/outils/[slug].js index 07bf972ab5..f1c5aeda6f 100644 --- a/packages/code-du-travail-frontend/pages/outils/[slug].js +++ b/packages/code-du-travail-frontend/pages/outils/[slug].js @@ -1,9 +1,12 @@ import tools from "@cdt/data...tools/internals.json"; +import * as Sentry from "@sentry/browser"; import { Container, Section, theme, Wrapper } from "@socialgouv/cdtn-ui"; +import getConfig from "next/config"; import React, { useEffect } from "react"; import styled from "styled-components"; import Metas from "../../src/common/Metas"; +import { RelatedItems } from "../../src/common/RelatedItems"; import { Share } from "../../src/common/Share"; import { Layout } from "../../src/layout/Layout"; import { ToolSurvey } from "../../src/outils/common/ToolSurvey"; @@ -15,6 +18,12 @@ import { SimulateurIndemnitePrecarite } from "../../src/outils/IndemnitePrecarit import { SimulateurEmbauche } from "../../src/outils/SimulateurEmbauche"; import { matopush } from "../../src/piwik"; +const { SOURCES } = require("@socialgouv/cdtn-sources"); + +const { + publicRuntimeConfig: { API_URL }, +} = getConfig(); + const toolsBySlug = { "heures-recherche-emploi": HeuresRechercheEmploi, "indemnite-licenciement": CalculateurIndemnite, @@ -24,7 +33,7 @@ const toolsBySlug = { "simulateur-embauche": SimulateurEmbauche, }; -function Outils({ description, icon, slug, title }) { +function Outils({ description, icon, slug, relatedItems, title }) { const Tool = toolsBySlug[slug]; useEffect(() => { matopush(["trackEvent", "outil", `view_step_${title}`, "start"]); @@ -44,6 +53,7 @@ function Outils({ description, icon, slug, title }) { + @@ -55,10 +65,21 @@ export default Outils; Outils.getInitialProps = async ({ query }) => { const { slug } = query; const { description, icon, title } = tools.find((tool) => tool.slug === slug); + let relatedItems = []; + try { + const response = await fetch(`${API_URL}/items/${SOURCES.TOOLS}/${slug}`); + if (response.ok) { + relatedItems = await response.json().then((data) => data.relatedItems); + } + } catch (e) { + console.error(e); + Sentry.captureException(e); + } return { description, icon, + relatedItems, slug, title, }; diff --git a/packages/code-du-travail-frontend/src/common/RelatedItems.js b/packages/code-du-travail-frontend/src/common/RelatedItems.js index deb2088c66..3fe6a4d8af 100644 --- a/packages/code-du-travail-frontend/src/common/RelatedItems.js +++ b/packages/code-du-travail-frontend/src/common/RelatedItems.js @@ -29,7 +29,7 @@ const matoSelectRelated = (reco, selection) => { ]); }; -export const RelatedItems = ({ items = [] }) => { +export const RelatedItems = ({ disableSurvey = false, items = [] }) => { const isArticleSource = (source) => ![SOURCES.EXTERNALS, SOURCES.LETTERS, SOURCES.TOOLS].includes(source); @@ -47,53 +47,55 @@ export const RelatedItems = ({ items = [] }) => { return ( - - {({ - setModalVisible, - isPromptVisible, - isSurveyDisabled, - setPromptVisible, - setSurveyDisabled, - }) => - !isSurveyDisabled && - isPromptVisible && ( - - - { - setPromptVisible(false); - setSurveyDisabled(true); - }} - > - fermer la modale - - - - Questionnaire - Aidez-nous à améliorer le site - - - - - ) - } - + fermer la modale + + + + Questionnaire + Aidez-nous à améliorer le site + + + + + ) + } + + )} {relatedGroups.map( ({ title, items }) => items.length > 0 && (