Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Filter context #460

Merged
merged 18 commits into from
May 29, 2024
149 changes: 61 additions & 88 deletions src/components/DirectoryItemSelector/directory-item-selector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ import {
} from '../TreeViewFinder/TreeViewFinder';
import { UUID } from 'crypto';
import { useSnackMessage } from '../../hooks/useSnackMessage';
import { ElementAttributes } from '../../utils/types';
import {
fetchDirectoryContent,
fetchElementsInfos,
fetchRootFolders,
} from '../../services';

const styles = {
icon: (theme: Theme) => ({
Expand All @@ -37,16 +41,6 @@ interface DirectoryItemSelectorProps extends TreeViewFinderProps {
types: string[];
equipmentTypes?: string[];
itemFilter?: any;
fetchDirectoryContent?: (
directoryUuid: UUID,
elementTypes: string[]
) => Promise<ElementAttributes[]>;
fetchRootFolders?: (types: string[]) => Promise<ElementAttributes[]>;
fetchElementsInfos?: (
ids: UUID[],
elementTypes: string[],
equipmentTypes: string[]
) => Promise<ElementAttributes[]>;
classes?: any;
contentText?: string;
defaultExpanded?: string[];
Expand All @@ -64,9 +58,6 @@ const DirectoryItemSelector: FunctionComponent<DirectoryItemSelectorProps> = ({
types,
equipmentTypes,
itemFilter,
fetchDirectoryContent,
fetchRootFolders,
fetchElementsInfos,
expanded,
...otherTreeViewFinderProps
}) => {
Expand Down Expand Up @@ -142,88 +133,70 @@ const DirectoryItemSelector: FunctionComponent<DirectoryItemSelectorProps> = ({
);

const updateRootDirectories = useCallback(() => {
fetchRootFolders &&
fetchRootFolders(types)
.then((data) => {
let [nrs, mdr] = updatedTree(
rootsRef.current,
nodeMap.current,
null,
data
);
setRootDirectories(nrs);
nodeMap.current = mdr;
setData(convertRoots(nrs));
})
.catch((error) => {
snackError({
messageTxt: error.message,
headerId: 'DirectoryItemSelector',
});
fetchRootFolders(types)
.then((data) => {
let [nrs, mdr] = updatedTree(
rootsRef.current,
nodeMap.current,
null,
data
);
setRootDirectories(nrs);
nodeMap.current = mdr;
setData(convertRoots(nrs));
})
.catch((error) => {
snackError({
messageTxt: error.message,
headerId: 'DirectoryItemSelector',
});
}, [convertRoots, types, snackError, fetchRootFolders]);
});
}, [convertRoots, types, snackError]);

const fetchDirectory = useCallback(
(nodeId: UUID): void => {
const typeList = types.includes(ElementType.DIRECTORY) ? [] : types;
fetchDirectoryContent &&
fetchDirectoryContent(nodeId, typeList)
.then((children) => {
const childrenMatchedTypes = children.filter(
(item: any) => contentFilter().has(item.type)
);
fetchDirectoryContent(nodeId, typeList)
.then((children) => {
const childrenMatchedTypes = children.filter((item: any) =>
contentFilter().has(item.type)
);

if (
childrenMatchedTypes.length > 0 &&
equipmentTypes &&
equipmentTypes.length > 0
) {
fetchElementsInfos &&
fetchElementsInfos(
childrenMatchedTypes.map(
(e: any) => e.elementUuid
),
types,
equipmentTypes
).then((childrenWithMetadata) => {
const children = itemFilter
? childrenWithMetadata.filter(
(val: any) => {
// Accept every directory
if (
val.type ===
ElementType.DIRECTORY
) {
return true;
}
// otherwise filter with the custom itemFilter func
return itemFilter(val);
}
)
: childrenWithMetadata;
// update directory content
addToDirectory(nodeId, children);
});
} else {
if (
childrenMatchedTypes.length > 0 &&
equipmentTypes &&
equipmentTypes.length > 0
) {
fetchElementsInfos(
childrenMatchedTypes.map((e: any) => e.elementUuid),
types,
equipmentTypes
).then((childrenWithMetadata) => {
const children = itemFilter
? childrenWithMetadata.filter((val: any) => {
// Accept every directory
if (val.type === ElementType.DIRECTORY) {
return true;
}
// otherwise filter with the custom itemFilter func
return itemFilter(val);
})
: childrenWithMetadata;
// update directory content
addToDirectory(nodeId, childrenMatchedTypes);
}
})
.catch((error) => {
console.warn(
`Could not update subs (and content) of '${nodeId}' : ${error.message}`
);
});
addToDirectory(nodeId, children);
});
} else {
// update directory content
addToDirectory(nodeId, childrenMatchedTypes);
}
})
.catch((error) => {
console.warn(
`Could not update subs (and content) of '${nodeId}' : ${error.message}`
);
});
},
[
types,
equipmentTypes,
itemFilter,
contentFilter,
addToDirectory,
fetchDirectoryContent,
fetchElementsInfos,
]
[types, equipmentTypes, itemFilter, contentFilter, addToDirectory]
);

useEffect(() => {
Expand Down
6 changes: 3 additions & 3 deletions src/components/TopBar/TopBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import GridLogo, { GridLogoProps } from './GridLogo';
import AboutDialog, { AboutDialogProps } from './AboutDialog';
import { LogoutProps } from '../Login/Logout';
import { User } from 'oidc-client';
import { Metadata } from '../../hooks/predefined-properties-hook';
import { CommonMetadata } from '../../services';

const styles = {
grow: {
Expand Down Expand Up @@ -167,7 +167,7 @@ export type TopBarProps = Omit<GridLogoProps, 'onClick'> &
onLogoClick: GridLogoProps['onClick'];
user: User;
onAboutClick?: () => void;
appsAndUrls: Metadata[];
appsAndUrls: CommonMetadata[];
onThemeClick?: (theme: GsTheme) => void;
theme?: GsTheme;
onEquipmentLabellingClick?: (toggle: boolean) => void;
Expand Down Expand Up @@ -309,7 +309,7 @@ const TopBar = ({
<Box
component="a"
key={item.name}
href={item.url}
href={item.url?.toString()}
sx={styles.link}
target="_blank"
rel="noopener noreferrer"
Expand Down
33 changes: 11 additions & 22 deletions src/components/dialogs/modify-element-selection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import React, { useContext, useEffect, useState } from 'react';
import React, { useEffect, useState } from 'react';
import { Button, Grid, Typography } from '@mui/material';
import { FormattedMessage, useIntl } from 'react-intl';
import { useController } from 'react-hook-form';
import { UUID } from 'crypto';
import { FilterContext } from '../filter/filter-context';
import { TreeViewFinderNodeProps } from '../TreeViewFinder';
import { FieldConstants } from '../../utils/field-constants';
import DirectoryItemSelector from '../DirectoryItemSelector/directory-item-selector';
import { ElementType } from '../../utils/ElementType';
import { fetchDirectoryElementPath } from '../../services';

export interface ModifyElementSelectionProps {
elementType: ElementType;
Expand All @@ -33,13 +33,6 @@ const ModifyElementSelection: React.FunctionComponent<
const [open, setOpen] = useState<boolean>(false);
const [activeDirectoryName, setActiveDirectoryName] = useState('');

const {
fetchDirectoryContent,
fetchRootFolders,
fetchElementsInfos,
fetchPath,
} = useContext(FilterContext);

const {
field: { onChange, value: directory },
} = useController({
Expand All @@ -48,17 +41,16 @@ const ModifyElementSelection: React.FunctionComponent<

useEffect(() => {
if (directory) {
fetchPath &&
fetchPath(directory).then((res: any) => {
setActiveDirectoryName(
res
.map((element: any) => element.elementName.trim())
.reverse()
.join('/')
);
});
fetchDirectoryElementPath(directory).then((res: any) => {
setActiveDirectoryName(
res
.map((element: any) => element.elementName.trim())
.reverse()
.join('/')
);
});
}
}, [directory, fetchPath]);
}, [directory]);

const handleSelectFolder = () => {
setOpen(true);
Expand Down Expand Up @@ -122,9 +114,6 @@ const ModifyElementSelection: React.FunctionComponent<
contentText={intl.formatMessage({
id: props.dialogMessageLabel,
})}
fetchDirectoryContent={fetchDirectoryContent}
fetchRootFolders={fetchRootFolders}
fetchElementsInfos={fetchElementsInfos}
/>
</Grid>
);
Expand Down
39 changes: 6 additions & 33 deletions src/components/filter/expert/expert-filter-edition-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@ import { saveExpertFilter } from '../utils/filter-api';
import { importExpertRules } from './expert-filter-utils';
import { UUID } from 'crypto';
import { elementExistsType } from '../criteria-based/criteria-based-filter-edition-dialog';
import { FilterContext } from '../filter-context';
import { FilterType } from '../constants/filter-constants';
import { FetchStatus } from '../../../utils/FetchStatus';
import { ElementAttributes } from '../../../utils/types';
import { StudyMetadata } from '../../../hooks/predefined-properties-hook';

const formSchema = yup
.object()
Expand All @@ -49,17 +46,6 @@ export interface ExpertFilterEditionDialogProps {
activeDirectory?: UUID;
elementExists?: elementExistsType;
language?: string;
fetchDirectoryContent: (
directoryUuid: UUID,
elementTypes: string[]
) => Promise<ElementAttributes[]>;
fetchRootFolders: (types: string[]) => Promise<ElementAttributes[]>;
fetchElementsInfos: (
ids: UUID[],
elementTypes?: string[],
equipmentTypes?: string[]
) => Promise<ElementAttributes[]>;
fetchAppsAndUrls: () => Promise<StudyMetadata[]>;
}

const ExpertFilterEditionDialog: FunctionComponent<
Expand All @@ -77,10 +63,6 @@ const ExpertFilterEditionDialog: FunctionComponent<
activeDirectory,
elementExists,
language,
fetchDirectoryContent,
fetchRootFolders,
fetchElementsInfos,
fetchAppsAndUrls,
}) => {
const { snackError } = useSnackMessage();
const [dataFetchStatus, setDataFetchStatus] = useState(FetchStatus.IDLE);
Expand Down Expand Up @@ -174,21 +156,12 @@ const ExpertFilterEditionDialog: FunctionComponent<
isDataFetching={dataFetchStatus === FetchStatus.FETCHING}
language={language}
>
<FilterContext.Provider
value={{
fetchDirectoryContent: fetchDirectoryContent,
fetchRootFolders: fetchRootFolders,
fetchElementsInfos: fetchElementsInfos,
fetchAppsAndUrls: fetchAppsAndUrls,
}}
>
{isDataReady && (
<FilterForm
activeDirectory={activeDirectory}
elementExists={elementExists}
/>
)}
</FilterContext.Provider>
{isDataReady && (
<FilterForm
activeDirectory={activeDirectory}
elementExists={elementExists}
/>
)}
</CustomMuiDialog>
);
};
Expand Down
32 changes: 0 additions & 32 deletions src/components/filter/filter-context.ts

This file was deleted.

Loading
Loading