Skip to content

Commit

Permalink
fix: [DHIS2-17843] Disable delete enrollment button when user does no…
Browse files Browse the repository at this point in the history
…t have authority (#3859)

* fix: string adjustment

* fix: disable button and add tooltip

* fix: revert wrong commit

* fix: create generic authorities hook
  • Loading branch information
henrikmv authored Oct 29, 2024
1 parent 806caa9 commit edee6d3
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 49 deletions.
7 changes: 5 additions & 2 deletions i18n/en.pot
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ msgstr ""
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
"POT-Creation-Date: 2024-10-14T14:53:34.553Z\n"
"PO-Revision-Date: 2024-10-14T14:53:34.553Z\n"
"POT-Creation-Date: 2024-10-25T18:18:11.518Z\n"
"PO-Revision-Date: 2024-10-25T18:18:11.518Z\n"

msgid "Choose one or more dates..."
msgstr "Choose one or more dates..."
Expand Down Expand Up @@ -1133,6 +1133,9 @@ msgstr "Mark as cancelled"
msgid "Mark incomplete"
msgstr "Mark incomplete"

msgid "You do not have access to delete this enrollment"
msgstr "You do not have access to delete this enrollment"

msgid "Delete enrollment"
msgstr "Delete enrollment"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const ActionsPlain = ({
onUpdate,
onDelete,
onUpdateOwnership,
canCascadeDeleteEnrollment,
isTransferLoading,
onAddNew,
loading,
Expand Down Expand Up @@ -115,6 +116,7 @@ export const ActionsPlain = ({
onUpdate={handleOnUpdateStatus}
/>
<Delete
canCascadeDeleteEnrollment={canCascadeDeleteEnrollment}
enrollment={enrollment}
onDelete={handleOnDelete}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ActionsComponent } from './Actions.component';
import type { Props } from './actions.types';
import { useUpdateEnrollment, useDeleteEnrollment } from '../dataMutation/dataMutation';
import { useUpdateOwnership } from './Transfer/hooks';
import { useAuthorities } from '../../../utils/authority/useAuthorities';

export const Actions = ({
enrollment = {},
Expand All @@ -20,6 +21,7 @@ export const Actions = ({
}: Props) => {
const { updateMutation, updateLoading } = useUpdateEnrollment(refetchEnrollment, refetchTEI, onError, onSuccess);
const { deleteMutation, deleteLoading } = useDeleteEnrollment(onDelete, onError, onSuccess);
const { hasAuthority } = useAuthorities({ authorities: ['F_ENROLLMENT_CASCADE_DELETE'] });
const { updateEnrollmentOwnership, isTransferLoading } = useUpdateOwnership({
teiId: enrollment.trackedEntity,
programId: enrollment.program,
Expand Down Expand Up @@ -52,6 +54,7 @@ export const Actions = ({
onUpdate={updateMutation}
onUpdateStatus={handleUpdateStatus}
onDelete={deleteMutation}
canCascadeDeleteEnrollment={hasAuthority}
loading={updateLoading || deleteLoading || updateStatusLoading}
onUpdateOwnership={updateEnrollmentOwnership}
isTransferLoading={isTransferLoading}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @flow
import React, { useState } from 'react';
import {
IconDelete16,
MenuItem,
Expand All @@ -9,18 +10,21 @@ import {
ButtonStrip,
Button,
} from '@dhis2/ui';
import React, { useState } from 'react';
import i18n from '@dhis2/d2-i18n';
import type { Props } from './delete.types';
import { ConditionalTooltip } from '../../../Tooltips/ConditionalTooltip/';

export const Delete = ({ enrollment, onDelete }: Props) => {
export const Delete = ({ canCascadeDeleteEnrollment, enrollment, onDelete }: Props) => {
const [toggle, setToggle] = useState(false);
const disabled = !canCascadeDeleteEnrollment;
const tooltipContent = i18n.t('You do not have access to delete this enrollment');

return (
<div>
<ConditionalTooltip content={tooltipContent} enabled={disabled}>
<MenuItem
dense
dataTest="widget-enrollment-actions-delete"
disabled={disabled}
icon={<IconDelete16 />}
destructive
label={i18n.t('Delete')}
Expand Down Expand Up @@ -54,6 +58,6 @@ export const Delete = ({ enrollment, onDelete }: Props) => {
</ModalActions>
</Modal>
)}
</div>
</ConditionalTooltip>
);
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow

export type Props = {|
canCascadeDeleteEnrollment: boolean,
enrollment: Object,
onDelete: (arg: Object) => void,
|};
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export type PlainProps = {|
onDelete: (arg: Object) => void,
onAddNew: (arg: Object) => void,
onUpdateOwnership: UpdateEnrollmentOwnership,
canCascadeDeleteEnrollment: boolean,
isTransferLoading: boolean,
loading: boolean,
canAddNew: boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { EventChangelogWrapper } from './EventChangelogWrapper';
import { FEATURES, useFeature } from '../../../capture-core-utils';
import { inMemoryFileStore } from '../DataEntry/file/inMemoryFileStore';
import { eventStatuses } from './constants/status.const';
import { useAuthorities } from './hooks';
import { useAuthorities } from '../../utils/authority/useAuthorities';

const styles = {
header: {
Expand Down Expand Up @@ -101,8 +101,8 @@ export const WidgetEventEditPlain = ({
const loadedValues = useSelector(({ viewEventPage }) => viewEventPage.loadedValues);

const eventAccess = getProgramEventAccess(programId, stageId);
const { canEditCompletedEvent } = useAuthorities();
const blockEntryForm = stage.blockEntryForm && !canEditCompletedEvent && eventStatus === eventStatuses.COMPLETED;
const { hasAuthority } = useAuthorities({ authorities: ['F_UNCOMPLETE_EVENT'] });
const blockEntryForm = stage.blockEntryForm && !hasAuthority && eventStatus === eventStatuses.COMPLETED;
const disableEdit = !eventAccess?.write || blockEntryForm;

const tooltipContent = blockEntryForm ?
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// @flow
import React from 'react';
import { useAuthorities } from 'capture-core/utils/authority/useAuthorities';
import type { Props } from './OverflowMenu.types';
import { OverflowMenuComponent } from './OverflowMenu.component';
import { useAuthorities } from './hooks';

export const OverflowMenu = ({
trackedEntityTypeName,
Expand All @@ -13,13 +13,13 @@ export const OverflowMenu = ({
teiId,
programAPI,
}: Props) => {
const { canCascadeDeleteTei } = useAuthorities();
const { hasAuthority } = useAuthorities({ authorities: ['F_TEI_CASCADE_DELETE'] });

return (
<OverflowMenuComponent
trackedEntityTypeName={trackedEntityTypeName}
canWriteData={canWriteData}
canCascadeDeleteTei={canCascadeDeleteTei}
canCascadeDeleteTei={hasAuthority}
trackedEntity={trackedEntity}
onDeleteSuccess={onDeleteSuccess}
displayChangelog={displayChangelog}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
import { useApiMetadataQuery } from 'capture-core/utils/reactQueryHelpers';

const auth = Object.freeze({
F_UNCOMPLETE_EVENT: 'F_UNCOMPLETE_EVENT',
ALL: 'ALL',
});

export const useAuthorities = () => {
export const useAuthorities = ({ authorities }: { authorities: Array<string> }) => {
const queryKey = ['authorities'];
const queryFn = {
resource: 'me.json',
Expand All @@ -15,13 +14,15 @@ export const useAuthorities = () => {
},
};
const queryOptions = {
select: ({ authorities }) =>
authorities &&
authorities.some(authority => authority === auth.ALL || authority === auth.F_UNCOMPLETE_EVENT),
select: ({ authorities: userAuthorities }) =>
userAuthorities &&
authorities.some(
authority => userAuthorities.includes(auth.ALL) || userAuthorities.includes(authority),
),
};
const { data } = useApiMetadataQuery<any>(queryKey, queryFn, queryOptions);

return {
canEditCompletedEvent: Boolean(data),
hasAuthority: Boolean(data),
};
};

0 comments on commit edee6d3

Please sign in to comment.