Skip to content

Commit

Permalink
feat: throw error if FDM config not provided when using FDM (#3516)
Browse files Browse the repository at this point in the history
* feat: make FDM config argument mandatory

* chore: change naming convention to adhere to FDM standard

* feat: throw error if config undefined instead

* fix: argument reorder unnecessary changes

* chore: lint fix

* chore: lint fix

---------

Co-authored-by: Savelii Novikov <45129444+Savokr@users.noreply.github.com>
Co-authored-by: Savelii Novikov <kvaka2000@gmail.com>
  • Loading branch information
3 people authored Jul 31, 2023
1 parent 4b4f1cf commit a46d43f
Showing 1 changed file with 16 additions and 24 deletions.
40 changes: 16 additions & 24 deletions react-components/src/hooks/useFdmAssetMappings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,31 @@ import { type UseQueryResult, useQuery } from '@tanstack/react-query';
import { type FdmAssetMappingsConfig, type ThreeDModelMappings } from './types';
import { DEFAULT_QUERY_STALE_TIME } from '../utilities/constants';

const DefaultFdmConfig: FdmAssetMappingsConfig = {
source: {
space: 'fdm-3d-test-savelii',
version: '1',
type: 'view',
externalId: 'CDF_3D_Connection_Data'
},
assetFdmSpace: 'bark-corporation'
};

/**
* This hook fetches the list of FDM asset mappings for the given external ids
*/
export const useFdmAssetMappings = (
fdmAssetExternalIds: CogniteExternalId[],
fdmConfig: FdmAssetMappingsConfig = DefaultFdmConfig
fdmConfig?: FdmAssetMappingsConfig
): UseQueryResult<ThreeDModelMappings[]> => {
const fdmSdk = useFdmSdk();

const fdmAssetMappingFilter = {
in: {
property: ['edge', 'startNode'],
values: fdmAssetExternalIds.map((externalId) => ({
space: fdmConfig.assetFdmSpace,
externalId
}))
}
};

return useQuery(
['reveal', 'react-components', fdmAssetExternalIds],
async () => {
if (fdmAssetExternalIds?.length === 0) return [];
if (fdmConfig === undefined)
throw Error('FDM config must be defined when using FDM asset mappings');

const fdmAssetMappingFilter = {
in: {
property: ['edge', 'startNode'],
values: fdmAssetExternalIds.map((externalId) => ({
space: fdmConfig.assetFdmSpace,
externalId
}))
}
};

const instances = await fdmSdk.filterInstances(
fdmAssetMappingFilter,
Expand All @@ -56,7 +48,7 @@ export const useFdmAssetMappings = (
];

const modelId = Number.parseInt(instance.endNode.externalId.slice(9));
const revisionId = mappingProperty.RevisionId;
const revisionId = mappingProperty.revisionId;

const isAdded = modelMappingsTemp.some(
(mapping) => mapping.modelId === modelId && mapping.revisionId === revisionId
Expand All @@ -67,7 +59,7 @@ export const useFdmAssetMappings = (
modelId,
revisionId,
mappings: [
{ nodeId: mappingProperty.NodeId, externalId: instance.startNode.externalId }
{ nodeId: mappingProperty.revisionNodeId, externalId: instance.startNode.externalId }
]
});
} else {
Expand All @@ -76,7 +68,7 @@ export const useFdmAssetMappings = (
);

modelMapping?.mappings.push({
nodeId: mappingProperty.NodeId,
nodeId: mappingProperty.revisionNodeId,
externalId: instance.startNode.externalId
});
}
Expand Down

0 comments on commit a46d43f

Please sign in to comment.