-
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: [DHIS2-16372] Delete Relationships (#3520)
- Loading branch information
1 parent
4a1ad4f
commit 9fed562
Showing
17 changed files
with
237 additions
and
18 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
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
85 changes: 85 additions & 0 deletions
85
...s/WidgetsRelationship/common/RelationshipsWidget/DeleteRelationship/DeleteRelationship.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,85 @@ | ||
// @flow | ||
|
||
import React, { useState } from 'react'; | ||
import i18n from '@dhis2/d2-i18n'; | ||
import { | ||
DataTableCell, | ||
IconDelete16, | ||
Modal, | ||
ModalContent, | ||
ModalTitle, | ||
ModalActions, | ||
ButtonStrip, | ||
Button, | ||
colors, | ||
} from '@dhis2/ui'; | ||
import { IconButton } from 'capture-ui'; | ||
import { withStyles } from '@material-ui/core/styles'; | ||
|
||
type Props = { | ||
handleDeleteRelationship: () => void, | ||
disabled?: boolean, | ||
classes: { | ||
tableCell: string, | ||
}, | ||
} | ||
|
||
const styles = { | ||
tableCell: { | ||
display: 'flex', | ||
justifyContent: 'center', | ||
}, | ||
}; | ||
|
||
export const DeleteRelationshipPlain = ({ handleDeleteRelationship, disabled, classes }: Props) => { | ||
const [isModalOpen, setIsModalOpen] = useState(false); | ||
return ( | ||
<> | ||
<DataTableCell className={classes.tableCell}> | ||
<IconButton | ||
onClick={() => { | ||
if (disabled) return; | ||
setIsModalOpen(true); | ||
}} | ||
dataTest={'delete-relationship-button'} | ||
> | ||
<IconDelete16 color={colors.red600} /> | ||
</IconButton> | ||
</DataTableCell> | ||
|
||
{isModalOpen && ( | ||
<Modal | ||
hide={!isModalOpen} | ||
onClose={() => setIsModalOpen(false)} | ||
dataTest={'delete-relationship-modal'} | ||
> | ||
<ModalTitle>{i18n.t('Delete relationship')}</ModalTitle> | ||
<ModalContent> | ||
{i18n.t('Deleting the relationship is permanent and cannot be undone. Are you sure you want to delete this relationship?')} | ||
</ModalContent> | ||
|
||
<ModalActions> | ||
<ButtonStrip> | ||
<Button onClick={() => setIsModalOpen(false)}> | ||
{i18n.t('No, cancel')} | ||
</Button> | ||
|
||
<Button | ||
destructive | ||
dataTest={'delete-relationship-confirmation-button'} | ||
onClick={() => { | ||
handleDeleteRelationship(); | ||
setIsModalOpen(false); | ||
}} | ||
> | ||
{i18n.t('Yes, delete relationship')} | ||
</Button> | ||
</ButtonStrip> | ||
</ModalActions> | ||
</Modal> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
export const DeleteRelationship = withStyles(styles)(DeleteRelationshipPlain); |
3 changes: 3 additions & 0 deletions
3
...ore/components/WidgetsRelationship/common/RelationshipsWidget/DeleteRelationship/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 { DeleteRelationship } from './DeleteRelationship'; |
69 changes: 69 additions & 0 deletions
69
...idgetsRelationship/common/RelationshipsWidget/DeleteRelationship/useDeleteRelationship.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,69 @@ | ||
// @flow | ||
import i18n from '@dhis2/d2-i18n'; | ||
import log from 'loglevel'; | ||
import { errorCreator, FEATURES, useFeature } from 'capture-core-utils'; | ||
import { handleAPIResponse, REQUESTED_ENTITIES } from 'capture-core/utils/api'; | ||
import { useMutation, useQueryClient } from 'react-query'; | ||
import { useAlert, useDataEngine } from '@dhis2/app-runtime'; | ||
import { ReactQueryAppNamespace } from '../../../../../utils/reactQueryHelpers'; | ||
|
||
type Props = { | ||
sourceId: string, | ||
}; | ||
|
||
export type OnDeleteRelationship = ({ relationshipId: string }) => void; | ||
|
||
const deleteRelationshipMutation = { | ||
resource: 'tracker?importStrategy=DELETE&async=false', | ||
type: 'create', | ||
data: ({ relationshipId }) => ({ | ||
relationships: [ | ||
{ | ||
relationship: relationshipId, | ||
}, | ||
], | ||
}), | ||
}; | ||
export const useDeleteRelationship = ({ sourceId }: Props): { onDeleteRelationship: OnDeleteRelationship } => { | ||
const dataEngine = useDataEngine(); | ||
const queryKey: string = useFeature(FEATURES.exportablePayload) ? 'relationships' : 'instances'; | ||
const queryClient = useQueryClient(); | ||
const { show: showError } = useAlert( | ||
i18n.t('An error occurred while deleting the relationship.'), | ||
{ | ||
critical: true, | ||
}, | ||
); | ||
const { mutate: onDeleteRelationship } = useMutation( | ||
({ relationshipId }) => dataEngine.mutate(deleteRelationshipMutation, { variables: { relationshipId } }), | ||
{ | ||
onMutate: ({ relationshipId }) => { | ||
const prevRelationships = queryClient | ||
.getQueryData([ReactQueryAppNamespace, 'relationships', sourceId]); | ||
|
||
const apiRelationships = handleAPIResponse(REQUESTED_ENTITIES.relationships, prevRelationships); | ||
|
||
const newRelationships = apiRelationships | ||
?.filter(({ relationship }) => relationship !== relationshipId); | ||
|
||
queryClient.setQueryData( | ||
[ReactQueryAppNamespace, 'relationships', sourceId], | ||
{ [queryKey]: newRelationships }); | ||
|
||
return { prevRelationships }; | ||
}, | ||
onError: (error, { relationshipId }, context) => { | ||
log.error(errorCreator('An error occurred while deleting the relationship')({ error, relationshipId })); | ||
showError(); | ||
|
||
if (!context?.prevRelationships) return; | ||
queryClient.setQueryData( | ||
[ReactQueryAppNamespace, 'relationships', sourceId], | ||
context.prevRelationships, | ||
); | ||
}, | ||
}, | ||
); | ||
|
||
return { onDeleteRelationship }; | ||
}; |
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
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
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
2 changes: 2 additions & 0 deletions
2
...e/components/WidgetsRelationship/common/RelationshipsWidget/linkedEntitiesViewer.types.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
2 changes: 2 additions & 0 deletions
2
...core/components/WidgetsRelationship/common/RelationshipsWidget/linkedEntityTable.types.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
2 changes: 2 additions & 0 deletions
2
.../components/WidgetsRelationship/common/RelationshipsWidget/linkedEntityTableBody.types.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
Oops, something went wrong.