Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/expert-filter' into expert-filter
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinebhs committed Oct 18, 2023
2 parents f14afa4 + ad8f4f8 commit 8729dfd
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
63 changes: 62 additions & 1 deletion src/components/menus/content-contextual-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import React, { useState, useCallback } from 'react';
import React, { useState, useCallback, useRef } from 'react';
import { useSelector } from 'react-redux';
import PropTypes from 'prop-types';
import { useIntl } from 'react-intl';
Expand All @@ -23,6 +23,7 @@ import DeleteDialog from '../dialogs/delete-dialog';
import ReplaceWithScriptDialog from '../dialogs/replace-with-script-dialog';
import CopyToScriptDialog from '../dialogs/copy-to-script-dialog';
import CreateStudyDialog from '../dialogs/create-study-dialog/create-study-dialog';
import { downloadCase } from 'utils/rest-api';

import { DialogsId } from '../../utils/UIconstants';

Expand All @@ -41,6 +42,7 @@ import {
duplicateStudy,
getNameCandidate,
duplicateParameter,
getCaseOriginalName,
} from '../../utils/rest-api';

import { ContingencyListType, ElementType } from '../../utils/elementType';
Expand All @@ -52,6 +54,7 @@ import {
} from '../../utils/custom-hooks';
import { useSnackMessage } from '@gridsuite/commons-ui';
import MoveDialog from '../dialogs/move-dialog';
import { FileDownload } from '@mui/icons-material';

const ContentContextualMenu = (props) => {
const {
Expand Down Expand Up @@ -397,6 +400,42 @@ const ContentContextualMenu = (props) => {
);
}, [isUserAllowed, selectedElements]);

const allowsDownloadCase = useCallback(() => {
//if selectedElements contains at least one case
return selectedElements.some(
(element) => element.type === ElementType.CASE
);
}, [selectedElements]);

/**
* Downloads the selected cases as a file.
* @function
* @name handleDownloadCase
* @returns {void}
*/
const handleDownloadCase = useCallback(() => {
//for each selectedElements , filter cases and return their uuid and subtype
const casesToDownload = selectedElements
.filter((element) => element.type === ElementType.CASE)
.map((element) => ({
elementUuid: element.elementUuid,
}));

casesToDownload.forEach(async (element) => {
const result = await downloadCase(element.elementUuid);
const name = await getCaseOriginalName(element.elementUuid);
const blob = await result.blob();
const href = window.URL.createObjectURL(blob);
const a = linkRef.current;
a.download = name;
a.href = href;
a.click();
a.href = '';
});
}, [selectedElements]);

const linkRef = useRef(null);

const buildMenu = () => {
if (selectedElements.length === 0) {
return;
Expand Down Expand Up @@ -468,6 +507,17 @@ const ContentContextualMenu = (props) => {
icon: <FileCopyIcon fontSize="small" />,
});
}
if (allowsDownloadCase()) {
// is export allowed
menuItems.push({
messageDescriptorId: 'downloadCase',
callback: () => {
handleDownloadCase();
handleCloseDialog();
},
icon: <FileDownload fontSize="small" />,
});
}

if (allowsReplaceContingencyWithScript()) {
menuItems.push({
Expand Down Expand Up @@ -651,6 +701,17 @@ const ContentContextualMenu = (props) => {
/>
)}
{renderDialog()}
{/* for download cases*/}
<a
href="/"
title="download case"
ref={linkRef}
target="_blank"
rel="noreferrer"
style={{ display: 'none' }}
>
Download Case
</a>

<iframe
id={DownloadIframe}
Expand Down
1 change: 1 addition & 0 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,5 @@
"emptyRule": "Filter contains an empty field",
"incorrectRule": "Filter contains an incorrect field",
"emptyGroup": "Filter contains an empty group. Consider removing it or adding rules to this group"
"downloadCase": "Download Case"
}
1 change: 1 addition & 0 deletions src/translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,5 @@
"emptyRule": "Le filtre contient un champ vide",
"incorrectRule": "Le filtre contient un champ incorrect",
"emptyGroup": "Le filtre contient un groupe vide. Supprimez le ou ajoutez des règles à ce groupe"
"downloadCase": "Télécharger la situation"
}
25 changes: 25 additions & 0 deletions src/utils/rest-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,23 @@ export function getNameCandidate(directoryUuid, elementName, type) {
});
}

/**
* Retrieves the original name of a case using its UUID.
* @param {string} caseUuid - The UUID of the element.
* @returns {Promise<string|boolean>} - A promise that resolves to the original name of the case if found, or false if not found.
*/
export function getCaseOriginalName(caseUuid) {
const caseNameUrl = PREFIX_CASE_QUERIES + `/v1/cases/${caseUuid}/name`;
console.debug(caseNameUrl);
return backendFetchText(caseNameUrl).catch((error) => {
if (error.status === 404) {
return false;
} else {
throw error;
}
});
}

export function rootDirectoryExists(directoryName) {
const existsRootDirectoryUrl =
PREFIX_DIRECTORY_SERVER_QUERIES +
Expand Down Expand Up @@ -929,3 +946,11 @@ export function deleteCase(caseUuid) {
method: 'delete',
});
}

export function downloadCase(caseUuid) {
const downloadCaseUrl =
PREFIX_CASE_QUERIES + '/v1/cases/' + caseUuid + '?xiidm=false';
return backendFetch(downloadCaseUrl, {
method: 'get',
});
}

0 comments on commit 8729dfd

Please sign in to comment.