-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add delete enrollments in program
- Loading branch information
1 parent
1344fb4
commit 40bf195
Showing
13 changed files
with
302 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 0 additions & 3 deletions
3
...nents/WorkingLists/TeiWorkingLists/TrackedEntityBulkActions/Actions/DeleteAction/index.js
This file was deleted.
Oops, something went wrong.
52 changes: 52 additions & 0 deletions
52
...Lists/TrackedEntityBulkActions/Actions/DeleteEnrollmentsAction/DeleteEnrollmentsAction.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// @flow | ||
import React, { useState } from 'react'; | ||
import i18n from '@dhis2/d2-i18n'; | ||
import { | ||
Button, | ||
} from '@dhis2/ui'; | ||
import { useAuthority } from '../../../../../../utils/userInfo/useAuthority'; | ||
import { ConditionalTooltip } from '../../../../../Tooltips/ConditionalTooltip'; | ||
import { EnrollmentDeleteModal } from './EnrollmentDeleteModal'; | ||
|
||
type Props = { | ||
selectedRows: { [id: string]: boolean }, | ||
programId: string, | ||
onUpdateList: () => void, | ||
} | ||
|
||
const CASCADE_DELETE_TEI_AUTHORITY = 'F_ENROLLMENT_CASCADE_DELETE'; | ||
|
||
export const DeleteEnrollmentsAction = ({ | ||
selectedRows, | ||
programId, | ||
onUpdateList, | ||
}: Props) => { | ||
const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false); | ||
const { hasAuthority } = useAuthority({ authority: CASCADE_DELETE_TEI_AUTHORITY }); | ||
|
||
return ( | ||
<> | ||
<ConditionalTooltip | ||
enabled={!hasAuthority} | ||
content={i18n.t('You do not have the required authority to delete enrollments')} | ||
> | ||
<Button | ||
small | ||
onClick={() => setIsDeleteDialogOpen(true)} | ||
disabled={!hasAuthority} | ||
> | ||
{i18n.t('Delete enrollments')} | ||
</Button> | ||
</ConditionalTooltip> | ||
|
||
{isDeleteDialogOpen && ( | ||
<EnrollmentDeleteModal | ||
selectedRows={selectedRows} | ||
programId={programId} | ||
onUpdateList={onUpdateList} | ||
setIsDeleteDialogOpen={setIsDeleteDialogOpen} | ||
/> | ||
)} | ||
</> | ||
); | ||
}; |
98 changes: 98 additions & 0 deletions
98
...ulkActions/Actions/DeleteEnrollmentsAction/EnrollmentDeleteModal/EnrollmentDeleteModal.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// @flow | ||
import React from 'react'; | ||
import { | ||
Button, | ||
ButtonStrip, | ||
DataTableCell, | ||
DataTableRow, | ||
Modal, | ||
ModalActions, | ||
ModalContent, | ||
ModalTitle, | ||
} from '@dhis2/ui'; | ||
import i18n from '@dhis2/d2-i18n'; | ||
import { BulkActionCountTable } from '../../../../../WorkingListsBase/BulkActionBar'; | ||
import { useDeleteEnrollments } from '../hooks/useDeleteEnrollments'; | ||
|
||
type Props = { | ||
selectedRows: { [id: string]: boolean }, | ||
programId: string, | ||
onUpdateList: () => void, | ||
setIsDeleteDialogOpen: (open: boolean) => void, | ||
} | ||
|
||
export const EnrollmentDeleteModal = ({ | ||
selectedRows, | ||
programId, | ||
onUpdateList, | ||
setIsDeleteDialogOpen, | ||
}: Props) => { | ||
const { | ||
deleteEnrollments, | ||
isDeletingEnrollments, | ||
enrollmentCounts, | ||
isLoadingEnrollments, | ||
} = useDeleteEnrollments({ | ||
selectedRows, | ||
programId, | ||
onUpdateList, | ||
setIsDeleteDialogOpen, | ||
}); | ||
|
||
return ( | ||
<Modal | ||
small | ||
onClose={() => setIsDeleteDialogOpen(false)} | ||
> | ||
<ModalTitle> | ||
{i18n.t('Delete enrollments')} | ||
</ModalTitle> | ||
|
||
<ModalContent> | ||
{i18n.t('Are you sure you want to delete all enrollments in the selected program?')} | ||
|
||
<BulkActionCountTable | ||
total={enrollmentCounts?.total} | ||
isLoading={isLoadingEnrollments} | ||
totalLabel={i18n.t('Total enrollments to be deleted')} | ||
> | ||
<DataTableRow> | ||
<DataTableCell> | ||
{i18n.t('Active')} | ||
</DataTableCell> | ||
<DataTableCell align={'center'}> | ||
{enrollmentCounts?.active} | ||
</DataTableCell> | ||
</DataTableRow> | ||
|
||
<DataTableRow> | ||
<DataTableCell> | ||
{i18n.t('Completed')} | ||
</DataTableCell> | ||
<DataTableCell align={'center'}> | ||
{enrollmentCounts?.completed} | ||
</DataTableCell> | ||
</DataTableRow> | ||
</BulkActionCountTable> | ||
</ModalContent> | ||
|
||
<ModalActions> | ||
<ButtonStrip> | ||
<Button | ||
secondary | ||
onClick={() => setIsDeleteDialogOpen(false)} | ||
> | ||
{i18n.t('Cancel')} | ||
</Button> | ||
<Button | ||
destructive | ||
loading={isDeletingEnrollments} | ||
onClick={deleteEnrollments} | ||
> | ||
{i18n.t('Delete')} | ||
</Button> | ||
</ButtonStrip> | ||
</ModalActions> | ||
</Modal> | ||
); | ||
}; |
3 changes: 3 additions & 0 deletions
3
...s/TrackedEntityBulkActions/Actions/DeleteEnrollmentsAction/EnrollmentDeleteModal/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// @flow | ||
|
||
export { EnrollmentDeleteModal } from './EnrollmentDeleteModal'; |
103 changes: 103 additions & 0 deletions
103
...ts/TrackedEntityBulkActions/Actions/DeleteEnrollmentsAction/hooks/useDeleteEnrollments.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// @flow | ||
import { useMemo } from 'react'; | ||
import log from 'loglevel'; | ||
import i18n from '@dhis2/d2-i18n'; | ||
import { useMutation } from 'react-query'; | ||
import { useAlert, useDataEngine } from '@dhis2/app-runtime'; | ||
import { handleAPIResponse, REQUESTED_ENTITIES } from '../../../../../../../utils/api'; | ||
import { useApiDataQuery } from '../../../../../../../utils/reactQueryHelpers'; | ||
import { errorCreator } from '../../../../../../../../capture-core-utils'; | ||
|
||
type Props = { | ||
selectedRows: { [id: string]: boolean }, | ||
programId: string, | ||
onUpdateList: () => void, | ||
setIsDeleteDialogOpen: (open: boolean) => void, | ||
} | ||
|
||
export const useDeleteEnrollments = ({ | ||
selectedRows, | ||
programId, | ||
onUpdateList, | ||
setIsDeleteDialogOpen, | ||
}: Props) => { | ||
const dataEngine = useDataEngine(); | ||
const { show: showAlert } = useAlert( | ||
({ message }) => message, | ||
{ critical: true }, | ||
); | ||
|
||
const { data: enrollments, isLoading: isLoadingEnrollments } = useApiDataQuery( | ||
['WorkingLists', 'BulkActionBar', 'DeleteEnrollmentsAction', 'trackedEntities', selectedRows], | ||
{ | ||
resource: 'tracker/trackedEntities', | ||
params: { | ||
fields: 'trackedEntity,enrollments[enrollment,program,status]', | ||
trackedEntities: Object.keys(selectedRows).join(','), | ||
program: programId, | ||
}, | ||
}, | ||
{ | ||
enabled: Object.keys(selectedRows).length > 0, | ||
select: (data: any) => { | ||
const apiTrackedEntities = handleAPIResponse(REQUESTED_ENTITIES.trackedEntities, data); | ||
if (!apiTrackedEntities) return []; | ||
|
||
return apiTrackedEntities | ||
.flatMap(apiTrackedEntity => apiTrackedEntity.enrollments) | ||
// fallback in case the api returns an enrollment in another program | ||
.filter(enrollment => enrollment.program === programId); | ||
}, | ||
}, | ||
); | ||
|
||
const { mutate: deleteEnrollments, isLoading: isDeletingEnrollments } = useMutation<any>( | ||
() => dataEngine.mutate({ | ||
resource: 'tracker?async=false&importStrategy=DELETE', | ||
type: 'create', | ||
data: { | ||
// $FlowFixMe - business logic dictates that enrollments is not undefined at this point | ||
enrollments: enrollments.map(({ enrollment }) => ({ enrollment })), | ||
}, | ||
}), | ||
{ | ||
onError: (error) => { | ||
log.error(errorCreator('An error occurred when deleting enrollments')({ error })); | ||
showAlert({ message: i18n.t('An error occurred when deleting enrollments') }); | ||
}, | ||
onSuccess: () => { | ||
onUpdateList(); | ||
setIsDeleteDialogOpen(false); | ||
}, | ||
}, | ||
); | ||
|
||
const enrollmentCounts = useMemo(() => { | ||
if (!enrollments) { | ||
return null; | ||
} | ||
|
||
const { activeEnrollments, completedEnrollments } = enrollments.reduce((acc, enrollment) => { | ||
if (enrollment.status === 'ACTIVE') { | ||
acc.activeEnrollments += 1; | ||
} else { | ||
acc.completedEnrollments += 1; | ||
} | ||
|
||
return acc; | ||
}, { activeEnrollments: 0, completedEnrollments: 0 }); | ||
|
||
return { | ||
active: activeEnrollments, | ||
completed: completedEnrollments, | ||
total: enrollments.length, | ||
}; | ||
}, [enrollments]); | ||
|
||
return { | ||
deleteEnrollments, | ||
isDeletingEnrollments, | ||
isLoadingEnrollments, | ||
enrollmentCounts, | ||
}; | ||
}; |
3 changes: 3 additions & 0 deletions
3
...ngLists/TeiWorkingLists/TrackedEntityBulkActions/Actions/DeleteEnrollmentsAction/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// @flow | ||
|
||
export { DeleteEnrollmentsAction } from './DeleteEnrollmentsAction'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
3 changes: 3 additions & 0 deletions
3
...ts/WorkingLists/TeiWorkingLists/TrackedEntityBulkActions/Actions/DeleteTeiAction/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// @flow | ||
|
||
export { DeleteTeiAction } from './DeleteTeiAction'; |
2 changes: 1 addition & 1 deletion
2
...re-core/components/WorkingLists/TeiWorkingLists/TrackedEntityBulkActions/Actions/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// @flow | ||
|
||
export { CompleteAction } from './CompleteAction'; | ||
export { DeleteAction } from './DeleteAction'; | ||
export { DeleteTeiAction } from './DeleteTeiAction'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters