Skip to content

Commit ed9957d

Browse files
author
Hugo Marcellin
committed
Rollback some changes
1 parent 826736a commit ed9957d

File tree

7 files changed

+107
-10
lines changed

7 files changed

+107
-10
lines changed

src/components/dialogs/commons/prefilled-name-input.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
ElementType,
1313
FieldConstants,
1414
} from '@gridsuite/commons-ui';
15+
import { elementExists } from '../../../utils/rest-api';
1516
import { useSelector } from 'react-redux';
1617
import { ReduxState } from '../../../redux/reducer.type';
1718

@@ -79,6 +80,7 @@ const PrefilledNameInput: FunctionComponent<PrefilledNameInputProps> = ({
7980
name={name}
8081
label={label}
8182
elementType={elementType}
83+
elementExists={elementExists}
8284
activeDirectory={activeDirectory}
8385
autoFocus={!caseFile}
8486
onManualChangeCallback={() => setModifiedByUser(true)}

src/components/dialogs/contingency-list/creation/contingency-list-creation-form.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { useFormContext, useWatch } from 'react-hook-form';
2323
import ExplicitNamingForm from '../explicit-naming/explicit-naming-form';
2424
import ScriptInputForm from '../script/script-input-form';
2525
import { useSelector } from 'react-redux';
26+
import { elementExists } from '../../../../utils/rest-api';
2627

2728
const ContingencyListCreationForm = () => {
2829
const { setValue } = useFormContext();
@@ -56,6 +57,7 @@ const ContingencyListCreationForm = () => {
5657
elementType={ElementType.CONTINGENCY_LIST}
5758
autoFocus
5859
activeDirectory={activeDirectory}
60+
elementExists={elementExists}
5961
/>
6062
</Grid>
6163
<Grid item xs={12}>

src/components/dialogs/field-hook.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
import React, { useCallback, useEffect, useMemo, useState } from 'react';
99
import { FormattedMessage, useIntl } from 'react-intl';
10-
import { rootDirectoryExists } from '../../utils/rest-api';
10+
import { elementExists, rootDirectoryExists } from '../../utils/rest-api';
1111
import { CircularProgress, InputAdornment, TextField } from '@mui/material';
1212
import CheckIcon from '@mui/icons-material/Check';
13-
import { ElementType, useDebounce, elementExists } from '@gridsuite/commons-ui';
13+
import { ElementType, useDebounce } from '@gridsuite/commons-ui';
1414

1515
const styles = {
1616
helperText: {

src/components/directory-content.jsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import {
3434
} from '@gridsuite/commons-ui';
3535
import { Box, Checkbox } from '@mui/material';
3636

37-
import { getFilterById, updateElement } from '../utils/rest-api';
37+
import { elementExists, getFilterById, updateElement } from '../utils/rest-api';
3838

3939
import ContentContextualMenu from './menus/content-contextual-menu';
4040
import ContentToolbar from './toolbars/content-toolbar';
@@ -1054,6 +1054,7 @@ const DirectoryContent = () => {
10541054
broadcastChannel={broadcastChannel}
10551055
getFilterById={getFilterById}
10561056
activeDirectory={activeDirectory}
1057+
elementExists={elementExists}
10571058
language={languageLocal}
10581059
/>
10591060
);
@@ -1069,6 +1070,7 @@ const DirectoryContent = () => {
10691070
getFilterById={getFilterById}
10701071
selectionForCopy={selectionForCopy}
10711072
activeDirectory={activeDirectory}
1073+
elementExists={elementExists}
10721074
language={languageLocal}
10731075
/>
10741076
);
@@ -1084,6 +1086,7 @@ const DirectoryContent = () => {
10841086
selectionForCopy={selectionForCopy}
10851087
getFilterById={getFilterById}
10861088
activeDirectory={activeDirectory}
1089+
elementExists={elementExists}
10871090
language={languageLocal}
10881091
/>
10891092
);

src/components/menus/content-contextual-menu.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { DialogsId } from '../../utils/UIconstants';
3030
import {
3131
deleteElements,
3232
duplicateElement,
33+
elementExists,
3334
moveElementsToDirectory,
3435
newScriptFromFilter,
3536
newScriptFromFiltersContingencyList,
@@ -774,6 +775,7 @@ const ContentContextualMenu = (props) => {
774775
activeElement.specificMetadata.equipmentType,
775776
}}
776777
activeDirectory={activeDirectory}
778+
elementExists={elementExists}
777779
language={languageLocal}
778780
/>
779781
);

src/components/menus/directory-tree-contextual-menu.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
insertRootDirectory,
3333
renameElement,
3434
updateAccessRights,
35+
elementExists,
3536
} from '../../utils/rest-api';
3637

3738
import CommonContextualMenu from './common-contextual-menu';
@@ -422,6 +423,7 @@ const DirectoryTreeContextualMenu = (props) => {
422423
open={true}
423424
onClose={handleCloseDialog}
424425
activeDirectory={activeDirectory}
426+
elementExists={elementExists}
425427
language={languageLocal}
426428
/>
427429
);

src/utils/rest-api.js

Lines changed: 93 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,8 @@ import { ContingencyListType } from './elementType';
1212
import { CONTINGENCY_ENDPOINTS } from './constants-endpoints';
1313
import {
1414
ElementType,
15-
fetchEnv,
16-
backendFetch,
17-
backendFetchJson,
18-
backendFetchText,
1915
getRequestParamFromList,
20-
getUserToken,
16+
fetchEnv,
2117
} from '@gridsuite/commons-ui';
2218

2319
const PREFIX_USER_ADMIN_SERVER_QUERIES =
@@ -39,6 +35,11 @@ const PREFIX_FILTERS_QUERIES =
3935
import.meta.env.VITE_API_GATEWAY + '/filter/v1/filters';
4036
const PREFIX_STUDY_QUERIES = import.meta.env.VITE_API_GATEWAY + '/study';
4137

38+
function getToken() {
39+
const state = store.getState();
40+
return state.user.id_token;
41+
}
42+
4243
export function connectNotificationsWsUpdateConfig() {
4344
const webSocketBaseUrl = document.baseURI
4445
.replace(/^http:\/\//, 'ws://')
@@ -50,7 +51,7 @@ export function connectNotificationsWsUpdateConfig() {
5051
APP_NAME;
5152

5253
const reconnectingWebSocket = new ReconnectingWebSocket(
53-
() => webSocketUrl + '&access_token=' + getUserToken()
54+
() => webSocketUrl + '&access_token=' + getToken()
5455
);
5556
reconnectingWebSocket.onopen = function () {
5657
console.info(
@@ -60,6 +61,78 @@ export function connectNotificationsWsUpdateConfig() {
6061
return reconnectingWebSocket;
6162
}
6263

64+
function parseError(text) {
65+
try {
66+
return JSON.parse(text);
67+
} catch (err) {
68+
return null;
69+
}
70+
}
71+
72+
function handleError(response) {
73+
return response.text().then((text) => {
74+
const errorName = 'HttpResponseError : ';
75+
let error;
76+
const errorJson = parseError(text);
77+
if (
78+
errorJson &&
79+
errorJson.status &&
80+
errorJson.error &&
81+
errorJson.message
82+
) {
83+
error = new Error(
84+
errorName +
85+
errorJson.status +
86+
' ' +
87+
errorJson.error +
88+
', message : ' +
89+
errorJson.message
90+
);
91+
error.status = errorJson.status;
92+
} else {
93+
error = new Error(
94+
errorName + response.status + ' ' + response.statusText
95+
);
96+
error.status = response.status;
97+
}
98+
throw error;
99+
});
100+
}
101+
102+
function prepareRequest(init, token) {
103+
if (!(typeof init == 'undefined' || typeof init == 'object')) {
104+
throw new TypeError(
105+
'Argument 2 of backendFetch is not an object' + typeof init
106+
);
107+
}
108+
const initCopy = Object.assign({}, init);
109+
initCopy.headers = new Headers(initCopy.headers || {});
110+
const tokenCopy = token ? token : getToken();
111+
initCopy.headers.append('Authorization', 'Bearer ' + tokenCopy);
112+
return initCopy;
113+
}
114+
115+
function safeFetch(url, initCopy) {
116+
return fetch(url, initCopy).then((response) =>
117+
response.ok ? response : handleError(response)
118+
);
119+
}
120+
121+
export function backendFetch(url, init, token) {
122+
const initCopy = prepareRequest(init, token);
123+
return safeFetch(url, initCopy);
124+
}
125+
126+
export function backendFetchText(url, init, token) {
127+
const initCopy = prepareRequest(init, token);
128+
return safeFetch(url, initCopy).then((safeResponse) => safeResponse.text());
129+
}
130+
131+
export function backendFetchJson(url, init, token) {
132+
const initCopy = prepareRequest(init, token);
133+
return safeFetch(url, initCopy).then((safeResponse) => safeResponse.json());
134+
}
135+
63136
const getContingencyUriParamType = (contingencyListType) => {
64137
switch (contingencyListType) {
65138
case ContingencyListType.SCRIPT.id:
@@ -411,6 +484,19 @@ export function duplicateElement(
411484
});
412485
}
413486

487+
export function elementExists(directoryUuid, elementName, type) {
488+
const elementNameEncoded = encodeURIComponent(elementName);
489+
const existsElementUrl =
490+
PREFIX_DIRECTORY_SERVER_QUERIES +
491+
`/v1/directories/${directoryUuid}/elements/${elementNameEncoded}/types/${type}`;
492+
console.debug(existsElementUrl);
493+
return backendFetch(existsElementUrl, { method: 'head' }).then(
494+
(response) => {
495+
return response.status !== 204; // HTTP 204 : No-content
496+
}
497+
);
498+
}
499+
414500
export function getNameCandidate(directoryUuid, elementName, type) {
415501
const nameCandidateUrl =
416502
PREFIX_DIRECTORY_SERVER_QUERIES +
@@ -632,7 +718,7 @@ export function connectNotificationsWsUpdateDirectories() {
632718
'/notify?updateType=directories';
633719

634720
const reconnectingWebSocket = new ReconnectingWebSocket(
635-
() => webSocketUrl + '&access_token=' + getUserToken()
721+
() => webSocketUrl + '&access_token=' + getToken()
636722
);
637723
reconnectingWebSocket.onopen = function () {
638724
console.info(

0 commit comments

Comments
 (0)