From fff70472879fa4de33494f0b1f2e7b159ba24474 Mon Sep 17 00:00:00 2001 From: Mailine Nguyen <64129348+MailineN@users.noreply.github.com> Date: Wed, 11 Dec 2024 14:42:31 +0100 Subject: [PATCH] fix: visualize with context (#885) * chore: remove StromaeV2 visualization option * feat: add web business/household visualization * fix: context parameter & fix tests * refactor: changed how the request url is build for visualization --- src/constants/dictionary.jsx | 12 +++--- src/utils/remote-api.jsx | 43 ++++++++++++++++--- .../visualize-dropdown.spec.jsx.snap | 12 +++--- .../components/visualize-dropdown.jsx | 8 ++-- 4 files changed, 54 insertions(+), 21 deletions(-) diff --git a/src/constants/dictionary.jsx b/src/constants/dictionary.jsx index 170991ef7..9df43b29b 100644 --- a/src/constants/dictionary.jsx +++ b/src/constants/dictionary.jsx @@ -1599,13 +1599,13 @@ const dictionary = { fr: 'Web V1', en: 'Web V1', }, - VISUALIZE_WEB_STROMAE_V2: { - fr: 'Web V2', - en: 'Web V2', + VISUALIZE_WEB_HOUSEHOLD: { + fr: 'Web ménage', + en: 'Web household', }, - VISUALIZE_WEB_STROMAE_V3: { - fr: 'Web DSFR', - en: 'Web DSFR', + VISUALIZE_WEB_BUSINESS: { + fr: 'Web entreprise', + en: 'Web business', }, VISUALIZE_QUEEN_CAPI: { fr: 'Enquêteur face à face', diff --git a/src/utils/remote-api.jsx b/src/utils/remote-api.jsx index 7189b1a6d..3d3e2fad6 100644 --- a/src/utils/remote-api.jsx +++ b/src/utils/remote-api.jsx @@ -19,6 +19,23 @@ const getBaseURI = () => { }); }; +/** + * Constructs a URL with the given base URL, path, reference, and optional context. + * + * @param {string} baseUrl - Backend baseUrl. + * @param {string} path - The specific path of the endpoint. + * @param {boolean} ref - Indicates if the questionnaire contains a reference to another questionnaire. + * @param {string} [context] - The optional context of the visualization (HOUSEHOLD or BUSINESS). + * @returns {string} - The constructed URL with the provided parameters. + */ +const buildUrl = (baseUrl, path, ref, context) => { + let url = `${baseUrl}/${path}?references=${ref}`; + if (context) { + url += `&context=${context}`; + } + return url; +}; + const pathInit = 'init'; const pathQuestionnaireList = 'persistence/questionnaires'; const pathQuestionnaireListSearch = 'persistence/questionnaires/search/meta'; @@ -60,8 +77,16 @@ export const getVisualization = async (type, qr, ref, token) => { response: 'url', path: `/${qr.DataCollection[0].id}/${qr.Name}`, }, - 'stromae-v2': { response: 'url', path: `-stromae-v2/${qr.Name}` }, - 'stromae-v3': { response: 'url', path: `-stromae-v3/${qr.Name}` }, + 'web-household': { + response: 'url', + path: `-stromae-v3/${qr.Name}`, + context: 'HOUSEHOLD', + }, + 'web-business': { + response: 'url', + path: `-stromae-v3/${qr.Name}`, + context: 'BUSINESS', + }, 'queen-capi': { response: 'url', path: `-queen/${qr.Name}` }, 'queen-cati': { response: 'url', path: `-queen-telephone/${qr.Name}` }, }; @@ -72,7 +97,13 @@ export const getVisualization = async (type, qr, ref, token) => { ); } if (visualiseType[type].response === 'url') { - return postVisualization(visualiseType[type].path, qr, ref, token) + return postVisualization( + visualiseType[type].path, + qr, + ref, + token, + visualiseType[type].context ? visualiseType[type].context : null, + ) .then((response) => response.text()) .then((url) => { const a = document.createElement('a'); @@ -91,11 +122,13 @@ export const getVisualization = async (type, qr, ref, token) => { * @param {*} qr : the questionnaire to visualize * @param {*} ref : a boolean that indicates if the questionnaire contains a reference to another questionnaire * @param {*} token : the token + * @param {*} context : the context of the visualization (optional, houseold or business) * @returns a data to interpret */ -const postVisualization = async (path, qr, ref, token) => { +const postVisualization = async (path, qr, ref, token, context = null) => { const b = await getBaseURI(); - return fetch(`${b}/${pathVisualisation}${path}?references=${ref}`, { + const url = buildUrl(b, pathVisualisation + path, ref, context); + return fetch(url, { method: 'POST', headers: getHeaders({ 'Content-Type': 'application/json' }, token), body: JSON.stringify(qr), diff --git a/src/widgets/visualize-dropdown/__snapshots__/visualize-dropdown.spec.jsx.snap b/src/widgets/visualize-dropdown/__snapshots__/visualize-dropdown.spec.jsx.snap index 2f2572629..bb15dc10c 100644 --- a/src/widgets/visualize-dropdown/__snapshots__/visualize-dropdown.spec.jsx.snap +++ b/src/widgets/visualize-dropdown/__snapshots__/visualize-dropdown.spec.jsx.snap @@ -32,14 +32,14 @@ exports[`Visualize Dropdown Component: > Should disable the button 1`] = ` - Web V2 + Web household
  • - Web DSFR + Web business
  • @@ -113,14 +113,14 @@ exports[`Visualize Dropdown Component: > Should display the dropdown on top 1`] - Web V2 + Web household
  • - Web DSFR + Web business
  • @@ -194,14 +194,14 @@ exports[`Visualize Dropdown Component: > Should return the right HTML 1`] = ` - Web V2 + Web household
  • - Web DSFR + Web business
  • diff --git a/src/widgets/visualize-dropdown/components/visualize-dropdown.jsx b/src/widgets/visualize-dropdown/components/visualize-dropdown.jsx index 596334f53..454594f01 100644 --- a/src/widgets/visualize-dropdown/components/visualize-dropdown.jsx +++ b/src/widgets/visualize-dropdown/components/visualize-dropdown.jsx @@ -82,12 +82,12 @@ function VisualizeDropdown({ const links = [ { actionType: 'html', actionLabel: Dictionary.VISUALIZE_WEB }, { - actionType: 'stromae-v2', - actionLabel: Dictionary.VISUALIZE_WEB_STROMAE_V2, + actionType: 'web-household', + actionLabel: Dictionary.VISUALIZE_WEB_HOUSEHOLD, }, { - actionType: 'stromae-v3', - actionLabel: Dictionary.VISUALIZE_WEB_STROMAE_V3, + actionType: 'web-business', + actionLabel: Dictionary.VISUALIZE_WEB_BUSINESS, }, { actionType: 'queen-capi', actionLabel: Dictionary.VISUALIZE_QUEEN_CAPI }, { actionType: 'queen-cati', actionLabel: Dictionary.VISUALIZE_QUEEN_CATI },