Skip to content

Commit

Permalink
fix: visualize with context (#885)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
MailineN authored Dec 11, 2024
1 parent 1bc2111 commit fff7047
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 21 deletions.
12 changes: 6 additions & 6 deletions src/constants/dictionary.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
43 changes: 38 additions & 5 deletions src/utils/remote-api.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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}` },
};
Expand All @@ -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');
Expand All @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ exports[`Visualize Dropdown Component: > Should disable the button 1`] = `
<a
href="#"
>
Web V2
Web household
</a>
</li>
<li>
<a
href="#"
>
Web DSFR
Web business
</a>
</li>
<li>
Expand Down Expand Up @@ -113,14 +113,14 @@ exports[`Visualize Dropdown Component: > Should display the dropdown on top 1`]
<a
href="#"
>
Web V2
Web household
</a>
</li>
<li>
<a
href="#"
>
Web DSFR
Web business
</a>
</li>
<li>
Expand Down Expand Up @@ -194,14 +194,14 @@ exports[`Visualize Dropdown Component: > Should return the right HTML 1`] = `
<a
href="#"
>
Web V2
Web household
</a>
</li>
<li>
<a
href="#"
>
Web DSFR
Web business
</a>
</li>
<li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down

0 comments on commit fff7047

Please sign in to comment.