Skip to content

Commit 14e64f2

Browse files
authored
Non directory elements are not appearing anymore in left bar (#437)
Signed-off-by: LE SAULNIER Kevin <[email protected]>
1 parent ccf9f1f commit 14e64f2

File tree

3 files changed

+58
-9
lines changed

3 files changed

+58
-9
lines changed

src/components/search/search-bar.tsx

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,23 @@ import {
1919
fetchDirectoryContent,
2020
searchElementsInfos,
2121
} from '../../utils/rest-api';
22-
import { useDebounce, useSnackMessage } from '@gridsuite/commons-ui';
22+
import {
23+
ElementType,
24+
useDebounce,
25+
useSnackMessage,
26+
} from '@gridsuite/commons-ui';
2327
import { Search } from '@mui/icons-material';
2428
import { useDispatch, useSelector } from 'react-redux';
2529
import { setSelectedDirectory, setTreeData } from '../../redux/actions';
2630
import { updatedTree } from '../tree-views-container';
2731
import { useIntl } from 'react-intl';
2832
import { SearchItem } from './search-item';
29-
import { IDirectory, ITreeData, ReduxState } from '../../redux/reducer.type';
33+
import {
34+
IDirectory,
35+
IElement,
36+
ITreeData,
37+
ReduxState,
38+
} from '../../redux/reducer.type';
3039

3140
export const SEARCH_FETCH_TIMEOUT_MILLIS = 1000; // 1 second
3241

@@ -112,7 +121,7 @@ export const SearchBar: FunctionComponent<SearchBarProps> = ({ inputRef }) => {
112121
);
113122

114123
const updateMapData = useCallback(
115-
(nodeId: string, children: IDirectory) => {
124+
(nodeId: string, children: IDirectory[]) => {
116125
if (!treeDataRef.current) {
117126
return;
118127
}
@@ -153,8 +162,13 @@ export const SearchBar: FunctionComponent<SearchBarProps> = ({ inputRef }) => {
153162
const elementUuidPath = matchingElement?.pathUuid.reverse();
154163
const promises = elementUuidPath.map((e: string) => {
155164
return fetchDirectoryContent(e)
156-
.then((res) => {
157-
updateMapData(e, res);
165+
.then((res: IElement[]) => {
166+
updateMapData(
167+
e,
168+
res.filter(
169+
(res) => res.type === ElementType.DIRECTORY
170+
) as IDirectory[]
171+
);
158172
})
159173
.catch((error) =>
160174
snackError({

src/redux/reducer.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
USER_VALIDATION_ERROR,
4040
RESET_AUTHENTICATION_ROUTER_ERROR,
4141
SHOW_AUTH_INFO_LOGIN,
42+
ElementType,
4243
} from '@gridsuite/commons-ui';
4344
import { PARAM_LANGUAGE, PARAM_THEME } from '../utils/config-params';
4445

@@ -73,6 +74,12 @@ const initialState = {
7374
...paramsInitialState,
7475
};
7576

77+
const filterFromObject = (objectToFilter, filterMethod) => {
78+
return Object.fromEntries(
79+
Object.entries(objectToFilter).filter(filterMethod)
80+
);
81+
};
82+
7683
export const reducer = createReducer(initialState, (builder) => {
7784
builder.addCase(SELECT_THEME, (state, action) => {
7885
state.theme = action.theme;
@@ -160,7 +167,29 @@ export const reducer = createReducer(initialState, (builder) => {
160167
});
161168

162169
builder.addCase(TREE_DATA, (state, action) => {
163-
state.treeData = action.treeData;
170+
//TODO: remove those filters below when this file has been migrated to Typescript
171+
// it's only here to prevent regressions due to javascript not checking types
172+
173+
// filtering non DIRECTORY elements from action.treeData
174+
// used to prevent non DIRECTORY elements from appearing in left menu
175+
const filteredTreeDataRootDirectories =
176+
action.treeData.rootDirectories.filter(
177+
(element) => element.type === ElementType.DIRECTORY
178+
);
179+
180+
// action.treeData.mapData is an object looking like this : {<elementId1>: <element1>, <elementId2>: <element2>}
181+
// Object.entries changes it to an array looking like [[elementId1, element1], [elementId2, element2]], in order to make it easier to filter
182+
// Object.fromEntries will turn [[elementId1, element1], [elementId2, element2]] back to {<elementId1>: <element1>, <elementId2>: <element2>} which is the initial form
183+
const filteredTreeDataMapData = filterFromObject(
184+
action.treeData.mapData,
185+
([elementId, element]) => element.type === ElementType.DIRECTORY
186+
);
187+
188+
state.treeData = {
189+
...action.treeData,
190+
rootDirectories: filteredTreeDataRootDirectories,
191+
mapData: filteredTreeDataMapData,
192+
};
164193
});
165194

166195
builder.addCase(SELECTION_FOR_COPY, (state, action) => {

src/redux/reducer.type.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* License, v. 2.0. If a copy of the MPL was not distributed with this
55
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
66
*/
7+
import { ElementType } from '@gridsuite/commons-ui';
78
import { UUID } from 'crypto';
89

910
type UserProfile = {
@@ -22,10 +23,10 @@ interface IUser {
2223
expires_at: number;
2324
}
2425

25-
export interface IDirectory {
26+
export interface IElement {
2627
elementUuid: UUID;
2728
elementName: string;
28-
type: 'DIRECTORY';
29+
type: ElementType;
2930
owner: string;
3031
subdirectoriesCount: number;
3132
creationDate: string;
@@ -35,9 +36,14 @@ export interface IDirectory {
3536
parentUuid: null | UUID;
3637
}
3738

39+
// IDirectory is exactly an IElement, with a specific type value
40+
export type IDirectory = IElement & {
41+
type: ElementType.DIRECTORY;
42+
};
43+
3844
export interface ITreeData {
3945
rootDirectories: IDirectory[];
40-
mapData: Record<string, any>;
46+
mapData: Record<string, IDirectory>;
4147
}
4248

4349
export interface ReduxState {

0 commit comments

Comments
 (0)