Skip to content

Commit

Permalink
feat: remove duplicated code detected by sonar
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelDemey committed Nov 9, 2024
1 parent 0368c5f commit 8488ab5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
13 changes: 6 additions & 7 deletions src/packages/modules-datasets/datasets/view/menu.tsx
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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;
Expand All @@ -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 (
Expand Down
14 changes: 6 additions & 8 deletions src/packages/modules-datasets/distributions/view/menu.tsx
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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;
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
@@ -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)
);
};
5 changes: 5 additions & 0 deletions src/packages/redux/selectors.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit 8488ab5

Please sign in to comment.