diff --git a/src/packages/modules-datasets/datasets/view/menu.tsx b/src/packages/modules-datasets/datasets/view/menu.tsx index 714333b58..870c094a1 100644 --- a/src/packages/modules-datasets/datasets/view/menu.tsx +++ b/src/packages/modules-datasets/datasets/view/menu.tsx @@ -1,4 +1,4 @@ -import { ADMIN, DATASET_CONTRIBUTOR } from '../../../auth/roles'; +import { ADMIN } from '../../../auth/roles'; import { ValidationButton } from '../../../components'; import { ActionToolbar } from '../../../components/action-toolbar'; import { @@ -10,6 +10,7 @@ import { Dataset } from '../../../model/Dataset'; import { UNPUBLISHED } from '../../../model/ValidationState'; import { usePermission } from '../../../redux/hooks/usePermission'; import { useGoBack } from '../../../utils/hooks/useGoBack'; +import { checkIfContributorContainsUserStamp } from '../../utils/check-stamp-with-contributor'; type ViewMenuTypes = { dataset: Dataset; @@ -25,13 +26,11 @@ export const ViewMenu = ({ const permission = usePermission(); - const contributors = Array.isArray(dataset?.catalogRecord?.contributor) - ? dataset?.catalogRecord?.contributor - : [dataset?.catalogRecord?.contributor]; + const hasDatasetRightsBasedOnStamp = checkIfContributorContainsUserStamp( + dataset, + permission, + ); - const hasDatasetRightsBasedOnStamp = - contributors.find((c) => c === permission?.stamp) && - permission?.roles?.includes(DATASET_CONTRIBUTOR); const isAdmin = permission?.roles?.includes(ADMIN); return ( diff --git a/src/packages/modules-datasets/distributions/view/menu.tsx b/src/packages/modules-datasets/distributions/view/menu.tsx index e93fd3bda..55e824221 100644 --- a/src/packages/modules-datasets/distributions/view/menu.tsx +++ b/src/packages/modules-datasets/distributions/view/menu.tsx @@ -1,4 +1,4 @@ -import { ADMIN, DATASET_CONTRIBUTOR } from '../../../auth/roles'; +import { ADMIN } from '../../../auth/roles'; import { ValidationButton } from '../../../components'; import { ActionToolbar } from '../../../components/action-toolbar'; import { @@ -10,6 +10,7 @@ import { Dataset, Distribution } from '../../../model/Dataset'; import { UNPUBLISHED } from '../../../model/ValidationState'; import { usePermission } from '../../../redux/hooks/usePermission'; import { useGoBack } from '../../../utils/hooks/useGoBack'; +import { checkIfContributorContainsUserStamp } from '../../utils/check-stamp-with-contributor'; type ViewMenuTypes = { distribution: Distribution; @@ -28,13 +29,10 @@ export const ViewMenu = ({ const permission = usePermission(); - const contributors = Array.isArray(dataset?.catalogRecord?.contributor) - ? dataset?.catalogRecord?.contributor - : [dataset?.catalogRecord?.contributor]; - - const hasDatasetRightsBasedOnStamp = - contributors.find((c) => c === permission?.stamp) && - permission?.roles?.includes(DATASET_CONTRIBUTOR); + const hasDatasetRightsBasedOnStamp = checkIfContributorContainsUserStamp( + dataset, + permission, + ); const isAdmin = permission?.roles?.includes(ADMIN); diff --git a/src/packages/modules-datasets/utils/check-stamp-with-contributor.ts b/src/packages/modules-datasets/utils/check-stamp-with-contributor.ts new file mode 100644 index 000000000..885cabc21 --- /dev/null +++ b/src/packages/modules-datasets/utils/check-stamp-with-contributor.ts @@ -0,0 +1,17 @@ +import { DATASET_CONTRIBUTOR } from '../../auth/roles'; +import { Dataset } from '../../model/Dataset'; +import { Permission } from '../../redux/selectors'; + +export const checkIfContributorContainsUserStamp = ( + dataset: Dataset, + permission: Permission, +) => { + const contributors = Array.isArray(dataset?.catalogRecord?.contributor) + ? dataset?.catalogRecord?.contributor + : [dataset?.catalogRecord?.contributor]; + + return ( + contributors.find((c) => c === permission?.stamp) && + permission?.roles?.includes(DATASET_CONTRIBUTOR) + ); +}; diff --git a/src/packages/redux/selectors.ts b/src/packages/redux/selectors.ts index 658bc5ff7..a26760d7b 100644 --- a/src/packages/redux/selectors.ts +++ b/src/packages/redux/selectors.ts @@ -1,5 +1,10 @@ import { ReduxModel } from './model'; +export type Permission = { + authType: string; + roles: string[]; + stamp: string; +} export const getPermission = (state: ReduxModel) => { const { type: authType,